2024-08-31 15:25:44 +01:00
|
|
|
package admin
|
|
|
|
|
|
|
|
|
|
import (
|
2025-09-30 19:03:35 +01:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
2024-08-31 15:25:44 +01:00
|
|
|
|
2025-09-30 19:03:35 +01:00
|
|
|
"arimelody-web/admin/templates"
|
|
|
|
|
"arimelody-web/controller"
|
|
|
|
|
"arimelody-web/model"
|
2024-08-31 15:25:44 +01:00
|
|
|
)
|
|
|
|
|
|
2025-10-21 17:15:26 +01:00
|
|
|
func serveTracks(app *model.AppState) http.Handler {
|
2024-08-31 15:25:44 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2025-10-21 18:39:38 +01:00
|
|
|
session := r.Context().Value("session").(*model.Session)
|
|
|
|
|
|
2025-10-21 17:15:26 +01:00
|
|
|
slices := strings.Split(strings.TrimPrefix(r.URL.Path, "/tracks")[1:], "/")
|
2025-10-21 18:39:38 +01:00
|
|
|
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)
|
2024-09-01 04:43:32 +01:00
|
|
|
if err != nil {
|
2025-10-21 18:39:38 +01:00
|
|
|
fmt.Printf("WARN: Failed to serve admin track page for %s: %s\n", trackID, err)
|
2024-09-01 04:43:32 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-08-31 15:25:44 +01:00
|
|
|
if track == nil {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-21 14:53:18 +00:00
|
|
|
releases, err := controller.GetTrackReleases(app.DB, track.ID, true)
|
2024-09-01 04:43:32 +01:00
|
|
|
if err != nil {
|
2025-10-21 18:39:38 +01:00
|
|
|
fmt.Printf("WARN: Failed to fetch releases for track %s: %s\n", trackID, err)
|
2024-09-01 04:43:32 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 15:08:01 +00:00
|
|
|
type TrackResponse struct {
|
2025-10-21 15:37:40 +01:00
|
|
|
adminPageData
|
2025-01-20 15:08:01 +00:00
|
|
|
Track *model.Track
|
2024-09-03 08:07:45 +01:00
|
|
|
Releases []*model.Release
|
2024-09-01 04:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-30 19:03:35 +01:00
|
|
|
err = templates.EditTrackTemplate.Execute(w, TrackResponse{
|
2025-10-21 15:37:40 +01:00
|
|
|
adminPageData: adminPageData{ Path: r.URL.Path, Session: session },
|
2025-01-20 15:08:01 +00:00
|
|
|
Track: track,
|
|
|
|
|
Releases: releases,
|
|
|
|
|
})
|
2024-08-31 15:25:44 +01:00
|
|
|
if err != nil {
|
2025-10-21 18:39:38 +01:00
|
|
|
fmt.Printf("WARN: Failed to serve admin track page for %s: %s\n", trackID, err)
|
2024-08-31 15:25:44 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|