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",
Website: "https://alex.targoninc.com",
}
var red = Artist{
Name: "smoljorb",
}
func make_date_work(date string) time.Time {
res, err := time.Parse("2-Jan-2006", date)
@ -142,29 +139,29 @@ var placeholders = []Album{
and the trees in the breeze
living my best life`,
},
{
Id: "free2play",
Title: "free2play",
Type: "upcoming",
ReleaseDate: make_date_work("17-Mar-2024"),
Buyname: "pre-order",
Buylink: "https://arimelody.me/",
Description: "hello from your local SPACEGIRL! 💫",
Credits: []AlbumCredit{
AlbumCredit{
Artist: &ari,
Role: "vocals",
},
AlbumCredit{
Artist: &ari,
Role: "production",
},
AlbumCredit{
Artist: &ari,
Role: "artwork",
},
},
},
// {
// Id: "free2play",
// Title: "free2play",
// Type: "upcoming",
// ReleaseDate: make_date_work("17-Mar-2024"),
// Buyname: "pre-order",
// Buylink: "https://arimelody.me/",
// Description: "hello from your local SPACEGIRL! 💫",
// Credits: []AlbumCredit{
// AlbumCredit{
// Artist: &ari,
// Role: "vocals",
// },
// AlbumCredit{
// Artist: &ari,
// Role: "production",
// },
// AlbumCredit{
// Artist: &ari,
// Role: "artwork",
// },
// },
// },
{
Id: "dream",
Title: "Dream",
@ -539,10 +536,6 @@ var placeholders = []Album{
Artist: &ari,
Role: "production",
},
AlbumCredit{
Artist: &red,
Role: "artwork",
},
},
},
{
@ -568,10 +561,6 @@ var placeholders = []Album{
Artist: &ari,
Role: "production",
},
AlbumCredit{
Artist: &red,
Role: "artwork",
},
},
},
{
@ -620,6 +609,6 @@ func QueryAllAlbums() ([]Album) {
}
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
m := map[string]Artist{}
res := []Artist{}
for _, credit := range album.Credits {
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
res := []Artist{}
for _, artist := range m {
res = append(res, artist)
}
return res
}
func (album Album) PrintArtists() string {
if len(album.Credits) == 1 {
return album.Credits[0].Artist.Name
artists := album.GetUniqueArtists()
if len(artists) == 1 {
return artists[0].Name
}
artists := album.GetUniqueArtists()
names := []string{}
for _, artist := range artists {
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 (
id SERIAL primary key,
album varchar(64) references Albums(id),
album varchar(64) references Albums(id) on delete cascade,
name text,
url text
url text,
constraint albumlinks_pk primary key (album, name)
);
CREATE TABLE IF NOT EXISTS AlbumCredits (
id SERIAL primary key,
album varchar(64) references Albums(id),
artist int references Artists(id),
album varchar(64) references Albums(id) on delete cascade,
artist int references Artists(id) on delete cascade,
role text
constraint albumcredits_pk primary key (album, artist, role)
);`
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) {
query := "SELECT count(*) FROM Albums WHERE id=$1"
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 {
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)"
if count != 0 {
query = "UPDATE Albums SET title=$2, release_date=$3, artwork=$4, buyname=$5, buylink=$6, description=$7, lyrics=$8 WHERE id=$1"
artist_ids := map[string]int{};
for _, credit := range album.Credits {
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)
_, err = db.Exec(query,
if count == 0 {
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.Title,
album.ReleaseDate.Format("2-Jan-2006"),
@ -93,12 +121,21 @@ func PushAlbum(db *sqlx.DB, album music.Album) {
&album.Description,
&album.Lyrics,
)
if err != nil {
fmt.Printf("error while pushing album [%s] to the database: %v\n", album.Id, err)
// we're just gonna completely fresh them because
// 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 {
@ -112,7 +149,7 @@ func InitDatabase() *sqlx.DB {
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
db.MustExec(schema)
// db.MustExec(schema)
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() {
db := InitDatabase()
for _, album := range music.QueryAllAlbums() {
PushAlbum(db, album)
}
for _, artist := range music.QueryAllArtists() {
PushArtist(db, artist)
}
for _, album := range music.QueryAllAlbums() {
PushAlbum(db, album)
}
defer db.Close()
}
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)
log.Fatal(http.ListenAndServe(":8080", nil))