2024-08-05 00:23:17 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
"arimelody-web/global"
|
2024-11-01 19:33:26 +00:00
|
|
|
"arimelody-web/controller"
|
|
|
|
"arimelody-web/model"
|
2024-08-05 00:23:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func serveRelease() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-08-23 22:08:28 +00:00
|
|
|
slices := strings.Split(r.URL.Path[1:], "/")
|
2024-09-01 03:43:32 +00:00
|
|
|
releaseID := slices[0]
|
2024-09-01 23:15:23 +00:00
|
|
|
|
2024-11-01 19:33:26 +00:00
|
|
|
release, err := controller.GetRelease(global.DB, releaseID, true)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
2024-09-01 23:15:23 +00:00
|
|
|
if strings.Contains(err.Error(), "no rows") {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-09-03 07:07:45 +00:00
|
|
|
fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", releaseID, err)
|
2024-09-01 03:43:32 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-08-05 00:23:17 +00:00
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
authorised := GetSession(r) != nil
|
|
|
|
if !authorised && !release.Visible {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-23 22:08:28 +00:00
|
|
|
if len(slices) > 1 {
|
|
|
|
switch slices[1] {
|
|
|
|
case "editcredits":
|
2024-09-01 23:15:23 +00:00
|
|
|
serveEditCredits(release).ServeHTTP(w, r)
|
2024-08-23 22:08:28 +00:00
|
|
|
return
|
|
|
|
case "addcredit":
|
2024-09-01 23:15:23 +00:00
|
|
|
serveAddCredit(release).ServeHTTP(w, r)
|
2024-08-23 22:08:28 +00:00
|
|
|
return
|
|
|
|
case "newcredit":
|
|
|
|
serveNewCredit().ServeHTTP(w, r)
|
|
|
|
return
|
2024-08-31 00:30:30 +00:00
|
|
|
case "editlinks":
|
2024-09-01 23:15:23 +00:00
|
|
|
serveEditLinks(release).ServeHTTP(w, r)
|
2024-08-31 00:30:30 +00:00
|
|
|
return
|
|
|
|
case "edittracks":
|
2024-09-01 23:15:23 +00:00
|
|
|
serveEditTracks(release).ServeHTTP(w, r)
|
2024-08-31 00:30:30 +00:00
|
|
|
return
|
|
|
|
case "addtrack":
|
2024-09-01 23:15:23 +00:00
|
|
|
serveAddTrack(release).ServeHTTP(w, r)
|
2024-08-31 00:30:30 +00:00
|
|
|
return
|
|
|
|
case "newtrack":
|
2024-09-01 03:43:32 +00:00
|
|
|
serveNewTrack().ServeHTTP(w, r)
|
2024-08-31 00:30:30 +00:00
|
|
|
return
|
2024-08-23 22:08:28 +00:00
|
|
|
}
|
2024-08-05 00:23:17 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-01 23:15:23 +00:00
|
|
|
err = pages["release"].Execute(w, release)
|
2024-08-31 14:25:44 +00:00
|
|
|
if err != nil {
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("Error rendering admin release page for %s: %s\n", release.ID, err)
|
2024-08-31 14:25:44 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2024-08-05 00:23:17 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-08-23 22:08:28 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func serveEditCredits(release *model.Release) http.Handler {
|
2024-08-23 22:08:28 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-08-31 14:25:44 +00:00
|
|
|
err := components["editcredits"].Execute(w, release)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering edit credits component for %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-23 22:08:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func serveAddCredit(release *model.Release) http.Handler {
|
2024-08-23 22:08:28 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-11-01 19:33:26 +00:00
|
|
|
artists, err := controller.GetArtistsNotOnRelease(global.DB, release.ID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to pull artists not on %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2024-08-23 22:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
ReleaseID string;
|
|
|
|
Artists []*model.Artist
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-09-01 03:43:32 +00:00
|
|
|
err = components["addcredit"].Execute(w, response{
|
2024-08-23 22:08:28 +00:00
|
|
|
ReleaseID: release.ID,
|
|
|
|
Artists: artists,
|
2024-08-31 14:25:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering add credits component for %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-23 22:08:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveNewCredit() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-09-01 03:43:32 +00:00
|
|
|
artistID := strings.Split(r.URL.Path, "/")[3]
|
2024-11-01 19:33:26 +00:00
|
|
|
artist, err := controller.GetArtist(global.DB, artistID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to pull artists %s: %s\n", artistID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-08-23 22:08:28 +00:00
|
|
|
if artist == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-09-01 03:43:32 +00:00
|
|
|
err = components["newcredit"].Execute(w, artist)
|
2024-08-31 14:25:44 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering new credit component for %s: %s\n", artist.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-23 22:08:28 +00:00
|
|
|
})
|
|
|
|
}
|
2024-08-31 00:30:30 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func serveEditLinks(release *model.Release) http.Handler {
|
2024-08-31 00:30:30 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-08-31 14:25:44 +00:00
|
|
|
err := components["editlinks"].Execute(w, release)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering edit links component for %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-31 00:30:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func serveEditTracks(release *model.Release) http.Handler {
|
2024-08-31 00:30:30 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-09-01 03:43:32 +00:00
|
|
|
err := components["edittracks"].Execute(w, release)
|
2024-08-31 14:25:44 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering edit tracks component for %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-31 00:30:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func serveAddTrack(release *model.Release) http.Handler {
|
2024-08-31 00:30:30 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-11-01 19:33:26 +00:00
|
|
|
tracks, err := controller.GetTracksNotOnRelease(global.DB, release.ID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to pull tracks not on %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
2024-08-31 00:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
ReleaseID string;
|
|
|
|
Tracks []*model.Track
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-09-01 03:43:32 +00:00
|
|
|
err = components["addtrack"].Execute(w, response{
|
2024-08-31 00:30:30 +00:00
|
|
|
ReleaseID: release.ID,
|
|
|
|
Tracks: tracks,
|
2024-08-31 14:25:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering add tracks component for %s: %s\n", release.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-31 00:30:30 +00:00
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func serveNewTrack() http.Handler {
|
2024-08-31 00:30:30 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-09-01 03:43:32 +00:00
|
|
|
trackID := strings.Split(r.URL.Path, "/")[3]
|
2024-11-01 19:33:26 +00:00
|
|
|
track, err := controller.GetTrack(global.DB, trackID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering new track component for %s: %s\n", trackID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-08-31 00:30:30 +00:00
|
|
|
if track == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-09-01 03:43:32 +00:00
|
|
|
err = components["newtrack"].Execute(w, track)
|
2024-08-31 14:25:44 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error rendering new track component for %s: %s\n", track.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-31 00:30:30 +00:00
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|