86 lines
2.8 KiB
Go
86 lines
2.8 KiB
Go
package admin
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"arimelody-web/admin/templates"
|
|
"arimelody-web/model"
|
|
"arimelody-web/model/app"
|
|
)
|
|
|
|
func serveTracks(app *app.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 := app.MusicService.GetAllTracks()
|
|
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 *app.AppState, trackID string) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*model.Session)
|
|
|
|
track, err := app.MusicService.GetTrackByID(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 := app.MusicService.GetTrackReleases(trackID)
|
|
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)
|
|
}
|
|
})
|
|
}
|