use servemux *properly* this time; better error handling for DB gets
This commit is contained in:
parent
a33e6717e0
commit
b7c1d85830
16 changed files with 234 additions and 288 deletions
148
api/api.go
148
api/api.go
|
|
@ -18,142 +18,50 @@ func Handler(app *model.AppState) http.Handler {
|
|||
|
||||
// ARTIST ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/artist/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
mux.Handle("GET /v1/artist/{id}", ServeArtist(app))
|
||||
mux.Handle("PUT /v1/artist/{id}", requireAccount(UpdateArtist(app)))
|
||||
mux.Handle("DELETE /v1/artist/{id}", requireAccount(DeleteArtist(app)))
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/artist/{id}
|
||||
ServeArtist(app, artist).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/artist/{id} (admin)
|
||||
requireAccount(UpdateArtist(app, artist)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/artist/{id} (admin)
|
||||
requireAccount(DeleteArtist(app, artist)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
artistIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/artist
|
||||
ServeAllArtists(app).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/artist (admin)
|
||||
requireAccount(CreateArtist(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
mux.Handle("/v1/artist/", artistIndexHandler)
|
||||
mux.Handle("/v1/artist", artistIndexHandler)
|
||||
mux.Handle("GET /v1/artist/", ServeAllArtists(app))
|
||||
mux.Handle("GET /v1/artist", ServeAllArtists(app))
|
||||
mux.Handle("POST /v1/artist/", requireAccount(CreateArtist(app)))
|
||||
mux.Handle("POST /v1/artist", requireAccount(CreateArtist(app)))
|
||||
|
||||
// RELEASE ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/music/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
mux.Handle("GET /v1/music/{id}", ServeRelease(app))
|
||||
mux.Handle("PUT /v1/music/{id}", requireAccount(UpdateRelease(app)))
|
||||
mux.Handle("DELETE /v1/music/{id}", requireAccount(DeleteRelease(app)))
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/music/{id}
|
||||
ServeRelease(app, release).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/music/{id} (admin)
|
||||
requireAccount(UpdateRelease(app, release)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/music/{id} (admin)
|
||||
requireAccount(DeleteRelease(app, release)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
musicIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/music
|
||||
ServeCatalog(app).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/music (admin)
|
||||
requireAccount(CreateRelease(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
mux.Handle("/v1/music/", musicIndexHandler)
|
||||
mux.Handle("/v1/music", musicIndexHandler)
|
||||
mux.Handle("PUT /v1/music/{id}/tracks", requireAccount(UpdateReleaseTracks(app)))
|
||||
mux.Handle("PUT /v1/music/{id}/credits", requireAccount(UpdateReleaseCredits(app)))
|
||||
mux.Handle("PUT /v1/music/{id}/links", requireAccount(UpdateReleaseLinks(app)))
|
||||
|
||||
mux.Handle("GET /v1/music/", ServeCatalog(app))
|
||||
mux.Handle("GET /v1/music", ServeCatalog(app))
|
||||
mux.Handle("POST /v1/music/", requireAccount(CreateRelease(app)))
|
||||
mux.Handle("POST /v1/music", requireAccount(CreateRelease(app)))
|
||||
|
||||
// TRACK ENDPOINTS
|
||||
|
||||
mux.Handle("/v1/track/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
mux.Handle("GET /v1/track/{id}", requireAccount(ServeTrack(app)))
|
||||
mux.Handle("PUT /v1/track/{id}", requireAccount(UpdateTrack(app)))
|
||||
mux.Handle("DELETE /v1/track/{id}", requireAccount(DeleteTrack(app)))
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/track/{id} (admin)
|
||||
requireAccount(ServeTrack(app, track)).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/track/{id} (admin)
|
||||
requireAccount(UpdateTrack(app, track)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/track/{id} (admin)
|
||||
requireAccount(DeleteTrack(app, track)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
trackIndexHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/track (admin)
|
||||
requireAccount(ServeAllTracks(app)).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/track (admin)
|
||||
requireAccount(CreateTrack(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
mux.Handle("/v1/track/", trackIndexHandler)
|
||||
mux.Handle("/v1/track", trackIndexHandler)
|
||||
mux.Handle("GET /v1/track/", requireAccount(ServeAllTracks(app)))
|
||||
mux.Handle("GET /v1/track", requireAccount(ServeAllTracks(app)))
|
||||
mux.Handle("POST /v1/track/", requireAccount(CreateTrack(app)))
|
||||
mux.Handle("POST /v1/track", requireAccount(CreateTrack(app)))
|
||||
|
||||
// BLOG ENDPOINTS
|
||||
|
||||
mux.Handle("GET /v1/blog/{id}", ServeBlog(app))
|
||||
mux.Handle("PUT /v1/blog/{id}", requireAccount(UpdateBlog(app)))
|
||||
mux.Handle("DELETE /v1/blog/{id}", requireAccount(DeleteBlog(app)))
|
||||
|
||||
mux.Handle("GET /v1/blog/", ServeAllBlogs(app))
|
||||
mux.Handle("GET /v1/blog", ServeAllBlogs(app))
|
||||
mux.Handle("POST /v1/blog/", requireAccount(CreateBlog(app)))
|
||||
mux.Handle("POST /v1/blog", requireAccount(CreateBlog(app)))
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -35,8 +35,20 @@ func ServeAllArtists(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
func ServeArtist(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type (
|
||||
creditJSON struct {
|
||||
ID string `json:"id"`
|
||||
|
|
@ -121,11 +133,23 @@ func CreateArtist(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
func UpdateArtist(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&artist)
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.NewDecoder(r.Body).Decode(&artist)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to update artist: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
|
@ -158,10 +182,6 @@ func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
|||
|
||||
err = controller.UpdateArtist(app.DB, artist)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -170,16 +190,24 @@ func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
func DeleteArtist(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
err := controller.DeleteArtist(app.DB, artist.ID)
|
||||
var artistID = r.PathValue("id")
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = controller.DeleteArtist(app.DB, artist.ID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to delete artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
|||
12
api/blog.go
12
api/blog.go
|
|
@ -66,10 +66,6 @@ func ServeBlog(app *model.AppState) http.Handler {
|
|||
|
||||
blog, err := controller.GetBlogPost(app.DB, blogID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -150,10 +146,6 @@ func UpdateBlog(app *model.AppState) http.Handler {
|
|||
|
||||
blog, err := controller.GetBlogPost(app.DB, blogID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch blog post %s: %v\n", blogID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -212,10 +204,6 @@ func UpdateBlog(app *model.AppState) http.Handler {
|
|||
|
||||
err = controller.UpdateBlogPost(app.DB, blogID, blog)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update release %s: %v\n", blogID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
|||
132
api/release.go
132
api/release.go
|
|
@ -15,8 +15,20 @@ import (
|
|||
"arimelody-web/model"
|
||||
)
|
||||
|
||||
func ServeRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
func ServeRelease(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// only allow authorised users to view hidden releases
|
||||
privileged := false
|
||||
if !release.Visible {
|
||||
|
|
@ -119,7 +131,7 @@ func ServeRelease(app *model.AppState, release *model.Release) http.Handler {
|
|||
w.Header().Add("Content-Type", "application/json")
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", "\t")
|
||||
err := encoder.Encode(response)
|
||||
err = encoder.Encode(response)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -238,35 +250,23 @@ func CreateRelease(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateRelease(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
segments := strings.Split(r.URL.Path[1:], "/")
|
||||
|
||||
if len(segments) == 2 {
|
||||
switch segments[1] {
|
||||
case "tracks":
|
||||
UpdateReleaseTracks(app, release).ServeHTTP(w, r)
|
||||
case "credits":
|
||||
UpdateReleaseCredits(app, release).ServeHTTP(w, r)
|
||||
case "links":
|
||||
UpdateReleaseLinks(app, release).ServeHTTP(w, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(segments) > 2 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&release)
|
||||
err = json.NewDecoder(r.Body).Decode(&release)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to update release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
|
|
@ -299,10 +299,6 @@ func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
|
|||
|
||||
err = controller.UpdateRelease(app.DB, release)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -311,12 +307,24 @@ func UpdateRelease(app *model.AppState, release *model.Release) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateReleaseTracks(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var trackIDs = []string{}
|
||||
err := json.NewDecoder(r.Body).Decode(&trackIDs)
|
||||
err = json.NewDecoder(r.Body).Decode(&trackIDs)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -328,10 +336,6 @@ func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handl
|
|||
http.Error(w, "Release cannot have duplicate tracks", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update tracks for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -340,17 +344,29 @@ func UpdateReleaseTracks(app *model.AppState, release *model.Release) http.Handl
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateReleaseCredits(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type creditJSON struct {
|
||||
Artist string
|
||||
Role string
|
||||
Primary bool
|
||||
}
|
||||
var data []creditJSON
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
err = json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -373,10 +389,6 @@ func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Hand
|
|||
http.Error(w, "Artists may only be credited once", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update credits for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -385,12 +397,24 @@ func UpdateReleaseCredits(app *model.AppState, release *model.Release) http.Hand
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handler {
|
||||
func UpdateReleaseLinks(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var links = []*model.Link{}
|
||||
err := json.NewDecoder(r.Body).Decode(&links)
|
||||
err = json.NewDecoder(r.Body).Decode(&links)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -402,10 +426,6 @@ func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handle
|
|||
http.Error(w, "Release cannot have duplicate link names", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update links for %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
@ -414,16 +434,24 @@ func UpdateReleaseLinks(app *model.AppState, release *model.Release) http.Handle
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteRelease(app *model.AppState, release *model.Release) http.Handler {
|
||||
func DeleteRelease(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
err := controller.DeleteRelease(app.DB, release.ID)
|
||||
var releaseID = r.PathValue("id")
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if release == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
err = controller.DeleteRelease(app.DB, release.ID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to delete release %s: %s\n", release.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
|||
61
api/track.go
61
api/track.go
|
|
@ -1,13 +1,13 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/log"
|
||||
"arimelody-web/model"
|
||||
)
|
||||
|
||||
type (
|
||||
|
|
@ -50,8 +50,20 @@ func ServeAllTracks(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
func ServeTrack(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if track == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
dbReleases, err := controller.GetTrackReleases(app.DB, track.ID, false)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to pull track releases for %s from DB: %s\n", track.ID, err)
|
||||
|
|
@ -105,16 +117,23 @@ func CreateTrack(app *model.AppState) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
func UpdateTrack(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if track == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&track)
|
||||
err = json.NewDecoder(r.Body).Decode(&track)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -144,17 +163,23 @@ func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
func DeleteTrack(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var trackID = r.PathValue("id")
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if track == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
err := controller.DeleteTrack(app.DB, trackID)
|
||||
err = controller.DeleteTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to delete track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue