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 { fmt.Printf("Failed to create artist: %s\n", err) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } if data.ID == "" { http.Error(w, "Artist ID cannot be blank", http.StatusBadRequest) return } if data.Name == "" { http.Error(w, "Artist name cannot be blank", 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, } 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 } global.Artists = append(global.Artists, &artist) w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) err = json.NewEncoder(w).Encode(artist) }) }