refactored out global
. long live AppState
This commit is contained in:
parent
3d674515ce
commit
00ebf99cc0
23 changed files with 334 additions and 346 deletions
|
@ -10,15 +10,14 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"arimelody-web/global"
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/model"
|
||||
)
|
||||
|
||||
func ServeAllArtists() http.Handler {
|
||||
func ServeAllArtists(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artists = []*model.Artist{}
|
||||
artists, err := controller.GetAllArtists(global.DB)
|
||||
artists, err := controller.GetAllArtists(app.DB)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to serve all artists: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -35,7 +34,7 @@ func ServeAllArtists() http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func ServeArtist(artist *model.Artist) http.Handler {
|
||||
func ServeArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
type (
|
||||
creditJSON struct {
|
||||
|
@ -52,7 +51,7 @@ func ServeArtist(artist *model.Artist) http.Handler {
|
|||
}
|
||||
)
|
||||
|
||||
account, err := controller.GetAccountByRequest(global.DB, r)
|
||||
account, err := controller.GetAccountByRequest(app.DB, r)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %v\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -60,7 +59,7 @@ func ServeArtist(artist *model.Artist) http.Handler {
|
|||
}
|
||||
show_hidden_releases := account != nil
|
||||
|
||||
dbCredits, err := controller.GetArtistCredits(global.DB, artist.ID, show_hidden_releases)
|
||||
dbCredits, err := controller.GetArtistCredits(app.DB, artist.ID, show_hidden_releases)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to retrieve artist credits for %s: %v\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
@ -92,7 +91,7 @@ func ServeArtist(artist *model.Artist) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func CreateArtist() http.Handler {
|
||||
func CreateArtist(app *model.AppState) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var artist model.Artist
|
||||
err := json.NewDecoder(r.Body).Decode(&artist)
|
||||
|
@ -107,7 +106,7 @@ func CreateArtist() http.Handler {
|
|||
}
|
||||
if artist.Name == "" { artist.Name = artist.ID }
|
||||
|
||||
err = controller.CreateArtist(global.DB, &artist)
|
||||
err = controller.CreateArtist(app.DB, &artist)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "duplicate key") {
|
||||
http.Error(w, fmt.Sprintf("Artist %s already exists\n", artist.ID), http.StatusBadRequest)
|
||||
|
@ -122,7 +121,7 @@ func CreateArtist() http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func UpdateArtist(artist *model.Artist) http.Handler {
|
||||
func UpdateArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
err := json.NewDecoder(r.Body).Decode(&artist)
|
||||
if err != nil {
|
||||
|
@ -136,7 +135,7 @@ func UpdateArtist(artist *model.Artist) http.Handler {
|
|||
} else {
|
||||
if strings.Contains(artist.Avatar, ";base64,") {
|
||||
var artworkDirectory = filepath.Join("uploads", "avatar")
|
||||
filename, err := HandleImageUpload(&artist.Avatar, artworkDirectory, artist.ID)
|
||||
filename, err := HandleImageUpload(app, &artist.Avatar, artworkDirectory, artist.ID)
|
||||
|
||||
// clean up files with this ID and different extensions
|
||||
err = filepath.Walk(artworkDirectory, func(path string, info fs.FileInfo, err error) error {
|
||||
|
@ -155,7 +154,7 @@ func UpdateArtist(artist *model.Artist) http.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
err = controller.UpdateArtist(global.DB, artist)
|
||||
err = controller.UpdateArtist(app.DB, artist)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
|
@ -167,9 +166,9 @@ func UpdateArtist(artist *model.Artist) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
func DeleteArtist(artist *model.Artist) http.Handler {
|
||||
func DeleteArtist(app *model.AppState, artist *model.Artist) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
err := controller.DeleteArtist(global.DB, artist.ID)
|
||||
err := controller.DeleteArtist(app.DB, artist.ID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no rows") {
|
||||
http.NotFound(w, r)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue