package admin import ( "fmt" "net/http" "strings" "arimelody-web/admin/templates" "arimelody-web/controller" "arimelody-web/model" ) func serveArtists(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session) slices := strings.Split(strings.TrimPrefix(r.URL.Path, "/artists")[1:], "/") artistID := slices[0] if len(artistID) > 0 { serveArtist(app, artistID).ServeHTTP(w, r) return } artists, err := controller.GetAllArtists(app.DB) if err != nil { fmt.Printf("WARN: Failed to fetch artists: %s\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } type ArtistsResponse struct { adminPageData Artists []*model.Artist } err = templates.ArtistsTemplate.Execute(w, ArtistsResponse{ adminPageData: adminPageData{ Path: r.URL.Path, Session: session }, Artists: artists, }) if err != nil { fmt.Printf("WARN: Failed to serve admin artists page: %s\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } }) } func serveArtist(app *model.AppState, artistID string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session) artist, err := controller.GetArtist(app.DB, artistID) if err != nil { if artist == nil { http.NotFound(w, r) return } fmt.Printf("WARN: Failed to fetch artist %s: %s\n", artistID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } credits, err := controller.GetArtistCredits(app.DB, artist.ID, true) if err != nil { fmt.Printf("WARN: Failed to serve admin artist page for %s: %s\n", artistID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } type ArtistResponse struct { adminPageData Artist *model.Artist Credits []*model.Credit } err = templates.EditArtistTemplate.Execute(w, ArtistResponse{ adminPageData: adminPageData{ Path: r.URL.Path, Session: session }, Artist: artist, Credits: credits, }) if err != nil { fmt.Printf("WARN: Failed to serve admin artist page for %s: %s\n", artistID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } }) }