arimelody.me/music/controller/track.go

138 lines
3 KiB
Go
Raw Normal View History

package music
import (
"arimelody.me/arimelody.me/music/model"
"github.com/jmoiron/sqlx"
)
// DATABASE
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) {
var tracks = []*model.Track{}
err := db.Select(&tracks, "SELECT * FROM musictrack WHERE id NOT IN (SELECT track FROM musicreleasetrack)")
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) {
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)
if err != nil {
return nil, err
}
return tracks, nil
}
2024-09-01 23:15:23 +00:00
func GetTrackReleases(db *sqlx.DB, trackID string) ([]model.ReleaseShorthand, error) {
var releases = []model.ReleaseShorthand{}
err := db.Select(&releases,
2024-09-01 23:15:23 +00:00
"SELECT id,title,type,release_date,artwork,buylink FROM musicrelease "+
"JOIN musicreleasetrack ON release=id "+
"WHERE track=$1 "+
"ORDER BY release_date",
2024-09-01 23:15:23 +00:00
trackID,
)
if err != nil {
return nil, err
}
return releases, nil
}
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
}
func CreateTrack(db *sqlx.DB, track *model.Track) (string, error) {
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
}
func UpdateTrack(db *sqlx.DB, track *model.Track) error {
_, err := db.Exec(
"UPDATE musictrack "+
"SET title=$2, description=$3, lyrics=$4, preview_url=$5 "+
"WHERE id=$1",
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 {
_, err := db.Exec(
"DELETE FROM musictrack "+
"WHERE id=$1",
2024-09-01 23:15:23 +00:00
trackID,
)
if err != nil {
return err
}
return nil
}