arimelody.me/api/v1/music/artist.go
2024-08-31 01:52:33 +01:00

96 lines
1.5 KiB
Go

package music
import (
"fmt"
"github.com/jmoiron/sqlx"
)
type Artist struct {
id string
name string
website string
}
var Artists []Artist
func GetArtist(id string) *Artist {
for _, artist := range Artists {
if artist.GetID() == id {
return &artist
}
}
return nil
}
// GETTERS
func (artist Artist) GetID() string {
return artist.id
}
func (artist Artist) GetName() string {
return artist.name
}
func (artist Artist) GetWebsite() string {
return artist.website
}
// SETTERS
func (artist Artist) SetID(id string) error {
artist.id = id
return nil
}
func (artist Artist) SetName(name string) error {
artist.name = name
return nil
}
func (artist Artist) SetWebsite(website string) error {
artist.website = website
return nil
}
// DATABASE
func (artist Artist) PushToDB(db *sqlx.DB) {
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,
)
fmt.Printf("done!\n")
}
func PullAllArtists(db *sqlx.DB) ([]Artist, error) {
artists := []Artist{}
rows, err := db.Query("SELECT id, name, website FROM artists")
if err != nil {
return nil, err
}
for rows.Next() {
var artist = Artist{}
err = rows.Scan(
&artist.id,
&artist.name,
&artist.website,
)
if err != nil {
return nil, err
}
artists = append(artists, artist)
}
return artists, nil
}