2021-03-28 08:52:34 +00:00
|
|
|
package goscrobble
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2021-04-02 09:24:00 +00:00
|
|
|
"fmt"
|
2021-03-28 08:52:34 +00:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Album struct {
|
|
|
|
Uuid string `json:"uuid"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Desc sql.NullString `json:"desc"`
|
|
|
|
Img sql.NullString `json:"img"`
|
2021-04-03 08:29:31 +00:00
|
|
|
MusicBrainzID string `json:"mbid"`
|
|
|
|
SpotifyID string `json:"spotify_id"`
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertAlbum - This will return if it exists or create it based on MBID > Name
|
2021-04-02 09:24:00 +00:00
|
|
|
func insertAlbum(name string, mbid string, spotifyId string, artists []string, tx *sql.Tx) (Album, error) {
|
2021-03-28 08:52:34 +00:00
|
|
|
album := Album{}
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
// Try locate our album
|
2021-03-28 08:52:34 +00:00
|
|
|
if mbid != "" {
|
|
|
|
album = fetchAlbum("mbid", mbid, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
} else if spotifyId != "" {
|
|
|
|
album = fetchAlbum("spotify_id", spotifyId, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it didn't match above, lookup by name
|
|
|
|
if album.Uuid == "" {
|
|
|
|
album = fetchAlbum("name", name, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't find it. Lets add it!
|
|
|
|
if album.Uuid == "" {
|
|
|
|
err := insertNewAlbum(name, mbid, spotifyId, tx)
|
|
|
|
if err != nil {
|
|
|
|
return album, errors.New("Failed to insert album")
|
|
|
|
}
|
2021-03-28 08:52:34 +00:00
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
// Fetch the recently inserted album to get the UUID
|
|
|
|
if mbid != "" {
|
2021-03-28 08:52:34 +00:00
|
|
|
album = fetchAlbum("mbid", mbid, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
} else if spotifyId != "" {
|
|
|
|
album = fetchAlbum("spotify_id", spotifyId, tx)
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
if album.Uuid == "" {
|
2021-03-28 08:52:34 +00:00
|
|
|
album = fetchAlbum("name", name, tx)
|
2021-04-02 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try linkem up
|
|
|
|
err = album.linkAlbumToArtists(artists, tx)
|
|
|
|
if err != nil {
|
|
|
|
return album, errors.New("Unable to link albums!")
|
2021-03-28 08:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if album.Uuid == "" {
|
|
|
|
return album, errors.New("Unable to fetch album!")
|
|
|
|
}
|
|
|
|
|
2021-04-03 08:29:31 +00:00
|
|
|
if album.MusicBrainzID != mbid {
|
|
|
|
album.MusicBrainzID = mbid
|
|
|
|
album.updateAlbum("mbid", mbid, tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if album.SpotifyID != spotifyId {
|
|
|
|
album.SpotifyID = spotifyId
|
|
|
|
album.updateAlbum("spotify_id", spotifyId, tx)
|
|
|
|
}
|
|
|
|
|
2021-03-28 08:52:34 +00:00
|
|
|
return album, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchAlbum(col string, val string, tx *sql.Tx) Album {
|
|
|
|
var album Album
|
|
|
|
err := tx.QueryRow(
|
|
|
|
"SELECT BIN_TO_UUID(`uuid`, true), `name`, `desc`, `img`, `mbid` FROM `albums` WHERE `"+col+"` = ?",
|
|
|
|
val).Scan(&album.Uuid, &album.Name, &album.Desc, &album.Img, &album.MusicBrainzID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.Printf("Error fetching albums: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return album
|
|
|
|
}
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
func insertNewAlbum(name string, mbid string, spotifyId string, tx *sql.Tx) error {
|
|
|
|
_, err := tx.Exec("INSERT INTO `albums` (`uuid`, `name`, `mbid`, `spotify_id`) "+
|
|
|
|
"VALUES (UUID_TO_BIN(UUID(), true),?,?,?)", name, mbid, spotifyId)
|
2021-03-28 08:52:34 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (album *Album) linkAlbumToArtists(artists []string, tx *sql.Tx) error {
|
|
|
|
var err error
|
|
|
|
for _, artist := range artists {
|
2021-03-28 09:24:18 +00:00
|
|
|
_, err = tx.Exec("INSERT INTO `album_artist` (`album`, `artist`) "+
|
|
|
|
"VALUES (UUID_TO_BIN(?, true), UUID_TO_BIN(?, true))", album.Uuid, artist)
|
2021-03-28 08:52:34 +00:00
|
|
|
if err != nil {
|
2021-04-02 09:24:00 +00:00
|
|
|
fmt.Println(err)
|
2021-03-28 08:52:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-04-02 09:24:00 +00:00
|
|
|
|
2021-04-03 08:29:31 +00:00
|
|
|
func (album *Album) updateAlbum(col string, val string, tx *sql.Tx) error {
|
|
|
|
_, err := tx.Exec("UPDATE `albums` SET `"+col+"` = ? WHERE `uuid` = UUID_TO_BIN(?,true)", val, album.Uuid)
|
2021-04-02 09:24:00 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|