arimelody-web/admin/artisthttp.go

87 lines
2.9 KiB
Go
Raw Normal View History

2024-09-03 08:07:45 +01:00
package admin
import (
2025-09-30 19:03:35 +01:00
"fmt"
"net/http"
"strings"
2024-09-03 08:07:45 +01:00
2025-09-30 19:03:35 +01:00
"arimelody-web/admin/templates"
"arimelody-web/controller"
"arimelody-web/model"
2024-09-03 08:07:45 +01:00
)
func serveArtists(app *model.AppState) http.Handler {
2024-09-03 08:07:45 +01:00
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)
2024-09-03 08:07:45 +01:00
if err != nil {
if artist == nil {
http.NotFound(w, r)
return
}
fmt.Printf("WARN: Failed to fetch artist %s: %s\n", artistID, err)
2024-09-03 08:07:45 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
credits, err := controller.GetArtistCredits(app.DB, artist.ID, true)
2024-09-03 08:07:45 +01:00
if err != nil {
fmt.Printf("WARN: Failed to serve admin artist page for %s: %s\n", artistID, err)
2024-09-03 08:07:45 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
type ArtistResponse struct {
adminPageData
Artist *model.Artist
Credits []*model.Credit
2024-09-03 08:07:45 +01:00
}
2025-09-30 19:03:35 +01:00
err = templates.EditArtistTemplate.Execute(w, ArtistResponse{
adminPageData: adminPageData{ Path: r.URL.Path, Session: session },
Artist: artist,
Credits: credits,
})
2024-09-03 08:07:45 +01:00
if err != nil {
fmt.Printf("WARN: Failed to serve admin artist page for %s: %s\n", artistID, err)
2024-09-03 08:07:45 +01:00
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
}