+
diff --git a/api/api.go b/api/api.go
index 68a08a9..4bd7976 100644
--- a/api/api.go
+++ b/api/api.go
@@ -18,142 +18,50 @@ func Handler(app *model.AppState) http.Handler {
// ARTIST ENDPOINTS
- 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/{id}", ServeArtist(app))
+ mux.Handle("PUT /v1/artist/{id}", requireAccount(UpdateArtist(app)))
+ mux.Handle("DELETE /v1/artist/{id}", requireAccount(DeleteArtist(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)
+ 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)))
// RELEASE ENDPOINTS
- 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("GET /v1/music/{id}", ServeRelease(app))
+ mux.Handle("PUT /v1/music/{id}", requireAccount(UpdateRelease(app)))
+ mux.Handle("DELETE /v1/music/{id}", requireAccount(DeleteRelease(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)
+ 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)))
// TRACK ENDPOINTS
- 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/{id}", requireAccount(ServeTrack(app)))
+ mux.Handle("PUT /v1/track/{id}", requireAccount(UpdateTrack(app)))
+ mux.Handle("DELETE /v1/track/{id}", requireAccount(DeleteTrack(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)
+ 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)))
// 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 322bc5d..511b866 100644
--- a/api/artist.go
+++ b/api/artist.go
@@ -35,8 +35,20 @@ func ServeAllArtists(app *model.AppState) http.Handler {
})
}
-func ServeArtist(app *model.AppState, artist *model.Artist) http.Handler {
+func ServeArtist(app *model.AppState) 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"`
@@ -121,11 +133,23 @@ func CreateArtist(app *model.AppState) http.Handler {
})
}
-func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
+func UpdateArtist(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*model.Session)
- err := json.NewDecoder(r.Body).Decode(&artist)
+ 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)
if err != nil {
fmt.Printf("WARN: Failed to update artist: %s\n", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
@@ -158,10 +182,6 @@ func UpdateArtist(app *model.AppState, artist *model.Artist) 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)
}
@@ -170,16 +190,24 @@ func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
})
}
-func DeleteArtist(app *model.AppState, artist *model.Artist) http.Handler {
+func DeleteArtist(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*model.Session)
- err := controller.DeleteArtist(app.DB, artist.ID)
+ 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)
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 600064c..2a32929 100644
--- a/api/blog.go
+++ b/api/blog.go
@@ -66,10 +66,6 @@ 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
@@ -150,10 +146,6 @@ 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
@@ -212,10 +204,6 @@ 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 f2bf479..bbe82e5 100644
--- a/api/release.go
+++ b/api/release.go
@@ -15,8 +15,20 @@ import (
"arimelody-web/model"
)
-func ServeRelease(app *model.AppState, release *model.Release) http.Handler {
+func ServeRelease(app *model.AppState) 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 {
@@ -119,7 +131,7 @@ func ServeRelease(app *model.AppState, release *model.Release) 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
@@ -238,35 +250,23 @@ func CreateRelease(app *model.AppState) http.Handler {
})
}
-func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
+func UpdateRelease(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*model.Session)
- if r.URL.Path == "/" {
+ 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
}
- 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)
+ 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,10 +299,6 @@ func UpdateRelease(app *model.AppState, release *model.Release) 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)
}
@@ -311,12 +307,24 @@ func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
})
}
-func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handler {
+func UpdateReleaseTracks(app *model.AppState) 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
@@ -328,10 +336,6 @@ func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handl
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)
}
@@ -340,17 +344,29 @@ func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handl
})
}
-func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Handler {
+func UpdateReleaseCredits(app *model.AppState) 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
@@ -373,10 +389,6 @@ func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Hand
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)
}
@@ -385,12 +397,24 @@ func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Hand
})
}
-func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handler {
+func UpdateReleaseLinks(app *model.AppState) 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
@@ -402,10 +426,6 @@ func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handle
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)
}
@@ -414,16 +434,24 @@ func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handle
})
}
-func DeleteRelease(app *model.AppState, release *model.Release) http.Handler {
+func DeleteRelease(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*model.Session)
- err := controller.DeleteRelease(app.DB, release.ID)
+ 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)
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 ac5b83b..7fbc2b4 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,8 +50,20 @@ func ServeAllTracks(app *model.AppState) http.Handler {
})
}
-func ServeTrack(app *model.AppState, track *model.Track) http.Handler {
+func ServeTrack(app *model.AppState) 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)
@@ -105,16 +117,23 @@ func CreateTrack(app *model.AppState) http.Handler {
})
}
-func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
+func UpdateTrack(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.URL.Path == "/" {
+ 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 {
http.NotFound(w, r)
return
}
- session := r.Context().Value("session").(*model.Session)
-
- err := json.NewDecoder(r.Body).Decode(&track)
+ err = json.NewDecoder(r.Body).Decode(&track)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
@@ -144,17 +163,23 @@ func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
})
}
-func DeleteTrack(app *model.AppState, track *model.Track) http.Handler {
+func DeleteTrack(app *model.AppState) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- if r.URL.Path == "/" {
+ 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 {
http.NotFound(w, r)
return
}
- session := r.Context().Value("session").(*model.Session)
-
- var trackID = r.URL.Path[1:]
- err := controller.DeleteTrack(app.DB, trackID)
+ 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 ab64ca5..d2653af 100644
--- a/controller/account.go
+++ b/controller/account.go
@@ -23,9 +23,7 @@ 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
}
@@ -37,9 +35,7 @@ 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
}
@@ -51,9 +47,7 @@ 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
}
@@ -67,9 +61,7 @@ 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 adcdbc5..f82133a 100644
--- a/controller/artist.go
+++ b/controller/artist.go
@@ -1,9 +1,10 @@
package controller
import (
- "arimelody-web/model"
+ "arimelody-web/model"
+ "strings"
- "github.com/jmoiron/sqlx"
+ "github.com/jmoiron/sqlx"
)
// DATABASE
@@ -13,6 +14,7 @@ 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 aea6e38..3e367d5 100644
--- a/controller/blog.go
+++ b/controller/blog.go
@@ -3,6 +3,7 @@ package controller
import (
"arimelody-web/model"
"database/sql"
+ "strings"
"github.com/jmoiron/sqlx"
)
@@ -22,6 +23,7 @@ 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 a7bde40..6f12196 100644
--- a/controller/invite.go
+++ b/controller/invite.go
@@ -16,9 +16,7 @@ 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
}
@@ -32,7 +30,7 @@ func CreateInvite(db *sqlx.DB, length int, lifetime time.Duration) (*model.Invit
}
code := []byte{}
- for i := 0; i < length; i++ {
+ for range length {
code = append(code, inviteChars[rand.Intn(len(inviteChars) - 1)])
}
invite.Code = string(code)
diff --git a/controller/release.go b/controller/release.go
index b9e5ba7..ab25db4 100644
--- a/controller/release.go
+++ b/controller/release.go
@@ -14,6 +14,7 @@ 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
}
@@ -117,9 +118,7 @@ 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 3937459..0c5bd13 100644
--- a/controller/totp.go
+++ b/controller/totp.go
@@ -121,9 +121,7 @@ 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 27f4afc..152f2c5 100644
--- a/controller/track.go
+++ b/controller/track.go
@@ -1,9 +1,10 @@
package controller
import (
- "arimelody-web/model"
+ "arimelody-web/model"
+ "strings"
- "github.com/jmoiron/sqlx"
+ "github.com/jmoiron/sqlx"
)
// DATABASE
@@ -11,9 +12,9 @@ import (
func GetTrack(db *sqlx.DB, id string) (*model.Track, error) {
var track = model.Track{}
- stmt, _ := db.Preparex("SELECT * FROM musictrack WHERE id=$1")
- err := stmt.Get(&track, id)
+ err := db.Get("SELECT * FROM musictrack WHERE id=$1", 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 88d328b..29d99b0 100644
--- a/log/log.go
+++ b/log/log.go
@@ -114,17 +114,6 @@ 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 b122906..1679572 100644
--- a/view/blog.go
+++ b/view/blog.go
@@ -6,7 +6,6 @@ import (
"net/http"
"os"
"slices"
- "strings"
"arimelody-web/controller"
"arimelody-web/model"
@@ -59,10 +58,6 @@ 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
@@ -111,10 +106,6 @@ 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