refactored out global
. long live AppState
This commit is contained in:
parent
3d674515ce
commit
384579ee5e
24 changed files with 350 additions and 375 deletions
41
api/api.go
41
api/api.go
|
@ -7,11 +7,10 @@ import (
|
|||
|
||||
"arimelody-web/admin"
|
||||
"arimelody-web/controller"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"arimelody-web/model"
|
||||
)
|
||||
|
||||
func Handler(db *sqlx.DB) http.Handler {
|
||||
func Handler(app *model.AppState) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// ACCOUNT ENDPOINTS
|
||||
|
@ -32,7 +31,7 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
|
||||
mux.Handle("/v1/artist/", http.StripPrefix("/v1/artist", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artistID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
artist, err := controller.GetArtist(db, artistID)
|
||||
artist, err := controller.GetArtist(app.DB, artistID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
|
@ -46,13 +45,13 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/artist/{id}
|
||||
ServeArtist(artist).ServeHTTP(w, r)
|
||||
ServeArtist(app, artist).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/artist/{id} (admin)
|
||||
admin.RequireAccount(db, UpdateArtist(artist)).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, UpdateArtist(app, artist)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/artist/{id} (admin)
|
||||
admin.RequireAccount(db, DeleteArtist(artist)).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, DeleteArtist(app, artist)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
@ -61,10 +60,10 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/artist
|
||||
ServeAllArtists().ServeHTTP(w, r)
|
||||
ServeAllArtists(app).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/artist (admin)
|
||||
admin.RequireAccount(db, CreateArtist()).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, CreateArtist(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
@ -74,7 +73,7 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
|
||||
mux.Handle("/v1/music/", http.StripPrefix("/v1/music", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var releaseID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
release, err := controller.GetRelease(db, releaseID, true)
|
||||
release, err := controller.GetRelease(app.DB, releaseID, true)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
|
@ -88,13 +87,13 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/music/{id}
|
||||
ServeRelease(release).ServeHTTP(w, r)
|
||||
ServeRelease(app, release).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/music/{id} (admin)
|
||||
admin.RequireAccount(db, UpdateRelease(release)).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, UpdateRelease(app, release)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/music/{id} (admin)
|
||||
admin.RequireAccount(db, DeleteRelease(release)).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, DeleteRelease(app, release)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
@ -103,10 +102,10 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/music
|
||||
ServeCatalog().ServeHTTP(w, r)
|
||||
ServeCatalog(app).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/music (admin)
|
||||
admin.RequireAccount(db, CreateRelease()).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, CreateRelease(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
@ -116,7 +115,7 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
|
||||
mux.Handle("/v1/track/", http.StripPrefix("/v1/track", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var trackID = strings.Split(r.URL.Path[1:], "/")[0]
|
||||
track, err := controller.GetTrack(db, trackID)
|
||||
track, err := controller.GetTrack(app.DB, trackID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
|
@ -130,13 +129,13 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/track/{id} (admin)
|
||||
admin.RequireAccount(db, ServeTrack(track)).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, ServeTrack(app, track)).ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
// PUT /api/v1/track/{id} (admin)
|
||||
admin.RequireAccount(db, UpdateTrack(track)).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, UpdateTrack(app, track)).ServeHTTP(w, r)
|
||||
case http.MethodDelete:
|
||||
// DELETE /api/v1/track/{id} (admin)
|
||||
admin.RequireAccount(db, DeleteTrack(track)).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, DeleteTrack(app, track)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
@ -145,10 +144,10 @@ func Handler(db *sqlx.DB) http.Handler {
|
|||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
// GET /api/v1/track (admin)
|
||||
admin.RequireAccount(db, ServeAllTracks()).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, ServeAllTracks(app)).ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
// POST /api/v1/track (admin)
|
||||
admin.RequireAccount(db, CreateTrack()).ServeHTTP(w, r)
|
||||
admin.RequireAccount(app, CreateTrack(app)).ServeHTTP(w, r)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue