Compare commits
No commits in common. "b7c1d85830d297cc84ef4590d88802707a680f1c" and "0c2aaa0b388cf3c51da17456a2f8a1befff30526" have entirely different histories.
b7c1d85830
...
0c2aaa0b38
24 changed files with 335 additions and 294 deletions
|
|
@ -20,14 +20,14 @@ func Handler(app *model.AppState) http.Handler {
|
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/account/", accountIndexHandler(app))
|
||||
mux.Handle("/", accountIndexHandler(app))
|
||||
|
||||
mux.Handle("/account/totp-setup", totpSetupHandler(app))
|
||||
mux.Handle("/account/totp-confirm", totpConfirmHandler(app))
|
||||
mux.Handle("/account/totp-delete", totpDeleteHandler(app))
|
||||
mux.Handle("/totp-setup", totpSetupHandler(app))
|
||||
mux.Handle("/totp-confirm", totpConfirmHandler(app))
|
||||
mux.Handle("/totp-delete", totpDeleteHandler(app))
|
||||
|
||||
mux.Handle("/account/password", changePasswordHandler(app))
|
||||
mux.Handle("/account/delete", deleteAccountHandler(app))
|
||||
mux.Handle("/password", changePasswordHandler(app))
|
||||
mux.Handle("/delete", deleteAccountHandler(app))
|
||||
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ func Handler(app *model.AppState) http.Handler {
|
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/blogs/{id}", serveBlogPost(app))
|
||||
mux.Handle("/blogs/", serveBlogIndex(app))
|
||||
mux.Handle("/{id}", serveBlogPost(app))
|
||||
mux.Handle("/", serveBlogIndex(app))
|
||||
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -26,10 +26,10 @@ func Handler(app *model.AppState) http.Handler {
|
|||
mux.Handle("/logout", core.RequireAccount(auth.LogoutHandler(app)))
|
||||
|
||||
mux.Handle("/logs", core.RequireAccount(logs.Handler(app)))
|
||||
mux.Handle("/music/", core.RequireAccount(music.Handler(app)))
|
||||
mux.Handle("/blogs/", core.RequireAccount(blog.Handler(app)))
|
||||
mux.Handle("/music/", core.RequireAccount(http.StripPrefix("/music", music.Handler(app))))
|
||||
mux.Handle("/blogs/", core.RequireAccount(http.StripPrefix("/blogs", blog.Handler(app))))
|
||||
|
||||
mux.Handle("/account/", core.RequireAccount(account.Handler(app)))
|
||||
mux.Handle("/account/", core.RequireAccount(http.StripPrefix("/account", account.Handler(app))))
|
||||
|
||||
mux.Handle("/static/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/static/admin.css" {
|
||||
|
|
|
|||
|
|
@ -9,23 +9,17 @@ func Handler(app *model.AppState) http.Handler {
|
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/music/releases/{id}/", func(w http.ResponseWriter, r *http.Request) {
|
||||
serveEditRelease(app, r.PathValue("id")).ServeHTTP(w, r)
|
||||
})
|
||||
mux.HandleFunc("/music/releases/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
serveRelease(app, r.PathValue("id")).ServeHTTP(w, r)
|
||||
})
|
||||
mux.Handle("/music/releases/", serveReleases(app))
|
||||
mux.Handle("/releases/", http.StripPrefix("/releases", serveReleases(app)))
|
||||
|
||||
mux.HandleFunc("/music/artists/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("/artists/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
serveArtist(app, r.PathValue("id")).ServeHTTP(w, r)
|
||||
})
|
||||
mux.Handle("/music/artists/", serveArtists(app))
|
||||
mux.Handle("/artists/", http.StripPrefix("/artists", serveArtists(app)))
|
||||
|
||||
mux.HandleFunc("/music/tracks/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
mux.HandleFunc("/tracks/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
serveTrack(app, r.PathValue("id")).ServeHTTP(w, r)
|
||||
})
|
||||
mux.Handle("/music/tracks/", serveTracks(app))
|
||||
mux.Handle("/tracks/", http.StripPrefix("/tracks", serveTracks(app)))
|
||||
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/admin/core"
|
||||
"arimelody-web/admin/templates"
|
||||
|
|
@ -12,7 +13,41 @@ import (
|
|||
)
|
||||
|
||||
func serveReleases(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/{id}/", func(w http.ResponseWriter, r *http.Request) {
|
||||
releaseID := r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch full release data for %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/{id}/editcredits", serveEditCredits(release))
|
||||
mux.Handle("/{id}/addcredit", serveAddCredit(app, release))
|
||||
mux.Handle("/{id}/newcredit", serveNewCredit(app))
|
||||
|
||||
mux.Handle("/{id}/editlinks", serveEditLinks(release))
|
||||
|
||||
mux.Handle("/{id}/edittracks", serveEditTracks(release))
|
||||
mux.Handle("/{id}/addtrack", serveAddTrack(app, release))
|
||||
mux.Handle("/{id}/newtrack", serveNewTrack(app))
|
||||
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
serveRelease(app, r.PathValue("id")).ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
type ReleasesData struct {
|
||||
|
|
@ -40,22 +75,24 @@ func serveReleases(app *model.AppState) http.Handler {
|
|||
return
|
||||
}
|
||||
})
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
func serveRelease(app *model.AppState, releaseID string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch full release data for %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
type ReleaseResponse struct {
|
||||
core.AdminPageData
|
||||
|
|
@ -75,35 +112,6 @@ func serveRelease(app *model.AppState, releaseID string) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func serveEditRelease(app *model.AppState, releaseID string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch full release data for %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("GET /music/releases/{id}/editcredits", serveEditCredits(release))
|
||||
mux.Handle("GET /music/releases/{id}/addcredit", serveAddCredit(app, release))
|
||||
mux.Handle("GET /music/releases/{id}/newcredit/{artistID}", serveNewCredit(app))
|
||||
|
||||
mux.Handle("GET /music/releases/{id}/editlinks", serveEditLinks(release))
|
||||
|
||||
mux.Handle("GET /music/releases/{id}/edittracks", serveEditTracks(release))
|
||||
mux.Handle("GET /music/releases/{id}/addtrack", serveAddTrack(app, release))
|
||||
mux.Handle("GET /music/releases/{id}/newtrack/{trackID}", serveNewTrack(app))
|
||||
|
||||
mux.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func serveEditCredits(release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
|
|
@ -143,7 +151,8 @@ func serveAddCredit(app *model.AppState, release *model.Release) http.Handler {
|
|||
|
||||
func serveNewCredit(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
artistID := r.PathValue("artistID")
|
||||
split := strings.Split(r.URL.Path, "/")
|
||||
artistID := split[len(split) - 1]
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch artist %s: %s\n", artistID, err)
|
||||
|
|
@ -219,7 +228,8 @@ func serveAddTrack(app *model.AppState, release *model.Release) http.Handler {
|
|||
|
||||
func serveNewTrack(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
trackID := r.PathValue("trackID")
|
||||
split := strings.Split(r.URL.Path, "/")
|
||||
trackID := split[len(split) - 1]
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch track %s: %s\n", trackID, err)
|
||||
|
|
|
|||
|
|
@ -32,11 +32,6 @@ input[type="text"] {
|
|||
margin-top: 0;
|
||||
}
|
||||
|
||||
#blogpost button#set-current-date {
|
||||
margin: 0 .5em;
|
||||
padding: .4em .8em;
|
||||
}
|
||||
|
||||
#blogpost h2 {
|
||||
margin: 0;
|
||||
font-size: 2em;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
const blogID = document.getElementById("blogpost").dataset.id;
|
||||
const titleInput = document.getElementById("title");
|
||||
const publishDateInput = document.getElementById("publish-date");
|
||||
const setCurrentDateBtn = document.getElementById("set-current-date");
|
||||
const descInput = document.getElementById("description");
|
||||
const mdInput = document.getElementById("markdown");
|
||||
const blueskyActorInput = document.getElementById("bluesky-actor");
|
||||
|
|
@ -12,13 +11,6 @@ const visInput = document.getElementById("visibility");
|
|||
const saveBtn = document.getElementById("save");
|
||||
const deleteBtn = document.getElementById("delete");
|
||||
|
||||
setCurrentDateBtn.addEventListener("click", () => {
|
||||
let now = new Date;
|
||||
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
|
||||
publishDateInput.value = now.toISOString().slice(0, 16);
|
||||
saveBtn.disabled = false;
|
||||
});
|
||||
|
||||
saveBtn.addEventListener("click", () => {
|
||||
fetch("/api/v1/blog/" + blogID, {
|
||||
method: "PUT",
|
||||
|
|
|
|||
|
|
@ -465,7 +465,7 @@ dialog div.dialog-actions {
|
|||
}
|
||||
|
||||
#edittracks .track {
|
||||
background-color: var(--bg-1);
|
||||
background-color: var(--bg-2);
|
||||
transition: transform .2s ease-out, opacity .2s;
|
||||
}
|
||||
|
||||
|
|
@ -488,7 +488,7 @@ dialog div.dialog-actions {
|
|||
}
|
||||
|
||||
#edittracks .track:nth-child(even) {
|
||||
background-color: var(--bg-0);
|
||||
background-color: var(--bg-1);
|
||||
}
|
||||
|
||||
#edittracks .track-number {
|
||||
|
|
@ -510,17 +510,17 @@ dialog div.dialog-actions {
|
|||
padding: .5em;
|
||||
display: flex;
|
||||
gap: .5em;
|
||||
background-color: var(--bg-1);
|
||||
background-color: var(--bg-0);
|
||||
cursor: pointer;
|
||||
transition: background-color .1s ease-out, color .1s ease-out;
|
||||
}
|
||||
|
||||
#addtrack ul li.new-track:nth-child(even) {
|
||||
background-color: var(--bg-0);
|
||||
background: color-mix(in srgb, var(--bg-0) 95%, #fff);
|
||||
}
|
||||
|
||||
#addtrack ul li.new-track:hover {
|
||||
background-color: var(--bg-2);
|
||||
background: color-mix(in srgb, var(--bg-0) 90%, #fff);
|
||||
}
|
||||
|
||||
@media only screen and (max-width: 1105px) {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@
|
|||
|
||||
<label for="publish-date">Publish Date</label>
|
||||
<input type="datetime-local" name="publish-date" id="publish-date" value="{{.Post.TextPublishDate}}">
|
||||
<button type="button" id="set-current-date">Current date</button>
|
||||
|
||||
<label for="description">Description</label>
|
||||
<textarea
|
||||
|
|
|
|||
|
|
@ -31,13 +31,13 @@
|
|||
<hr>
|
||||
|
||||
<p class="section-label">music</p>
|
||||
<div class="nav-item{{if hasPrefix .Path "/music/releases"}} active{{end}}">
|
||||
<div class="nav-item{{if hasPrefix .Path "/releases"}} active{{end}}">
|
||||
<a href="/admin/music/releases/">releases</a>
|
||||
</div>
|
||||
<div class="nav-item{{if hasPrefix .Path "/music/artists"}} active{{end}}">
|
||||
<div class="nav-item{{if hasPrefix .Path "/artists"}} active{{end}}">
|
||||
<a href="/admin/music/artists/">artists</a>
|
||||
</div>
|
||||
<div class="nav-item{{if hasPrefix .Path "/music/tracks"}} active{{end}}">
|
||||
<div class="nav-item{{if hasPrefix .Path "/tracks"}} active{{end}}">
|
||||
<a href="/admin/music/tracks/">tracks</a>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
148
api/api.go
148
api/api.go
|
|
@ -18,50 +18,142 @@ func Handler(app *model.AppState) http.Handler {
|
|||
|
||||
// ARTIST ENDPOINTS
|
||||
|
||||
mux.Handle("GET /v1/artist/{id}", ServeArtist(app))
|
||||
mux.Handle("PUT /v1/artist/{id}", requireAccount(UpdateArtist(app)))
|
||||
mux.Handle("DELETE /v1/artist/{id}", requireAccount(DeleteArtist(app)))
|
||||
mux.Handle("/v1/artist/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
mux.Handle("GET /v1/artist/", ServeAllArtists(app))
|
||||
mux.Handle("GET /v1/artist", ServeAllArtists(app))
|
||||
mux.Handle("POST /v1/artist/", requireAccount(CreateArtist(app)))
|
||||
mux.Handle("POST /v1/artist", requireAccount(CreateArtist(app)))
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/artist/{id}
|
||||
ServeArtist(app, artist).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/artist/{id} (admin)
|
||||
requireAccount(UpdateArtist(app, artist)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/artist/{id} (admin)
|
||||
requireAccount(DeleteArtist(app, artist)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
artistIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/artist
|
||||
ServeAllArtists(app).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/artist (admin)
|
||||
requireAccount(CreateArtist(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
mux.Handle("/v1/artist/", artistIndexHandler)
|
||||
mux.Handle("/v1/artist", artistIndexHandler)
|
||||
|
||||
// RELEASE ENDPOINTS
|
||||
|
||||
mux.Handle("GET /v1/music/{id}", ServeRelease(app))
|
||||
mux.Handle("PUT /v1/music/{id}", requireAccount(UpdateRelease(app)))
|
||||
mux.Handle("DELETE /v1/music/{id}", requireAccount(DeleteRelease(app)))
|
||||
mux.Handle("/v1/music/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
mux.Handle("PUT /v1/music/{id}/tracks", requireAccount(UpdateReleaseTracks(app)))
|
||||
mux.Handle("PUT /v1/music/{id}/credits", requireAccount(UpdateReleaseCredits(app)))
|
||||
mux.Handle("PUT /v1/music/{id}/links", requireAccount(UpdateReleaseLinks(app)))
|
||||
|
||||
mux.Handle("GET /v1/music/", ServeCatalog(app))
|
||||
mux.Handle("GET /v1/music", ServeCatalog(app))
|
||||
mux.Handle("POST /v1/music/", requireAccount(CreateRelease(app)))
|
||||
mux.Handle("POST /v1/music", requireAccount(CreateRelease(app)))
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/music/{id}
|
||||
ServeRelease(app, release).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/music/{id} (admin)
|
||||
requireAccount(UpdateRelease(app, release)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/music/{id} (admin)
|
||||
requireAccount(DeleteRelease(app, release)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
musicIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/music
|
||||
ServeCatalog(app).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/music (admin)
|
||||
requireAccount(CreateRelease(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
mux.Handle("/v1/music/", musicIndexHandler)
|
||||
mux.Handle("/v1/music", musicIndexHandler)
|
||||
|
||||
// TRACK ENDPOINTS
|
||||
|
||||
mux.Handle("GET /v1/track/{id}", requireAccount(ServeTrack(app)))
|
||||
mux.Handle("PUT /v1/track/{id}", requireAccount(UpdateTrack(app)))
|
||||
mux.Handle("DELETE /v1/track/{id}", requireAccount(DeleteTrack(app)))
|
||||
mux.Handle("/v1/track/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
mux.Handle("GET /v1/track/", requireAccount(ServeAllTracks(app)))
|
||||
mux.Handle("GET /v1/track", requireAccount(ServeAllTracks(app)))
|
||||
mux.Handle("POST /v1/track/", requireAccount(CreateTrack(app)))
|
||||
mux.Handle("POST /v1/track", requireAccount(CreateTrack(app)))
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/track/{id} (admin)
|
||||
requireAccount(ServeTrack(app, track)).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/track/{id} (admin)
|
||||
requireAccount(UpdateTrack(app, track)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/track/{id} (admin)
|
||||
requireAccount(DeleteTrack(app, track)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
trackIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/track (admin)
|
||||
requireAccount(ServeAllTracks(app)).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/track (admin)
|
||||
requireAccount(CreateTrack(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
mux.Handle("/v1/track/", trackIndexHandler)
|
||||
mux.Handle("/v1/track", trackIndexHandler)
|
||||
|
||||
// BLOG ENDPOINTS
|
||||
|
||||
mux.Handle("GET /v1/blog/{id}", ServeBlog(app))
|
||||
mux.Handle("PUT /v1/blog/{id}", requireAccount(UpdateBlog(app)))
|
||||
mux.Handle("DELETE /v1/blog/{id}", requireAccount(DeleteBlog(app)))
|
||||
|
||||
mux.Handle("GET /v1/blog/", ServeAllBlogs(app))
|
||||
mux.Handle("GET /v1/blog", ServeAllBlogs(app))
|
||||
mux.Handle("POST /v1/blog/", requireAccount(CreateBlog(app)))
|
||||
mux.Handle("POST /v1/blog", requireAccount(CreateBlog(app)))
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -35,20 +35,8 @@ func ServeAllArtists(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeArtist(app *model.AppState) http.Handler {
|
||||
func ServeArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type (
|
||||
creditJSON struct {
|
||||
ID string `json:"id"`
|
||||
|
|
@ -133,23 +121,11 @@ func CreateArtist(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateArtist(app *model.AppState) http.Handler {
|
||||
func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(&artist)
|
||||
err := json.NewDecoder(r.Body).Decode(&artist)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to update artist: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
|
@ -182,6 +158,10 @@ func UpdateArtist(app *model.AppState) http.Handler {
|
|||
|
||||
err = controller.UpdateArtist(app.DB, artist)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -190,24 +170,16 @@ func UpdateArtist(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteArtist(app *model.AppState) http.Handler {
|
||||
func DeleteArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = controller.DeleteArtist(app.DB, artist.ID)
|
||||
err := controller.DeleteArtist(app.DB, artist.ID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to delete artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
|||
12
api/blog.go
12
api/blog.go
|
|
@ -66,6 +66,10 @@ func ServeBlog(app *model.AppState) http.Handler {
|
|||
|
||||
blog, err := controller.GetBlogPost(app.DB, blogID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -146,6 +150,10 @@ func UpdateBlog(app *model.AppState) http.Handler {
|
|||
|
||||
blog, err := controller.GetBlogPost(app.DB, blogID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -204,6 +212,10 @@ func UpdateBlog(app *model.AppState) http.Handler {
|
|||
|
||||
err = controller.UpdateBlogPost(app.DB, blogID, blog)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update release %s: %v\n", blogID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
|||
132
api/release.go
132
api/release.go
|
|
@ -15,20 +15,8 @@ import (
|
|||
"arimelody-web/model"
|
||||
)
|
||||
|
||||
func ServeRelease(app *model.AppState) http.Handler {
|
||||
func ServeRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// only allow authorised users to view hidden releases
|
||||
privileged := false
|
||||
if !release.Visible {
|
||||
|
|
@ -131,7 +119,7 @@ func ServeRelease(app *model.AppState) http.Handler {
|
|||
w.Header().Add("Content-Type", "application/json")
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", "\t")
|
||||
err = encoder.Encode(response)
|
||||
err := encoder.Encode(response)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -250,23 +238,35 @@ func CreateRelease(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateRelease(app *model.AppState) http.Handler {
|
||||
func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
if r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(&release)
|
||||
segments := strings.Split(r.URL.Path[1:], "/")
|
||||
|
||||
if len(segments) == 2 {
|
||||
switch segments[1] {
|
||||
case "tracks":
|
||||
UpdateReleaseTracks(app, release).ServeHTTP(w, r)
|
||||
case "credits":
|
||||
UpdateReleaseCredits(app, release).ServeHTTP(w, r)
|
||||
case "links":
|
||||
UpdateReleaseLinks(app, release).ServeHTTP(w, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(segments) > 2 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&release)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to update release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
|
@ -299,6 +299,10 @@ func UpdateRelease(app *model.AppState) http.Handler {
|
|||
|
||||
err = controller.UpdateRelease(app.DB, release)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -307,24 +311,12 @@ func UpdateRelease(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseTracks(app *model.AppState) http.Handler {
|
||||
func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var trackIDs = []string{}
|
||||
err = json.NewDecoder(r.Body).Decode(&trackIDs)
|
||||
err := json.NewDecoder(r.Body).Decode(&trackIDs)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -336,6 +328,10 @@ func UpdateReleaseTracks(app *model.AppState) http.Handler {
|
|||
http.Error(w, "Release cannot have duplicate tracks", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update tracks for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -344,29 +340,17 @@ func UpdateReleaseTracks(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseCredits(app *model.AppState) http.Handler {
|
||||
func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type creditJSON struct {
|
||||
Artist string
|
||||
Role string
|
||||
Primary bool
|
||||
}
|
||||
var data []creditJSON
|
||||
err = json.NewDecoder(r.Body).Decode(&data)
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -389,6 +373,10 @@ func UpdateReleaseCredits(app *model.AppState) http.Handler {
|
|||
http.Error(w, "Artists may only be credited once", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update credits for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -397,24 +385,12 @@ func UpdateReleaseCredits(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseLinks(app *model.AppState) http.Handler {
|
||||
func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var links = []*model.Link{}
|
||||
err = json.NewDecoder(r.Body).Decode(&links)
|
||||
err := json.NewDecoder(r.Body).Decode(&links)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -426,6 +402,10 @@ func UpdateReleaseLinks(app *model.AppState) http.Handler {
|
|||
http.Error(w, "Release cannot have duplicate link names", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update links for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -434,24 +414,16 @@ func UpdateReleaseLinks(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteRelease(app *model.AppState) http.Handler {
|
||||
func DeleteRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = controller.DeleteRelease(app.DB, release.ID)
|
||||
err := controller.DeleteRelease(app.DB, release.ID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to delete release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
|||
61
api/track.go
61
api/track.go
|
|
@ -1,13 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
)
|
||||
|
||||
type (
|
||||
|
|
@ -50,20 +50,8 @@ func ServeAllTracks(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeTrack(app *model.AppState) http.Handler {
|
||||
func ServeTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if track == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
dbReleases, err := controller.GetTrackReleases(app.DB, track.ID, false)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to pull track releases for %s from DB: %s\n", track.ID, err)
|
||||
|
|
@ -117,23 +105,16 @@ func CreateTrack(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateTrack(app *model.AppState) http.Handler {
|
||||
func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if track == nil {
|
||||
if r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(&track)
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&track)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -163,23 +144,17 @@ func UpdateTrack(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteTrack(app *model.AppState) http.Handler {
|
||||
func DeleteTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if track == nil {
|
||||
if r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = controller.DeleteTrack(app.DB, trackID)
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
err := controller.DeleteTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to delete track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
|
|||
|
|
@ -23,7 +23,9 @@ func GetAccountByID(db *sqlx.DB, id string) (*model.Account, error) {
|
|||
|
||||
err := db.Get(&account, "SELECT * FROM account WHERE id=$1", id)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +37,9 @@ func GetAccountByUsername(db *sqlx.DB, username string) (*model.Account, error)
|
|||
|
||||
err := db.Get(&account, "SELECT * FROM account WHERE username=$1", username)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +51,9 @@ func GetAccountByEmail(db *sqlx.DB, email string) (*model.Account, error) {
|
|||
|
||||
err := db.Get(&account, "SELECT * FROM account WHERE email=$1", email)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +67,9 @@ func GetAccountBySession(db *sqlx.DB, sessionToken string) (*model.Account, erro
|
|||
|
||||
err := db.Get(&account, "SELECT account.* FROM account JOIN token ON id=account WHERE token=$1", sessionToken)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
"strings"
|
||||
"arimelody-web/model"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// DATABASE
|
||||
|
|
@ -14,7 +13,6 @@ func GetArtist(db *sqlx.DB, id string) (*model.Artist, error) {
|
|||
|
||||
err := db.Get(&artist, "SELECT * FROM artist WHERE id=$1", id)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package controller
|
|||
import (
|
||||
"arimelody-web/model"
|
||||
"database/sql"
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
|
@ -23,7 +22,6 @@ func GetBlogPost(db *sqlx.DB, id string) (*model.BlogPost, error) {
|
|||
id,
|
||||
)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,9 @@ func GetInvite(db *sqlx.DB, code string) (*model.Invite, error) {
|
|||
|
||||
err := db.Get(&invite, "SELECT * FROM invite WHERE code=$1", code)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -30,7 +32,7 @@ func CreateInvite(db *sqlx.DB, length int, lifetime time.Duration) (*model.Invit
|
|||
}
|
||||
|
||||
code := []byte{}
|
||||
for range length {
|
||||
for i := 0; i < length; i++ {
|
||||
code = append(code, inviteChars[rand.Intn(len(inviteChars) - 1)])
|
||||
}
|
||||
invite.Code = string(code)
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ func GetRelease(db *sqlx.DB, id string, full bool) (*model.Release, error) {
|
|||
|
||||
err := db.Get(&release, "SELECT * FROM musicrelease WHERE id=$1", id)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -118,7 +117,9 @@ func GetLatestRelease(db *sqlx.DB) (*model.Release, error) {
|
|||
|
||||
err := db.Get(&release, "SELECT * FROM musicrelease WHERE visible=true ORDER BY release_date DESC LIMIT 1")
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -121,7 +121,9 @@ func GetTOTP(db *sqlx.DB, accountID string, name string) (*model.TOTP, error) {
|
|||
name,
|
||||
)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
"strings"
|
||||
"arimelody-web/model"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// DATABASE
|
||||
|
|
@ -12,9 +11,9 @@ import (
|
|||
func GetTrack(db *sqlx.DB, id string) (*model.Track, error) {
|
||||
var track = model.Track{}
|
||||
|
||||
err := db.Get("SELECT * FROM musictrack WHERE id=$1", id)
|
||||
stmt, _ := db.Preparex("SELECT * FROM musictrack WHERE id=$1")
|
||||
err := stmt.Get(&track, id)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") { return nil, nil }
|
||||
return nil, err
|
||||
}
|
||||
return &track, nil
|
||||
|
|
|
|||
11
log/log.go
11
log/log.go
|
|
@ -114,6 +114,17 @@ func (self *Logger) Search(levelFilters []LogLevel, typeFilters []string, conten
|
|||
conditions,
|
||||
)
|
||||
|
||||
/*
|
||||
fmt.Printf("%s (", query)
|
||||
for i, param := range params {
|
||||
fmt.Print(param)
|
||||
if i < len(params) - 1 {
|
||||
fmt.Print(", ")
|
||||
}
|
||||
}
|
||||
fmt.Print(")\n")
|
||||
*/
|
||||
|
||||
err := self.DB.Select(&logs, query, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
|
|
@ -58,6 +59,10 @@ func BlogHandler(app *model.AppState) http.Handler {
|
|||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
posts, err := controller.GetBlogPosts(app.DB, true, -1, 0)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog posts: %v\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -106,6 +111,10 @@ func ServeBlogPost(app *model.AppState, blogPostID string) http.Handler {
|
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
blog, err := controller.GetBlogPost(app.DB, blogPostID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogPostID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue