arimelody.me/music/controller/release.go

318 lines
7.9 KiB
Go
Raw Normal View History

package music
import (
2024-09-01 23:15:23 +00:00
"errors"
"fmt"
2024-09-03 07:07:45 +00:00
"arimelody-web/music/model"
"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-03 07:07:45 +00:00
err := db.Get(&release, "SELECT * FROM musicrelease WHERE id=$1", id)
if err != nil {
return nil, err
}
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-03 07:07:45 +00:00
func GetAllReleases(db *sqlx.DB, onlyVisible bool, limit int, full bool) ([]*model.Release, error) {
var releases = []*model.Release{}
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)
}
if err != nil {
return nil, err
}
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
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)
}
}
}
return releases, nil
}
func CreateRelease(db *sqlx.DB, release *model.Release) error {
_, 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)",
release.ID,
release.Visible,
release.Title,
release.Description,
release.ReleaseType,
release.ReleaseDate.Format("2006-01-02 15:04:05"),
release.Artwork,
release.Buyname,
release.Buylink,
2024-09-01 23:15:23 +00:00
release.Copyright,
release.CopyrightURL,
)
if err != nil {
return err
}
return nil
}
func UpdateRelease(db *sqlx.DB, release *model.Release) error {
_, 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 "+
"WHERE id=$1",
release.ID,
release.Visible,
release.Title,
release.Description,
release.ReleaseType,
release.ReleaseDate.Format("2006-01-02 15:04:05"),
release.Artwork,
release.Buyname,
release.Buylink,
2024-09-01 23:15:23 +00:00
release.Copyright,
release.CopyrightURL,
)
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()
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(
"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)
if err != nil {
return err
}
}
2024-09-03 07:07:45 +00:00
err = tx.Commit()
if err != nil {
return err
}
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()
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
}
for _, credit := range new_credits {
2024-09-03 07:07:45 +00:00
_, err = tx.Exec(
"INSERT INTO musiccredit "+
"(release, artist, role, is_primary) "+
"VALUES ($1, $2, $3, $4)",
2024-09-03 07:07:45 +00:00
releaseID,
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
}
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()
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
}
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(
"INSERT INTO musiclink "+
"(release, name, url) "+
"VALUES ($1, $2, $3)",
2024-09-03 07:07:45 +00:00
releaseID,
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
}
return nil
}
2024-09-03 07:07:45 +00:00
func DeleteRelease(db *sqlx.DB, releaseID string) error {
_, err := db.Exec(
"DELETE FROM musicrelease "+
"WHERE id=$1",
2024-09-03 07:07:45 +00:00
releaseID,
)
if err != nil {
return err
}
return nil
}
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
}