well i guess i can POST releases now!

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-08-01 03:54:15 +01:00
parent 10f5f51e76
commit 151b2d8fd9
13 changed files with 417 additions and 203 deletions

View file

@ -1,15 +1,16 @@
package music
import (
"fmt"
"encoding/json"
"net/http"
"github.com/jmoiron/sqlx"
)
type Artist struct {
id string
name string
website string
ID string `json:"id"`
Name string `json:"name"`
Website string `json:"website"`
}
var Artists []Artist
@ -26,46 +27,50 @@ func GetArtist(id string) *Artist {
// GETTERS
func (artist Artist) GetID() string {
return artist.id
return artist.ID
}
func (artist Artist) GetName() string {
return artist.name
return artist.Name
}
func (artist Artist) GetWebsite() string {
return artist.website
return artist.Website
}
// SETTERS
func (artist Artist) SetID(id string) error {
artist.id = id
artist.ID = id
return nil
}
func (artist Artist) SetName(name string) error {
artist.name = name
artist.Name = name
return nil
}
func (artist Artist) SetWebsite(website string) error {
artist.website = website
artist.Website = website
return nil
}
// DATABASE
func (artist Artist) PushToDB(db *sqlx.DB) {
fmt.Printf("Pushing artist [%s] to database...", artist.name)
// fmt.Printf("Pushing artist [%s] to database...", artist.Name)
db.MustExec("INSERT INTO artists (id, name, website) VALUES ($1, $2, $3) ON CONFLICT (id) DO UPDATE SET name=$2, website=$3",
artist.id,
artist.name,
artist.website,
db.MustExec(
"INSERT INTO artists (id, name, website) "+
"VALUES ($1, $2, $3) "+
"ON CONFLICT (id) "+
"DO UPDATE SET name=$2, website=$3",
artist.ID,
artist.Name,
artist.Website,
)
fmt.Printf("done!\n")
// fmt.Printf("done!\n")
}
func PullAllArtists(db *sqlx.DB) ([]Artist, error) {
@ -80,9 +85,9 @@ func PullAllArtists(db *sqlx.DB) ([]Artist, error) {
var artist = Artist{}
err = rows.Scan(
&artist.id,
&artist.name,
&artist.website,
&artist.ID,
&artist.Name,
&artist.Website,
)
if err != nil {
return nil, err
@ -93,3 +98,58 @@ func PullAllArtists(db *sqlx.DB) ([]Artist, error) {
return artists, nil
}
// HTTP HANDLERS
func ServeArtist() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.NotFound(w, r)
return
}
type (
creditJSON struct {
Role string `json:"role"`
Primary bool `json:"primary"`
}
artistJSON struct {
Artist
Credits map[string]creditJSON `json:"credits"`
}
)
var res = artistJSON{}
res.ID = r.URL.Path[1:]
var artist = 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 Releases {
for _, credit := range release.Credits {
if credit.Artist.ID != res.ID {
continue
}
res.Credits[release.ID] = creditJSON{
Role: credit.Role,
Primary: credit.Primary,
}
}
}
jsonBytes, err := json.Marshal(res)
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonBytes)
})
}