2024-08-02 21:48:26 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-09-01 03:43:32 +00:00
|
|
|
"strings"
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
"arimelody.me/arimelody.me/global"
|
|
|
|
"arimelody.me/arimelody.me/music/model"
|
|
|
|
)
|
|
|
|
|
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) {
|
2024-09-01 03:43:32 +00:00
|
|
|
var artists = []*model.Artist{}
|
|
|
|
err := global.DB.Select(&artists, "SELECT * FROM artist")
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("FATAL: Failed to serve all artists: %s\n", err)
|
2024-08-02 21:48:26 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 03:43:32 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func ServeArtist(artist model.Artist) http.Handler {
|
2024-08-02 21:48:26 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type (
|
|
|
|
creditJSON struct {
|
2024-09-01 03:43:32 +00:00
|
|
|
Release string `json:"release"`
|
|
|
|
Role string `json:"role"`
|
|
|
|
Primary bool `json:"primary"`
|
2024-08-02 21:48:26 +00:00
|
|
|
}
|
|
|
|
artistJSON struct {
|
|
|
|
model.Artist
|
|
|
|
Credits map[string]creditJSON `json:"credits"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
var credits = map[string]creditJSON{}
|
|
|
|
err := global.DB.Select(&credits, "SELECT release,role,is_primary FROM musiccredit WHERE id=$1", artist.ID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("FATAL: Failed to retrieve artist credits for %s: %s\n", artist.ID, err)
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2024-08-02 21:48:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2024-09-01 03:43:32 +00:00
|
|
|
err = json.NewEncoder(w).Encode(artistJSON{
|
|
|
|
Artist: artist,
|
|
|
|
Credits: credits,
|
|
|
|
})
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateArtist() http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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 {
|
|
|
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
_, err = global.DB.Exec(
|
|
|
|
"INSERT INTO artist (id, name, website, avatar) "+
|
|
|
|
"VALUES ($1, $2, $3, $4)",
|
|
|
|
artist.ID,
|
|
|
|
artist.Name,
|
|
|
|
artist.Website,
|
|
|
|
artist.Avatar)
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
2024-09-01 03:43:32 +00:00
|
|
|
if strings.Contains(err.Error(), "duplicate key") {
|
|
|
|
http.Error(w, fmt.Sprintf("Artist %s already exists\n", data.ID), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("FATAL: Failed to create artist %s: %s\n", artist.ID, err)
|
2024-08-02 21:48:26 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2024-08-03 22:24:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func UpdateArtist(artist model.Artist) http.Handler {
|
2024-08-03 22:24:15 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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 {
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("FATAL: Failed to update artist: %s\n", err)
|
2024-08-03 22:24:15 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
if data.ID != "" { artist.ID = data.ID }
|
|
|
|
if data.Name != nil { artist.Name = *data.Name }
|
|
|
|
if data.Website != nil { artist.Website = *data.Website }
|
|
|
|
if data.Avatar != nil { artist.Avatar = *data.Avatar }
|
2024-08-03 22:24:15 +00:00
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
_, err = global.DB.Exec(
|
|
|
|
"UPDATE artist "+
|
|
|
|
"SET name=$2, website=$3, avatar=$4 "+
|
|
|
|
"WHERE id=$1",
|
|
|
|
artist.ID,
|
|
|
|
artist.Name,
|
|
|
|
artist.Website,
|
|
|
|
artist.Avatar)
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("FATAL: Failed to update artist %s: %s\n", artist.ID, err)
|
2024-08-03 22:24:15 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func DeleteArtist(artist model.Artist) http.Handler {
|
2024-08-03 22:24:15 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-09-01 03:43:32 +00:00
|
|
|
_, err := global.DB.Exec(
|
|
|
|
"DELETE FROM artist "+
|
|
|
|
"WHERE id=$1",
|
|
|
|
artist.ID)
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
2024-09-01 03:43:32 +00:00
|
|
|
fmt.Printf("FATAL: Failed to delete artist %s: %s\n", artist.ID, err)
|
2024-08-03 22:24:15 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
})
|
|
|
|
}
|