use servemux *properly* this time; better error handling for DB gets

This commit is contained in:
ari melody 2025-11-08 15:04:07 +00:00
parent a33e6717e0
commit b7c1d85830
Signed by: ari
GPG key ID: CF99829C92678188
16 changed files with 234 additions and 288 deletions

View file

@ -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)
}