2024-11-01 19:33:26 +00:00
|
|
|
package controller
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
import (
|
2024-11-01 19:33:26 +00:00
|
|
|
"arimelody-web/model"
|
2024-08-02 21:48:26 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DATABASE
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func GetArtist(db *sqlx.DB, id string) (*model.Artist, error) {
|
|
|
|
var artist = model.Artist{}
|
|
|
|
|
|
|
|
err := db.Get(&artist, "SELECT * FROM artist WHERE id=$1", id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &artist, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllArtists(db *sqlx.DB) ([]*model.Artist, error) {
|
2024-08-02 23:27:30 +00:00
|
|
|
var artists = []*model.Artist{}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
err := db.Select(&artists, "SELECT * FROM artist")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return artists, nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 23:15:23 +00:00
|
|
|
func GetArtistsNotOnRelease(db *sqlx.DB, releaseID string) ([]*model.Artist, error) {
|
2024-09-01 03:43:32 +00:00
|
|
|
var artists = []*model.Artist{}
|
|
|
|
|
|
|
|
err := db.Select(&artists,
|
|
|
|
"SELECT * FROM artist "+
|
|
|
|
"WHERE id NOT IN "+
|
|
|
|
"(SELECT artist FROM musiccredit WHERE release=$1)",
|
2024-09-01 23:15:23 +00:00
|
|
|
releaseID)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return artists, nil
|
|
|
|
}
|
|
|
|
|
2024-11-01 19:15:19 +00:00
|
|
|
func GetArtistCredits(db *sqlx.DB, artistID string, show_hidden bool) ([]*model.Credit, error) {
|
|
|
|
var query string = "SELECT release.id,title,artwork,release_date,artist.id,name,website,avatar,role,is_primary "+
|
2024-09-03 07:07:45 +00:00
|
|
|
"FROM musiccredit "+
|
|
|
|
"JOIN musicrelease AS release ON release=release.id "+
|
|
|
|
"JOIN artist ON artist=artist.id "+
|
2024-11-01 19:15:19 +00:00
|
|
|
"WHERE artist=$1 "
|
|
|
|
if !show_hidden { query += "AND visible=true " }
|
|
|
|
query += "ORDER BY release_date DESC"
|
|
|
|
rows, err := db.Query(query, artistID)
|
2024-09-01 23:15:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-03 07:07:45 +00:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
type NamePrimary struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Primary bool `json:"primary" db:"is_primary"`
|
|
|
|
}
|
2024-09-01 23:15:23 +00:00
|
|
|
var credits []*model.Credit
|
2024-09-03 07:07:45 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var credit model.Credit
|
|
|
|
err = rows.Scan(
|
|
|
|
&credit.Release.ID,
|
|
|
|
&credit.Release.Title,
|
|
|
|
&credit.Release.Artwork,
|
2024-11-01 19:15:19 +00:00
|
|
|
&credit.Release.ReleaseDate,
|
2024-09-03 07:07:45 +00:00
|
|
|
&credit.Artist.ID,
|
|
|
|
&credit.Artist.Name,
|
|
|
|
&credit.Artist.Website,
|
|
|
|
&credit.Artist.Avatar,
|
|
|
|
&credit.Role,
|
|
|
|
&credit.Primary,
|
|
|
|
)
|
|
|
|
|
|
|
|
otherArtists := []NamePrimary{}
|
|
|
|
err = db.Select(&otherArtists,
|
|
|
|
"SELECT name,is_primary FROM artist "+
|
|
|
|
"JOIN musiccredit ON artist=id "+
|
|
|
|
"WHERE release=$1",
|
|
|
|
credit.Release.ID)
|
|
|
|
for _, otherCredit := range otherArtists {
|
|
|
|
credit.Release.Credits = append(credit.Release.Credits, &model.Credit{
|
|
|
|
Artist: model.Artist{
|
|
|
|
Name: otherCredit.Name,
|
|
|
|
},
|
|
|
|
Primary: otherCredit.Primary,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
credits = append(credits, &credit)
|
2024-09-01 23:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return credits, nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func CreateArtist(db *sqlx.DB, artist *model.Artist) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"INSERT INTO artist (id, name, website, avatar) "+
|
|
|
|
"VALUES ($1, $2, $3, $4)",
|
|
|
|
artist.ID,
|
|
|
|
artist.Name,
|
|
|
|
artist.Website,
|
|
|
|
artist.Avatar,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func UpdateArtist(db *sqlx.DB, artist *model.Artist) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"UPDATE artist "+
|
|
|
|
"SET name=$2, website=$3, avatar=$4 "+
|
|
|
|
"WHERE id=$1",
|
|
|
|
artist.ID,
|
|
|
|
artist.Name,
|
|
|
|
artist.Website,
|
|
|
|
artist.Avatar,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func DeleteArtist(db *sqlx.DB, artistID string) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"DELETE FROM artist "+
|
|
|
|
"WHERE id=$1",
|
2024-09-03 07:07:45 +00:00
|
|
|
artistID,
|
2024-08-02 21:48:26 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|