refactoring everything teehee (i'm so glad this isn't a team project)

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-08-01 01:39:18 +01:00
parent c684f0c7ae
commit 10f5f51e76
17 changed files with 970 additions and 547 deletions

95
api/v1/music/artist.go Normal file
View file

@ -0,0 +1,95 @@
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
}