2024-08-02 21:48:26 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
type artistJSON struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name *string `json:"name"`
|
|
|
|
Website *string `json:"website"`
|
|
|
|
Avatar *string `json:"avatar"`
|
|
|
|
}
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
func ServeAllArtists() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2024-08-03 22:24:15 +00:00
|
|
|
err := json.NewEncoder(w).Encode(global.Artists)
|
2024-08-02 21:48:26 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
)
|
2024-08-03 22:24:15 +00:00
|
|
|
var artist = artistJSON{}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
artist.ID = r.URL.Path[1:]
|
|
|
|
var a = global.GetArtist(artist.ID)
|
|
|
|
if a == nil {
|
2024-08-02 21:48:26 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-08-03 22:24:15 +00:00
|
|
|
artist.Name = a.Name
|
|
|
|
artist.Website = a.Website
|
|
|
|
artist.Credits = make(map[string]creditJSON)
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
for _, release := range global.Releases {
|
|
|
|
for _, credit := range release.Credits {
|
2024-08-03 22:24:15 +00:00
|
|
|
if credit.Artist.ID != artist.ID {
|
2024-08-02 21:48:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-08-03 22:24:15 +00:00
|
|
|
artist.Credits[release.ID] = creditJSON{
|
2024-08-02 21:48:26 +00:00
|
|
|
Role: credit.Role,
|
|
|
|
Primary: credit.Primary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2024-08-03 22:24:15 +00:00
|
|
|
err := json.NewEncoder(w).Encode(artist)
|
2024-08-02 21:48:26 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
var data artistJSON
|
2024-08-02 21:48:26 +00:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
if err != nil {
|
2024-08-02 23:27:30 +00:00
|
|
|
fmt.Printf("Failed to create artist: %s\n", err)
|
2024-08-02 21:48:26 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-02 23:27:30 +00:00
|
|
|
if data.ID == "" {
|
2024-08-03 14:02:01 +00:00
|
|
|
http.Error(w, "Artist ID cannot be blank\n", http.StatusBadRequest)
|
2024-08-02 23:27:30 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-05 00:23:17 +00:00
|
|
|
if data.Name == nil || *data.Name == "" {
|
2024-08-03 14:02:01 +00:00
|
|
|
http.Error(w, "Artist name cannot be blank\n", http.StatusBadRequest)
|
2024-08-02 23:27:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
if global.GetArtist(data.ID) != nil {
|
2024-08-03 14:02:01 +00:00
|
|
|
http.Error(w, fmt.Sprintf("Artist %s already exists\n", data.ID), http.StatusBadRequest)
|
2024-08-02 21:48:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var artist = model.Artist{
|
|
|
|
ID: data.ID,
|
2024-08-05 00:23:17 +00:00
|
|
|
Name: *data.Name,
|
|
|
|
Website: *data.Website,
|
|
|
|
Avatar: *data.Avatar,
|
2024-08-02 21:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-02 23:27:30 +00:00
|
|
|
global.Artists = append(global.Artists, &artist)
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
err = json.NewEncoder(w).Encode(artist)
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateArtist() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPut {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
var data artistJSON
|
2024-08-03 22:24:15 +00:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to update artist: %s\n", err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var artistID = r.URL.Path[1:]
|
|
|
|
var artist = global.GetArtist(artistID)
|
|
|
|
if artist == nil {
|
|
|
|
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", artistID), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
var update = *artist
|
2024-08-03 22:24:15 +00:00
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
if data.ID != "" { update.ID = data.ID }
|
|
|
|
if data.Name != nil { update.Name = *data.Name }
|
|
|
|
if data.Website != nil { update.Website = *data.Website }
|
|
|
|
if data.Avatar != nil { update.Avatar = *data.Avatar }
|
2024-08-03 22:24:15 +00:00
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
err = controller.UpdateArtistDB(global.DB, &update)
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to update artist %s: %s\n", artist.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-05 00:23:17 +00:00
|
|
|
artist.ID = update.ID
|
|
|
|
artist.Name = update.Name
|
|
|
|
artist.Website = update.Website
|
|
|
|
artist.Avatar = update.Avatar
|
2024-08-03 22:24:15 +00:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
err = json.NewEncoder(w).Encode(artist)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteArtist() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodDelete {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var artistID = r.URL.Path[1:]
|
|
|
|
var artist = global.GetArtist(artistID)
|
|
|
|
if artist == nil {
|
|
|
|
http.Error(w, fmt.Sprintf("Artist %s does not exist\n", artistID), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := controller.DeleteArtistDB(global.DB, artist)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to delete artist %s: %s\n", artist.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
global.Artists = func () []*model.Artist {
|
|
|
|
var artists = []*model.Artist{}
|
|
|
|
for _, a := range global.Artists {
|
|
|
|
if a.ID == artist.ID { continue }
|
|
|
|
artists = append(artists, a)
|
|
|
|
}
|
|
|
|
return artists
|
|
|
|
}()
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(fmt.Sprintf("Artist %s has been deleted\n", artist.ID)))
|
2024-08-02 21:48:26 +00:00
|
|
|
})
|
|
|
|
}
|