2024-11-01 19:33:26 +00:00
|
|
|
package controller
|
2024-08-02 21:48:26 +00:00
|
|
|
|
|
|
|
import (
|
2024-09-01 23:15:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2024-11-01 19:33:26 +00:00
|
|
|
"arimelody-web/model"
|
2024-08-02 21:48:26 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
)
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func GetRelease(db *sqlx.DB, id string, full bool) (*model.Release, error) {
|
|
|
|
var release = model.Release{}
|
2024-09-01 03:43:32 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
err := db.Get(&release, "SELECT * FROM musicrelease WHERE id=$1", id)
|
2024-09-01 03:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
if full {
|
|
|
|
// get credits
|
|
|
|
credits, err := GetReleaseCredits(db, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Credits: %s", err))
|
|
|
|
}
|
|
|
|
for _, credit := range credits {
|
|
|
|
release.Credits = append(release.Credits, credit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get tracks
|
|
|
|
tracks, err := GetReleaseTracks(db, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Tracks: %s", err))
|
|
|
|
}
|
|
|
|
for _, track := range tracks {
|
|
|
|
release.Tracks = append(release.Tracks, track)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get links
|
|
|
|
links, err := GetReleaseLinks(db, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Links: %s", err))
|
|
|
|
}
|
|
|
|
for _, link := range links {
|
|
|
|
release.Links = append(release.Links, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &release, nil
|
2024-09-01 03:43:32 +00:00
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func GetAllReleases(db *sqlx.DB, onlyVisible bool, limit int, full bool) ([]*model.Release, error) {
|
2024-08-02 23:27:30 +00:00
|
|
|
var releases = []*model.Release{}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
query := "SELECT * FROM musicrelease"
|
|
|
|
if onlyVisible {
|
|
|
|
query += " WHERE visible=true"
|
|
|
|
}
|
|
|
|
query += " ORDER BY release_date DESC"
|
|
|
|
var err error
|
|
|
|
if limit > 0 {
|
|
|
|
err = db.Select(&releases, query + " LIMIT $1", limit)
|
|
|
|
} else {
|
|
|
|
err = db.Select(&releases, query)
|
|
|
|
}
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-11-01 19:15:19 +00:00
|
|
|
for _, release := range releases {
|
|
|
|
// get credits
|
|
|
|
credits, err := GetReleaseCredits(db, release.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Credits: %s", err))
|
|
|
|
}
|
|
|
|
for _, credit := range credits {
|
|
|
|
release.Credits = append(release.Credits, credit)
|
|
|
|
}
|
2024-09-03 07:07:45 +00:00
|
|
|
|
2024-11-01 19:15:19 +00:00
|
|
|
if full {
|
2024-09-03 07:07:45 +00:00
|
|
|
// get tracks
|
|
|
|
tracks, err := GetReleaseTracks(db, release.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Tracks: %s", err))
|
|
|
|
}
|
|
|
|
for _, track := range tracks {
|
|
|
|
release.Tracks = append(release.Tracks, track)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get links
|
|
|
|
links, err := GetReleaseLinks(db, release.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Links: %s", err))
|
|
|
|
}
|
|
|
|
for _, link := range links {
|
|
|
|
release.Links = append(release.Links, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
return releases, nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func CreateRelease(db *sqlx.DB, release *model.Release) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"INSERT INTO musicrelease "+
|
2024-09-01 23:15:23 +00:00
|
|
|
"(id, visible, title, description, type, release_date, artwork, buyname, buylink, copyright, copyrighturl) "+
|
|
|
|
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
|
2024-08-02 21:48:26 +00:00
|
|
|
release.ID,
|
|
|
|
release.Visible,
|
|
|
|
release.Title,
|
|
|
|
release.Description,
|
|
|
|
release.ReleaseType,
|
2024-08-05 00:23:17 +00:00
|
|
|
release.ReleaseDate.Format("2006-01-02 15:04:05"),
|
2024-08-02 21:48:26 +00:00
|
|
|
release.Artwork,
|
|
|
|
release.Buyname,
|
|
|
|
release.Buylink,
|
2024-09-01 23:15:23 +00:00
|
|
|
release.Copyright,
|
|
|
|
release.CopyrightURL,
|
2024-08-02 21:48:26 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func UpdateRelease(db *sqlx.DB, release *model.Release) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"UPDATE musicrelease SET "+
|
2024-09-01 23:15:23 +00:00
|
|
|
"visible=$2, title=$3, description=$4, type=$5, release_date=$6, artwork=$7, buyname=$8, buylink=$9, copyright=$10, copyrighturl=$11 "+
|
2024-08-02 21:48:26 +00:00
|
|
|
"WHERE id=$1",
|
|
|
|
release.ID,
|
2024-08-03 22:24:15 +00:00
|
|
|
release.Visible,
|
2024-08-02 21:48:26 +00:00
|
|
|
release.Title,
|
|
|
|
release.Description,
|
|
|
|
release.ReleaseType,
|
2024-08-05 00:23:17 +00:00
|
|
|
release.ReleaseDate.Format("2006-01-02 15:04:05"),
|
2024-08-02 21:48:26 +00:00
|
|
|
release.Artwork,
|
|
|
|
release.Buyname,
|
|
|
|
release.Buylink,
|
2024-09-01 23:15:23 +00:00
|
|
|
release.Copyright,
|
|
|
|
release.CopyrightURL,
|
2024-08-02 21:48:26 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func UpdateReleaseTracks(db *sqlx.DB, releaseID string, new_tracks []string) error {
|
|
|
|
tx, err := db.Begin()
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM musicreleasetrack WHERE release=$1", releaseID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-01 23:15:23 +00:00
|
|
|
for i, trackID := range new_tracks {
|
2024-09-03 07:07:45 +00:00
|
|
|
_, err = tx.Exec(
|
2024-08-03 22:24:15 +00:00
|
|
|
"INSERT INTO musicreleasetrack "+
|
|
|
|
"(release, track, number) "+
|
|
|
|
"VALUES ($1, $2, $3)",
|
2024-09-03 07:07:45 +00:00
|
|
|
releaseID,
|
2024-09-01 23:15:23 +00:00
|
|
|
trackID,
|
2024-09-03 07:07:45 +00:00
|
|
|
i)
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func UpdateReleaseCredits(db *sqlx.DB, releaseID string, new_credits []*model.Credit) error {
|
|
|
|
tx, err := db.Begin()
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM musiccredit WHERE release=$1", releaseID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-03 22:24:15 +00:00
|
|
|
for _, credit := range new_credits {
|
2024-09-03 07:07:45 +00:00
|
|
|
_, err = tx.Exec(
|
2024-08-03 22:24:15 +00:00
|
|
|
"INSERT INTO musiccredit "+
|
|
|
|
"(release, artist, role, is_primary) "+
|
|
|
|
"VALUES ($1, $2, $3, $4)",
|
2024-09-03 07:07:45 +00:00
|
|
|
releaseID,
|
2024-08-03 22:24:15 +00:00
|
|
|
credit.Artist.ID,
|
|
|
|
credit.Role,
|
|
|
|
credit.Primary,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func UpdateReleaseLinks(db *sqlx.DB, releaseID string, new_links []*model.Link) error {
|
|
|
|
tx, err := db.Begin()
|
2024-08-03 22:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM musiclink WHERE release=$1", releaseID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-03 22:24:15 +00:00
|
|
|
for _, link := range new_links {
|
2024-09-03 07:07:45 +00:00
|
|
|
fmt.Printf("%s: %s\n", link.Name, link.URL)
|
|
|
|
_, err := tx.Exec(
|
2024-08-03 22:24:15 +00:00
|
|
|
"INSERT INTO musiclink "+
|
|
|
|
"(release, name, url) "+
|
|
|
|
"VALUES ($1, $2, $3)",
|
2024-09-03 07:07:45 +00:00
|
|
|
releaseID,
|
2024-08-03 22:24:15 +00:00
|
|
|
link.Name,
|
|
|
|
link.URL,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func DeleteRelease(db *sqlx.DB, releaseID string) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"DELETE FROM musicrelease "+
|
|
|
|
"WHERE id=$1",
|
2024-09-03 07:07:45 +00:00
|
|
|
releaseID,
|
2024-08-02 21:48:26 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-01 03:43:32 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func GetReleaseTracks(db *sqlx.DB, releaseID string) ([]*model.Track, error) {
|
|
|
|
var tracks = []*model.Track{}
|
2024-09-01 23:15:23 +00:00
|
|
|
|
|
|
|
err := db.Select(&tracks,
|
|
|
|
"SELECT musictrack.* FROM musictrack "+
|
|
|
|
"JOIN musicreleasetrack ON track=id "+
|
|
|
|
"WHERE release=$1 "+
|
|
|
|
"ORDER BY number ASC",
|
|
|
|
releaseID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracks, nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func GetReleaseCredits(db *sqlx.DB, releaseID string) ([]*model.Credit, error) {
|
|
|
|
rows, err := db.Query(
|
|
|
|
"SELECT artist.id,artist.name,artist.website,artist.avatar,role,is_primary "+
|
|
|
|
"FROM musiccredit "+
|
|
|
|
"JOIN artist ON artist=artist.id "+
|
|
|
|
"JOIN musicrelease ON release=musicrelease.id "+
|
|
|
|
"WHERE musicrelease.id=$1 "+
|
|
|
|
"ORDER BY is_primary DESC",
|
2024-09-01 23:15:23 +00:00
|
|
|
releaseID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
var credits []*model.Credit
|
|
|
|
for rows.Next() {
|
|
|
|
credit := model.Credit{}
|
|
|
|
rows.Scan(
|
|
|
|
&credit.Artist.ID,
|
|
|
|
&credit.Artist.Name,
|
|
|
|
&credit.Artist.Website,
|
|
|
|
&credit.Artist.Avatar,
|
|
|
|
&credit.Role,
|
|
|
|
&credit.Primary)
|
|
|
|
credits = append(credits, &credit)
|
2024-09-01 23:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return credits, nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func GetReleaseLinks(db *sqlx.DB, releaseID string) ([]*model.Link, error) {
|
|
|
|
var links = []*model.Link{}
|
2024-09-01 23:15:23 +00:00
|
|
|
|
|
|
|
err := db.Select(&links, "SELECT name,url FROM musiclink WHERE release=$1", releaseID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return links, nil
|
|
|
|
}
|