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 GetTrack(db *sqlx.DB, id string) (*model.Track, error) {
|
|
|
|
var track = model.Track{}
|
|
|
|
|
|
|
|
stmt, _ := db.Preparex("SELECT * FROM musictrack WHERE id=$1")
|
|
|
|
err := stmt.Get(&track, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &track, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllTracks(db *sqlx.DB) ([]*model.Track, error) {
|
|
|
|
var tracks = []*model.Track{}
|
|
|
|
|
|
|
|
err := db.Select(&tracks, "SELECT * FROM musictrack")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOrphanTracks(db *sqlx.DB) ([]*model.Track, error) {
|
2024-08-02 23:27:30 +00:00
|
|
|
var tracks = []*model.Track{}
|
2024-08-02 21:48:26 +00:00
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
err := db.Select(&tracks, "SELECT * FROM musictrack WHERE id NOT IN (SELECT track FROM musicreleasetrack)")
|
2024-08-02 21:48:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracks, nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 23:15:23 +00:00
|
|
|
func GetTracksNotOnRelease(db *sqlx.DB, releaseID string) ([]*model.Track, error) {
|
2024-09-01 03:43:32 +00:00
|
|
|
var tracks = []*model.Track{}
|
|
|
|
|
|
|
|
err := db.Select(&tracks,
|
|
|
|
"SELECT * FROM musictrack "+
|
|
|
|
"WHERE id NOT IN "+
|
|
|
|
"(SELECT track FROM musicreleasetrack 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 tracks, nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
func GetTrackReleases(db *sqlx.DB, trackID string, full bool) ([]*model.Release, error) {
|
|
|
|
var releases = []*model.Release{}
|
2024-09-01 03:43:32 +00:00
|
|
|
|
|
|
|
err := db.Select(&releases,
|
2024-09-03 07:07:45 +00:00
|
|
|
"SELECT id,title,type,release_date,artwork,buylink "+
|
|
|
|
"FROM musicrelease "+
|
2024-09-01 03:43:32 +00:00
|
|
|
"JOIN musicreleasetrack ON release=id "+
|
|
|
|
"WHERE track=$1 "+
|
|
|
|
"ORDER BY release_date",
|
2024-09-01 23:15:23 +00:00
|
|
|
trackID,
|
2024-09-01 03:43:32 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
type NamePrimary struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Primary bool `json:"primary" db:"is_primary"`
|
|
|
|
}
|
|
|
|
for _, release := range releases {
|
|
|
|
// get artists
|
|
|
|
credits := []NamePrimary{}
|
|
|
|
err := db.Select(&credits,
|
|
|
|
"SELECT name,is_primary FROM artist "+
|
|
|
|
"JOIN musiccredit ON artist=artist.id "+
|
|
|
|
"JOIN musicrelease ON release=musicrelease.id "+
|
|
|
|
"WHERE musicrelease.id=$1", release.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, credit := range credits {
|
|
|
|
release.Credits = append(release.Credits, &model.Credit{
|
|
|
|
Artist: model.Artist{
|
|
|
|
Name: credit.Name,
|
|
|
|
},
|
|
|
|
Primary: credit.Primary,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// get tracks
|
|
|
|
tracks := []string{}
|
|
|
|
err = db.Select(&tracks, "SELECT track FROM musicreleasetrack WHERE release=$1", release.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, trackID := range tracks {
|
|
|
|
release.Tracks = append(release.Tracks, &model.Track{
|
|
|
|
ID: trackID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
return releases, nil
|
|
|
|
}
|
|
|
|
|
2024-08-03 22:24:15 +00:00
|
|
|
func PullOrphanTracks(db *sqlx.DB) ([]*model.Track, error) {
|
|
|
|
var tracks = []*model.Track{}
|
|
|
|
|
|
|
|
err := db.Select(&tracks,
|
|
|
|
"SELECT id, title, description, lyrics, preview_url FROM musictrack "+
|
|
|
|
"WHERE id NOT IN "+
|
|
|
|
"(SELECT track FROM musicreleasetrack)",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tracks, nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func CreateTrack(db *sqlx.DB, track *model.Track) (string, error) {
|
2024-08-02 21:48:26 +00:00
|
|
|
var trackID string
|
|
|
|
err := db.QueryRow(
|
|
|
|
"INSERT INTO musictrack (title, description, lyrics, preview_url) "+
|
|
|
|
"VALUES ($1, $2, $3, $4) "+
|
|
|
|
"RETURNING id",
|
|
|
|
track.Title,
|
|
|
|
track.Description,
|
|
|
|
track.Lyrics,
|
|
|
|
track.PreviewURL,
|
|
|
|
).Scan(&trackID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return trackID, nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:43:32 +00:00
|
|
|
func UpdateTrack(db *sqlx.DB, track *model.Track) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"UPDATE musictrack "+
|
|
|
|
"SET title=$2, description=$3, lyrics=$4, preview_url=$5 "+
|
2024-08-03 23:23:55 +00:00
|
|
|
"WHERE id=$1",
|
2024-08-02 21:48:26 +00:00
|
|
|
track.ID,
|
|
|
|
track.Title,
|
|
|
|
track.Description,
|
|
|
|
track.Lyrics,
|
|
|
|
track.PreviewURL,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-01 23:15:23 +00:00
|
|
|
func DeleteTrack(db *sqlx.DB, trackID string) error {
|
2024-08-02 21:48:26 +00:00
|
|
|
_, err := db.Exec(
|
|
|
|
"DELETE FROM musictrack "+
|
|
|
|
"WHERE id=$1",
|
2024-09-01 23:15:23 +00:00
|
|
|
trackID,
|
2024-08-02 21:48:26 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|