mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-22 00:21:55 +00:00
Daniel Mason
9866dea36b
- Fix redirects to /login for auth required pages - Add handling for 401/429 + No connection responses in API calls - Add background workers for Go (clear out password resets) - Fixed timezone issues
88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
package goscrobble
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"log"
|
|
)
|
|
|
|
type Artist struct {
|
|
Uuid string `json:"uuid"`
|
|
Name string `json:"name"`
|
|
Desc sql.NullString `json:"desc"`
|
|
Img sql.NullString `json:"img"`
|
|
MusicBrainzID sql.NullString `json:"mbid"`
|
|
SpotifyID sql.NullString `json:"spotify_id"`
|
|
}
|
|
|
|
// insertArtist - This will return if it exists or create it based on MBID > Name
|
|
func insertArtist(name string, mbid string, spotifyId string, tx *sql.Tx) (Artist, error) {
|
|
artist := Artist{}
|
|
|
|
// Try locate our artist
|
|
if mbid != "" {
|
|
artist = fetchArtist("mbid", mbid, tx)
|
|
} else if spotifyId != "" {
|
|
artist = fetchArtist("spotify_id", spotifyId, tx)
|
|
}
|
|
|
|
// If it didn't match above, lookup by name
|
|
if artist.Uuid == "" {
|
|
artist = fetchArtist("name", name, tx)
|
|
}
|
|
|
|
// If we can't find it. Lets add it!
|
|
if artist.Uuid == "" {
|
|
err := insertNewArtist(name, mbid, spotifyId, tx)
|
|
if err != nil {
|
|
log.Printf("Error inserting artist: %+v", err)
|
|
return artist, errors.New("Failed to insert artist")
|
|
}
|
|
}
|
|
|
|
// Fetch the recently inserted artist to get the UUID
|
|
if mbid != "" {
|
|
artist = fetchArtist("mbid", mbid, tx)
|
|
} else if spotifyId != "" {
|
|
artist = fetchArtist("spotify_id", spotifyId, tx)
|
|
}
|
|
|
|
if artist.Uuid == "" {
|
|
artist = fetchArtist("name", name, tx)
|
|
}
|
|
|
|
if artist.Uuid == "" {
|
|
return artist, errors.New("Unable to fetch artist!")
|
|
}
|
|
|
|
return artist, nil
|
|
}
|
|
|
|
func fetchArtist(col string, val string, tx *sql.Tx) Artist {
|
|
var artist Artist
|
|
err := tx.QueryRow(
|
|
"SELECT BIN_TO_UUID(`uuid`, true), `name`, `desc`, `img`, `mbid` FROM `artists` WHERE `"+col+"` = ?",
|
|
val).Scan(&artist.Uuid, &artist.Name, &artist.Desc, &artist.Img, &artist.MusicBrainzID)
|
|
|
|
if err != nil {
|
|
if err != sql.ErrNoRows {
|
|
log.Printf("Error fetching artists: %+v", err)
|
|
}
|
|
}
|
|
|
|
return artist
|
|
}
|
|
|
|
func insertNewArtist(name string, mbid string, spotifyId string, tx *sql.Tx) error {
|
|
_, err := tx.Exec("INSERT INTO `artists` (`uuid`, `name`, `mbid`, `spotify_id`) "+
|
|
"VALUES (UUID_TO_BIN(UUID(), true),?,?,?)", name, mbid, spotifyId)
|
|
|
|
return err
|
|
}
|
|
|
|
func updateArtist(uuid string, col string, val string, tx *sql.Tx) error {
|
|
_, err := tx.Exec("UPDATE `artists` SET `"+col+"` = ? WHERE `uuid` = UUID_TO_BIN(?,true)", val, uuid)
|
|
|
|
return err
|
|
}
|