Something went wrong. Try again.
home to your local SPACEGIRL 💫 arimelody.space
Something went wrong. Try again.
2.9 kB · 86 lines
Go
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687package admin
import ( "fmt" "net/http" "strings"
"arimelody-web/admin/templates" "arimelody-web/controller" "arimelody-web/model")
func serveTracks(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, "/tracks")[1:], "/") trackID := slices[0]
if len(trackID) > 0 { serveTrack(app, trackID).ServeHTTP(w, r) return }
tracks, err := controller.GetAllTracks(app.DB) if err != nil { fmt.Printf("WARN: Failed to fetch tracks: %s\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return }
type TracksResponse struct { adminPageData Tracks []*model.Track }
err = templates.TracksTemplate.Execute(w, TracksResponse{ adminPageData: adminPageData{ Path: r.URL.Path, Session: session }, Tracks: tracks, }) if err != nil { fmt.Printf("WARN: Failed to serve admin tracks page: %s\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } })}
func serveTrack(app *model.AppState, trackID string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session := r.Context().Value("session").(*model.Session)
track, err := controller.GetTrack(app.DB, trackID) if err != nil { fmt.Printf("WARN: Failed to serve admin track page for %s: %s\n", trackID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } if track == nil { http.NotFound(w, r) return }
releases, err := controller.GetTrackReleases(app.DB, track.ID, true) if err != nil { fmt.Printf("WARN: Failed to fetch releases for track %s: %s\n", trackID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return }
type TrackResponse struct { adminPageData Track *model.Track Releases []*model.Release }
err = templates.EditTrackTemplate.Execute(w, TrackResponse{ adminPageData: adminPageData{ Path: r.URL.Path, Session: session }, Track: track, Releases: releases, }) if err != nil { fmt.Printf("WARN: Failed to serve admin track page for %s: %s\n", trackID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } })}