minor fixes

Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
ari melody 2024-03-18 23:09:54 +00:00
parent 5eecef1d78
commit ae7477f86f
4 changed files with 99 additions and 67 deletions

View file

@ -21,9 +21,6 @@ var loudar = Artist{
Name: "Loudar", Name: "Loudar",
Website: "https://alex.targoninc.com", Website: "https://alex.targoninc.com",
} }
var red = Artist{
Name: "smoljorb",
}
func make_date_work(date string) time.Time { func make_date_work(date string) time.Time {
res, err := time.Parse("2-Jan-2006", date) res, err := time.Parse("2-Jan-2006", date)
@ -142,29 +139,29 @@ var placeholders = []Album{
and the trees in the breeze and the trees in the breeze
living my best life`, living my best life`,
}, },
{ // {
Id: "free2play", // Id: "free2play",
Title: "free2play", // Title: "free2play",
Type: "upcoming", // Type: "upcoming",
ReleaseDate: make_date_work("17-Mar-2024"), // ReleaseDate: make_date_work("17-Mar-2024"),
Buyname: "pre-order", // Buyname: "pre-order",
Buylink: "https://arimelody.me/", // Buylink: "https://arimelody.me/",
Description: "hello from your local SPACEGIRL! 💫", // Description: "hello from your local SPACEGIRL! 💫",
Credits: []AlbumCredit{ // Credits: []AlbumCredit{
AlbumCredit{ // AlbumCredit{
Artist: &ari, // Artist: &ari,
Role: "vocals", // Role: "vocals",
}, // },
AlbumCredit{ // AlbumCredit{
Artist: &ari, // Artist: &ari,
Role: "production", // Role: "production",
}, // },
AlbumCredit{ // AlbumCredit{
Artist: &ari, // Artist: &ari,
Role: "artwork", // Role: "artwork",
}, // },
}, // },
}, // },
{ {
Id: "dream", Id: "dream",
Title: "Dream", Title: "Dream",
@ -539,10 +536,6 @@ var placeholders = []Album{
Artist: &ari, Artist: &ari,
Role: "production", Role: "production",
}, },
AlbumCredit{
Artist: &red,
Role: "artwork",
},
}, },
}, },
{ {
@ -568,10 +561,6 @@ var placeholders = []Album{
Artist: &ari, Artist: &ari,
Role: "production", Role: "production",
}, },
AlbumCredit{
Artist: &red,
Role: "artwork",
},
}, },
}, },
{ {
@ -620,6 +609,6 @@ func QueryAllAlbums() ([]Album) {
} }
func QueryAllArtists() ([]Artist) { func QueryAllArtists() ([]Artist) {
return []Artist{ ari, zaire, mae, loudar, red } return []Artist{ ari, zaire, mae, loudar }
} }

View file

@ -43,26 +43,32 @@ func (album Album) GetUniqueArtists() []Artist {
} }
// create a map of artists to prevent duplicates // create a map of artists to prevent duplicates
m := map[string]Artist{} res := []Artist{}
for _, credit := range album.Credits { for _, credit := range album.Credits {
artist := *credit.Artist artist := *credit.Artist
m[artist.Name] = artist exists := false
for _, c := range res {
if c == *credit.Artist {
exists = true
break
}
}
if exists {
continue
}
res = append(res, artist)
} }
// now create the actual array to send // now create the actual array to send
res := []Artist{}
for _, artist := range m {
res = append(res, artist)
}
return res return res
} }
func (album Album) PrintArtists() string { func (album Album) PrintArtists() string {
if len(album.Credits) == 1 { artists := album.GetUniqueArtists()
return album.Credits[0].Artist.Name if len(artists) == 1 {
return artists[0].Name
} }
artists := album.GetUniqueArtists()
names := []string{} names := []string{}
for _, artist := range artists { for _, artist := range artists {
names = append(names, artist.Name) names = append(names, artist.Name)

73
db.go
View file

@ -30,17 +30,17 @@ CREATE TABLE IF NOT EXISTS Albums (
); );
CREATE TABLE IF NOT EXISTS AlbumLinks ( CREATE TABLE IF NOT EXISTS AlbumLinks (
id SERIAL primary key, album varchar(64) references Albums(id) on delete cascade,
album varchar(64) references Albums(id),
name text, name text,
url text url text,
constraint albumlinks_pk primary key (album, name)
); );
CREATE TABLE IF NOT EXISTS AlbumCredits ( CREATE TABLE IF NOT EXISTS AlbumCredits (
id SERIAL primary key, album varchar(64) references Albums(id) on delete cascade,
album varchar(64) references Albums(id), artist int references Artists(id) on delete cascade,
artist int references Artists(id),
role text role text
constraint albumcredits_pk primary key (album, artist, role)
);` );`
func PushArtist(db *sqlx.DB, artist music.Artist) { func PushArtist(db *sqlx.DB, artist music.Artist) {
@ -70,20 +70,48 @@ func PushArtist(db *sqlx.DB, artist music.Artist) {
} }
func PushAlbum(db *sqlx.DB, album music.Album) { func PushAlbum(db *sqlx.DB, album music.Album) {
query := "SELECT count(*) FROM Albums WHERE id=$1"
var count int var count int
err := db.Get(&count, query, album.Id) err := db.Get(&count, "SELECT count(*) FROM Albums WHERE id=$1", album.Id)
if err != nil { if err != nil {
fmt.Printf("error while pushing album [%s] to the database: %v\n", album.Id, err) fmt.Printf("error while pushing album [%s] to the database: %v\n", album.Id, err)
} }
query = "INSERT INTO Albums (id, title, release_date, artwork, buyname, buylink, description, lyrics) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)" artist_ids := map[string]int{};
if count != 0 { for _, credit := range album.Credits {
query = "UPDATE Albums SET title=$2, release_date=$3, artwork=$4, buyname=$5, buylink=$6, description=$7, lyrics=$8 WHERE id=$1" if _, ok := artist_ids[credit.Artist.Name]; ok {
continue;
}
var id int
err := db.Get(&id, "SELECT id FROM Artists WHERE name=$1", credit.Artist.Name)
if err != nil {
continue;
}
artist_ids[credit.Artist.Name] = id
} }
fmt.Printf("saving album [%s] to the database...", album.Id) if count == 0 {
_, err = db.Exec(query, fmt.Printf("creating album [%s]...", album.Id)
tx := db.MustBegin()
tx.MustExec("INSERT INTO Albums VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", &album.Id, &album.Title, album.ReleaseDate.Format("2-Jan-2006"), &album.Artwork, &album.Buyname, &album.Buylink, &album.Description, &album.Lyrics)
for _, link := range album.Links {
tx.MustExec("INSERT INTO AlbumLinks (album, name, url) VALUES ($1, $2, $3)", &album.Id, &link.Name, &link.Url)
}
for _, credit := range album.Credits {
artist_id := artist_ids[credit.Artist.Name]
tx.MustExec("INSERT INTO AlbumCredits (album, artist, role) VALUES ($1, $2, $3)", &album.Id, &artist_id, &credit.Role)
}
tx.Commit()
fmt.Printf("done!\n")
return;
}
fmt.Printf("updating album [%s]...", album.Id)
tx := db.MustBegin()
tx.MustExec("UPDATE Albums SET title=$2, release_date=$3, artwork=$4, buyname=$5, buylink=$6, description=$7, lyrics=$8 WHERE id=$1",
&album.Id, &album.Id,
&album.Title, &album.Title,
album.ReleaseDate.Format("2-Jan-2006"), album.ReleaseDate.Format("2-Jan-2006"),
@ -93,12 +121,21 @@ func PushAlbum(db *sqlx.DB, album music.Album) {
&album.Description, &album.Description,
&album.Lyrics, &album.Lyrics,
) )
if err != nil { // we're just gonna completely fresh them because
fmt.Printf("error while pushing album [%s] to the database: %v\n", album.Id, err) // like hell am i actually gonna comb through every
// single one of these
tx.MustExec("DELETE FROM AlbumLinks WHERE album=$1", &album.Id)
tx.MustExec("DELETE FROM AlbumCredits WHERE album=$1", &album.Id)
for _, link := range album.Links {
tx.MustExec("INSERT INTO AlbumLinks (album, name, url) VALUES ($1, $2, $3)", &album.Id, &link.Name, &link.Url)
} }
fmt.Printf("done!\n") for _, credit := range album.Credits {
artist_id := artist_ids[credit.Artist.Name]
tx.MustExec("INSERT INTO AlbumCredits (album, artist, role) VALUES ($1, $2, $3)", &album.Id, &artist_id, &credit.Role)
}
tx.Commit()
// defer db.Close() fmt.Printf("done!\n")
} }
func InitDatabase() *sqlx.DB { func InitDatabase() *sqlx.DB {
@ -112,7 +149,7 @@ func InitDatabase() *sqlx.DB {
db.SetMaxOpenConns(10) db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10) db.SetMaxIdleConns(10)
db.MustExec(schema) // db.MustExec(schema)
return db return db
} }

10
main.go
View file

@ -179,19 +179,19 @@ func parse_markdown(md []byte) []byte {
func push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later() { func push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later() {
db := InitDatabase() db := InitDatabase()
for _, album := range music.QueryAllAlbums() {
PushAlbum(db, album)
}
for _, artist := range music.QueryAllArtists() { for _, artist := range music.QueryAllArtists() {
PushArtist(db, artist) PushArtist(db, artist)
} }
for _, album := range music.QueryAllAlbums() {
PushAlbum(db, album)
}
defer db.Close() defer db.Close()
} }
func main() { func main() {
// push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later() push_to_db_this_is_a_testing_thing_and_will_be_superfluous_later()
http.HandleFunc("/", web_handler) http.HandleFunc("/", web_handler)
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))