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
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