turns out rewriting all of your database code takes a while

This commit is contained in:
ari melody 2024-09-01 04:43:32 +01:00
parent 1998a36d6d
commit 965d6f5c3e
30 changed files with 947 additions and 1036 deletions

View file

@ -4,10 +4,10 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"arimelody.me/arimelody.me/global"
"arimelody.me/arimelody.me/music/model"
controller "arimelody.me/arimelody.me/music/controller"
)
type artistJSON struct {
@ -19,76 +19,60 @@ type artistJSON struct {
func ServeAllArtists() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(global.Artists)
var artists = []*model.Artist{}
err := global.DB.Select(&artists, "SELECT * FROM artist")
if err != nil {
fmt.Printf("FATAL: Failed to serve all artists: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
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)
}
})
}
func ServeArtist() http.Handler {
func ServeArtist(artist model.Artist) 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"`
Release string `json:"release"`
Role string `json:"role"`
Primary bool `json:"primary"`
}
artistJSON struct {
model.Artist
Credits map[string]creditJSON `json:"credits"`
}
)
var artist = artistJSON{}
artist.ID = r.URL.Path[1:]
var a = global.GetArtist(artist.ID)
if a == nil {
http.NotFound(w, r)
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)
return
}
artist.Name = a.Name
artist.Website = a.Website
artist.Credits = make(map[string]creditJSON)
for _, release := range global.Releases {
for _, credit := range release.Credits {
if credit.Artist.ID != artist.ID {
continue
}
artist.Credits[release.ID] = creditJSON{
Role: credit.Role,
Primary: credit.Primary,
}
}
}
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(artist)
err = json.NewEncoder(w).Encode(artistJSON{
Artist: artist,
Credits: credits,
})
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 artistJSON
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
}
@ -102,11 +86,6 @@ func CreateArtist() http.Handler {
return
}
if global.GetArtist(data.ID) != nil {
http.Error(w, fmt.Sprintf("Artist %s already exists\n", data.ID), http.StatusBadRequest)
return
}
var artist = model.Artist{
ID: data.ID,
Name: *data.Name,
@ -114,116 +93,66 @@ func CreateArtist() http.Handler {
Avatar: *data.Avatar,
}
err = controller.CreateArtistDB(global.DB, &artist)
_, err = global.DB.Exec(
"INSERT INTO artist (id, name, website, avatar) "+
"VALUES ($1, $2, $3, $4)",
artist.ID,
artist.Name,
artist.Website,
artist.Avatar)
if err != nil {
fmt.Printf("Failed to create artist %s: %s\n", artist.ID, err)
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)
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)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
})
}
func UpdateArtist() http.Handler {
func UpdateArtist(artist model.Artist) 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
}
var data artistJSON
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
fmt.Printf("Failed to update artist: %s\n", err)
fmt.Printf("FATAL: 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
}
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 }
var update = *artist
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 }
err = controller.UpdateArtistDB(global.DB, &update)
_, err = global.DB.Exec(
"UPDATE artist "+
"SET name=$2, website=$3, avatar=$4 "+
"WHERE id=$1",
artist.ID,
artist.Name,
artist.Website,
artist.Avatar)
if err != nil {
fmt.Printf("Failed to update artist %s: %s\n", artist.ID, err)
fmt.Printf("FATAL: Failed to update artist %s: %s\n", artist.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
artist.ID = update.ID
artist.Name = update.Name
artist.Website = update.Website
artist.Avatar = update.Avatar
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 {
func DeleteArtist(artist model.Artist) 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)
_, err := global.DB.Exec(
"DELETE FROM artist "+
"WHERE id=$1",
artist.ID)
if err != nil {
fmt.Printf("Failed to delete artist %s: %s\n", artist.ID, err)
fmt.Printf("FATAL: 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)))
})
}