HOLY REFACTOR GOOD GRIEF (also finally started some CRUD work)
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
1c310c9101
commit
442889340c
80 changed files with 1571 additions and 1330 deletions
130
api/artist.go
Normal file
130
api/artist.go
Normal file
|
@ -0,0 +1,130 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody.me/arimelody.me/global"
|
||||
"arimelody.me/arimelody.me/music/model"
|
||||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
)
|
||||
|
||||
func ServeAllArtists() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
type (
|
||||
creditJSON struct {
|
||||
Role string `json:"role"`
|
||||
Primary bool `json:"primary"`
|
||||
}
|
||||
)
|
||||
|
||||
var artists = []model.Artist{}
|
||||
for _, artist := range global.Artists {
|
||||
artists = append(artists, model.Artist{
|
||||
ID: artist.ID,
|
||||
Name: artist.Name,
|
||||
Website: artist.Website,
|
||||
Avatar: artist.Avatar,
|
||||
})
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(artists)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ServeArtist() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
ServeAllArtists().ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
type (
|
||||
creditJSON struct {
|
||||
Role string `json:"role"`
|
||||
Primary bool `json:"primary"`
|
||||
}
|
||||
artistJSON struct {
|
||||
model.Artist
|
||||
Credits map[string]creditJSON `json:"credits"`
|
||||
}
|
||||
)
|
||||
var res = artistJSON{}
|
||||
|
||||
res.ID = r.URL.Path[1:]
|
||||
var artist = global.GetArtist(res.ID)
|
||||
if artist == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
res.Name = artist.Name
|
||||
res.Website = artist.Website
|
||||
res.Credits = make(map[string]creditJSON)
|
||||
|
||||
for _, release := range global.Releases {
|
||||
for _, credit := range release.Credits {
|
||||
if credit.Artist.ID != res.ID {
|
||||
continue
|
||||
}
|
||||
res.Credits[release.ID] = creditJSON{
|
||||
Role: credit.Role,
|
||||
Primary: credit.Primary,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(res)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func CreateArtist() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var data model.Artist
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if global.GetArtist(data.ID) != nil {
|
||||
http.Error(w, fmt.Sprintf("Artist %s already exists", data.ID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var artist = model.Artist{
|
||||
ID: data.ID,
|
||||
Name: data.Name,
|
||||
Website: data.Website,
|
||||
Avatar: data.Avatar,
|
||||
}
|
||||
|
||||
global.Artists = append(global.Artists, artist)
|
||||
|
||||
err = controller.CreateArtistDB(global.DB, &artist)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create artist %s: %s\n", artist.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
err = json.NewEncoder(w).Encode(artist)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue