arimelody.me/music/controller/credit.go

86 lines
1.9 KiB
Go
Raw Normal View History

package music
import (
"arimelody.me/arimelody.me/global"
"arimelody.me/arimelody.me/music/model"
"github.com/jmoiron/sqlx"
)
// DATABASE
func PullReleaseCredits(db *sqlx.DB, releaseID string) ([]*model.Credit, error) {
type creditDB struct {
Artist string
Role string
Primary bool `db:"is_primary"`
}
var credit_rows = []creditDB{}
var credits = []*model.Credit{}
err := db.Select(
&credit_rows,
"SELECT artist, role, is_primary FROM musiccredit WHERE release=$1",
releaseID,
)
if err != nil {
return nil, err
}
for _, c := range credit_rows {
credits = append(credits, &model.Credit{
Artist: global.GetArtist(c.Artist),
Role: c.Role,
Primary: c.Primary,
})
}
return credits, nil
}
func CreateCreditDB(db *sqlx.DB, releaseID string, artistID string, credit *model.Credit) (error) {
_, err := db.Exec(
"INSERT INTO musiccredit (release, artist, role, is_primary) "+
"VALUES ($1, $2, $3, $4)",
releaseID,
artistID,
credit.Role,
credit.Primary,
)
if err != nil {
return err
}
return nil
}
func UpdateCreditDB(db *sqlx.DB, releaseID string, artistID string, credit *model.Credit) (error) {
_, err := db.Exec(
"UPDATE musiccredit SET "+
"role=$3, is_primary=$4 "+
"WHERE release=$1, artist=$2",
releaseID,
artistID,
credit.Role,
credit.Primary,
)
if err != nil {
return err
}
return nil
}
func DeleteCreditDB(db *sqlx.DB, releaseID string, artistID string) (error) {
_, err := db.Exec(
"DELETE FROM musiccredit "+
"WHERE release=$1, artist=$2",
releaseID,
artistID,
)
if err != nil {
return err
}
return nil
}