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

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