arimelody.me/music/credit.go
ari melody cba791deba MORE REFACTORING!! + some improvements
Signed-off-by: ari melody <ari@arimelody.me>
2024-08-31 01:52:35 +01:00

93 lines
2 KiB
Go

package music
import (
"fmt"
"github.com/jmoiron/sqlx"
)
type (
Credit struct {
Artist *Artist `json:"artist"`
Role string `json:"role"`
Primary bool `json:"primary"`
}
PostCreditBody struct {
Artist string `json:"artist"`
Role string `json:"role"`
Primary bool `json:"primary"`
}
)
// GETTERS
func (credit Credit) GetArtist() Artist {
return *credit.Artist
}
func (credit Credit) GetRole() string {
return credit.Role
}
func (credit Credit) IsPrimary() bool {
return credit.Primary
}
// SETTERS
func (credit Credit) SetArtist(artist *Artist) error {
// TODO: update DB
credit.Artist = artist
return nil
}
func (credit Credit) SetRole(role string) error {
// TODO: update DB
credit.Role = role
return nil
}
func (credit Credit) SetPrimary(primary bool) error {
// TODO: update DB
credit.Primary = primary
return nil
}
// DATABASE
func PullReleaseCredits(db *sqlx.DB, releaseID string) ([]Credit, error) {
var credits = []Credit{}
credit_rows, err := db.Query("SELECT artist, role, is_primary FROM musiccredits WHERE release=$1", releaseID)
if err != nil {
return []Credit{}, err
}
for credit_rows.Next() {
var artistID string
var credit = Credit{}
err = credit_rows.Scan(
&artistID,
&credit.Role,
&credit.Primary,
)
if err != nil {
fmt.Printf("Error while pulling credit for release %s: %s\n", releaseID, err)
continue
}
credit.Artist = GetArtist(artistID)
if credit.Artist == nil {
// this should absolutely not happen ever due to foreign key
// constraints, but it doesn't hurt to be sure!
fmt.Printf("Error while pulling credit for release %s: Artist %s does not exist\n", releaseID, artistID)
continue
}
credits = append(credits, credit)
}
return credits, nil
}