MORE REFACTORING!! + some improvements
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
151b2d8fd9
commit
cba791deba
17 changed files with 376 additions and 223 deletions
92
music/credit.go
Normal file
92
music/credit.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue