refactored out global
. long live AppState
This commit is contained in:
parent
3d674515ce
commit
384579ee5e
24 changed files with 350 additions and 375 deletions
21
api/track.go
21
api/track.go
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
)
|
||||
|
@ -17,7 +16,7 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
func ServeAllTracks() http.Handler {
|
||||
func ServeAllTracks(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
type Track struct {
|
||||
ID string `json:"id"`
|
||||
|
@ -26,7 +25,7 @@ func ServeAllTracks() http.Handler {
|
|||
var tracks = []Track{}
|
||||
|
||||
var dbTracks = []*model.Track{}
|
||||
dbTracks, err := controller.GetAllTracks(global.DB)
|
||||
dbTracks, err := controller.GetAllTracks(app.DB)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to pull tracks from DB: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -50,9 +49,9 @@ func ServeAllTracks() http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeTrack(track *model.Track) http.Handler {
|
||||
func ServeTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
dbReleases, err := controller.GetTrackReleases(global.DB, track.ID, false)
|
||||
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)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -74,7 +73,7 @@ func ServeTrack(track *model.Track) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func CreateTrack() http.Handler {
|
||||
func CreateTrack(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.NotFound(w, r)
|
||||
|
@ -93,7 +92,7 @@ func CreateTrack() http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
id, err := controller.CreateTrack(global.DB, &track)
|
||||
id, err := controller.CreateTrack(app.DB, &track)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to create track: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -106,7 +105,7 @@ func CreateTrack() http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateTrack(track *model.Track) http.Handler {
|
||||
func UpdateTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut || r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
|
@ -124,7 +123,7 @@ func UpdateTrack(track *model.Track) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
err = controller.UpdateTrack(global.DB, track)
|
||||
err = controller.UpdateTrack(app.DB, track)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to update track %s: %s\n", track.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -141,7 +140,7 @@ func UpdateTrack(track *model.Track) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteTrack(track *model.Track) http.Handler {
|
||||
func DeleteTrack(app *model.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete || r.URL.Path == "/" {
|
||||
http.NotFound(w, r)
|
||||
|
@ -149,7 +148,7 @@ func DeleteTrack(track *model.Track) http.Handler {
|
|||
}
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
err := controller.DeleteTrack(global.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