+
diff --git a/api/api.go b/api/api.go
index 4bd7976..68a08a9 100644
--- a/api/api.go
+++ b/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) {
diff --git a/api/artist.go b/api/artist.go
index 511b866..322bc5d 100644
--- a/api/artist.go
+++ b/api/artist.go
@@ -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)
}
diff --git a/api/blog.go b/api/blog.go
index 2a32929..600064c 100644
--- a/api/blog.go
+++ b/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)
}
diff --git a/api/release.go b/api/release.go
index bbe82e5..f2bf479 100644
--- a/api/release.go
+++ b/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)
}
diff --git a/api/track.go b/api/track.go
index 7fbc2b4..ac5b83b 100644
--- a/api/track.go
+++ b/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)
diff --git a/controller/account.go b/controller/account.go
index d2653af..ab64ca5 100644
--- a/controller/account.go
+++ b/controller/account.go
@@ -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
}
diff --git a/controller/artist.go b/controller/artist.go
index f82133a..adcdbc5 100644
--- a/controller/artist.go
+++ b/controller/artist.go
@@ -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
}
diff --git a/controller/blog.go b/controller/blog.go
index 3e367d5..aea6e38 100644
--- a/controller/blog.go
+++ b/controller/blog.go
@@ -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
}
diff --git a/controller/invite.go b/controller/invite.go
index 6f12196..a7bde40 100644
--- a/controller/invite.go
+++ b/controller/invite.go
@@ -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)
diff --git a/controller/release.go b/controller/release.go
index ab25db4..b9e5ba7 100644
--- a/controller/release.go
+++ b/controller/release.go
@@ -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
}
diff --git a/controller/totp.go b/controller/totp.go
index 0c5bd13..3937459 100644
--- a/controller/totp.go
+++ b/controller/totp.go
@@ -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
}
diff --git a/controller/track.go b/controller/track.go
index 152f2c5..27f4afc 100644
--- a/controller/track.go
+++ b/controller/track.go
@@ -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
diff --git a/log/log.go b/log/log.go
index 29d99b0..88d328b 100644
--- a/log/log.go
+++ b/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
diff --git a/view/blog.go b/view/blog.go
index 1679572..b122906 100644
--- a/view/blog.go
+++ b/view/blog.go
@@ -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