package admin import ( "fmt" "html/template" "net/http" "path" "strings" "arimelody.me/arimelody.me/global" "arimelody.me/arimelody.me/music/model" ) type ( gatewayTrack struct { *model.Track Lyrics template.HTML Number int } gatewayRelease struct { *model.Release Tracks []gatewayTrack } ) func serveRelease() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { slices := strings.Split(r.URL.Path[1:], "/") id := slices[0] release := global.GetRelease(id) if release == nil { http.NotFound(w, r) return } if len(slices) > 1 { switch slices[1] { case "editcredits": serveEditCredits(release).ServeHTTP(w, r) return case "addcredit": serveAddCredit(release).ServeHTTP(w, r) return case "newcredit": serveNewCredit().ServeHTTP(w, r) return case "editlinks": serveEditLinks(release).ServeHTTP(w, r) return case "edittracks": serveEditTracks(release).ServeHTTP(w, r) return case "addtrack": serveAddTrack(release).ServeHTTP(w, r) return case "newtrack": serveNewTrack(release).ServeHTTP(w, r) return } http.NotFound(w, r) return } tracks := []gatewayTrack{} for i, track := range release.Tracks { tracks = append(tracks, gatewayTrack{ Track: track, Lyrics: template.HTML(strings.Replace(track.Lyrics, "\n", "
", -1)), Number: i + 1, }) } lrw := global.LoggingResponseWriter{ResponseWriter: w, Code: http.StatusOK} serveTemplate("edit-release.html", gatewayRelease{release, tracks}).ServeHTTP(&lrw, r) if lrw.Code != http.StatusOK { fmt.Printf("Error rendering admin release page for %s\n", id) return } }) } func serveEditCredits(release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") serveComponent(path.Join("credits", "editcredits.html"), release).ServeHTTP(w, r) return }) } func serveAddCredit(release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var artists = []*model.Artist{} for _, artist := range global.Artists { var exists = false for _, credit := range release.Credits { if credit.Artist == artist { exists = true break } } if !exists { artists = append(artists, artist) } } type response struct { ReleaseID string; Artists []*model.Artist } w.Header().Set("Content-Type", "text/html") serveComponent(path.Join("credits", "addcredit.html"), response{ ReleaseID: release.ID, Artists: artists, }).ServeHTTP(w, r) return }) } func serveNewCredit() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { artist := global.GetArtist(strings.Split(r.URL.Path, "/")[3]) if artist == nil { http.NotFound(w, r) return } w.Header().Set("Content-Type", "text/html") serveComponent(path.Join("credits", "newcredit.html"), artist).ServeHTTP(w, r) return }) } func serveEditLinks(release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") serveComponent(path.Join("links", "editlinks.html"), release).ServeHTTP(w, r) return }) } func serveEditTracks(release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") type Track struct { *model.Track Number int } type Release struct { *model.Release Tracks []Track } var data = Release{ release, []Track{} } for i, track := range release.Tracks { data.Tracks = append(data.Tracks, Track{track, i + 1}) } serveComponent(path.Join("tracks", "edittracks.html"), data).ServeHTTP(w, r) return }) } func serveAddTrack(release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var tracks = []*model.Track{} for _, track := range global.Tracks { var exists = false for _, t := range release.Tracks { if t == track { exists = true break } } if !exists { tracks = append(tracks, track) } } type response struct { ReleaseID string; Tracks []*model.Track } w.Header().Set("Content-Type", "text/html") serveComponent(path.Join("tracks", "addtrack.html"), response{ ReleaseID: release.ID, Tracks: tracks, }).ServeHTTP(w, r) return }) } func serveNewTrack(release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { track := global.GetTrack(strings.Split(r.URL.Path, "/")[3]) if track == nil { http.NotFound(w, r) return } type Track struct { *model.Track Number int } w.Header().Set("Content-Type", "text/html") serveComponent(path.Join("tracks", "newtrack.html"), Track{ track, len(release.Tracks) + 1, }).ServeHTTP(w, r) return }) }