add more detail to credits on /api/v1/artist/{id}

This commit is contained in:
ari melody 2024-11-01 19:15:19 +00:00
parent 9602918a1a
commit f0d29126ab
Signed by: ari
GPG key ID: CF99829C92678188
6 changed files with 41 additions and 21 deletions

View file

@ -7,7 +7,7 @@ tmp_dir = "tmp"
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["admin\\static", "public", "uploads", "test"]
exclude_dir = ["admin/static", "public", "uploads", "test", "db"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false

View file

@ -25,7 +25,7 @@ func serveArtist() http.Handler {
return
}
credits, err := music.GetArtistCredits(global.DB, artist.ID)
credits, err := music.GetArtistCredits(global.DB, artist.ID, true)
if err != nil {
fmt.Printf("Error rendering admin track page for %s: %s\n", id, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -8,7 +8,9 @@ import (
"os"
"path/filepath"
"strings"
"time"
"arimelody-web/admin"
"arimelody-web/global"
db "arimelody-web/music/controller"
music "arimelody-web/music/controller"
@ -37,8 +39,12 @@ func ServeArtist(artist *model.Artist) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type (
creditJSON struct {
Role string `json:"role"`
Primary bool `json:"primary"`
ID string `json:"id"`
Title string `json:"title"`
ReleaseDate time.Time `json:"releaseDate" db:"release_date"`
Artwork string `json:"artwork"`
Role string `json:"role"`
Primary bool `json:"primary"`
}
artistJSON struct {
*model.Artist
@ -46,8 +52,10 @@ func ServeArtist(artist *model.Artist) http.Handler {
}
)
show_hidden_releases := admin.GetSession(r) != nil
var dbCredits []*model.Credit
dbCredits, err := db.GetArtistCredits(global.DB, artist.ID)
dbCredits, err := db.GetArtistCredits(global.DB, artist.ID, show_hidden_releases)
if err != nil {
fmt.Printf("FATAL: Failed to retrieve artist credits for %s: %s\n", artist.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -57,6 +65,10 @@ func ServeArtist(artist *model.Artist) http.Handler {
var credits = map[string]creditJSON{}
for _, credit := range dbCredits {
credits[credit.Release.ID] = creditJSON{
ID: credit.Release.ID,
Title: credit.Release.Title,
ReleaseDate: credit.Release.ReleaseDate,
Artwork: credit.Release.Artwork,
Role: credit.Role,
Primary: credit.Primary,
}

View file

@ -27,6 +27,7 @@ func ServeCatalog() http.Handler {
type Release struct {
ID string `json:"id"`
Title string `json:"title"`
Artists []string `json:"artists"`
ReleaseType model.ReleaseType `json:"type" db:"type"`
ReleaseDate time.Time `json:"releaseDate" db:"release_date"`
Artwork string `json:"artwork"`
@ -40,9 +41,15 @@ func ServeCatalog() http.Handler {
if !release.Visible && !authorised {
continue
}
artists := []string{}
for _, credit := range release.Credits {
if !credit.Primary { continue }
artists = append(artists, credit.Artist.Name)
}
catalog = append(catalog, Release{
ID: release.ID,
Title: release.Title,
Artists: artists,
ReleaseType: release.ReleaseType,
ReleaseDate: release.ReleaseDate,
Artwork: release.Artwork,

View file

@ -44,15 +44,15 @@ func GetArtistsNotOnRelease(db *sqlx.DB, releaseID string) ([]*model.Artist, err
return artists, nil
}
func GetArtistCredits(db *sqlx.DB, artistID string) ([]*model.Credit, error) {
rows, err := db.Query(
"SELECT release.id,release.title,release.artwork,artist.id,artist.name,artist.website,artist.avatar,role,is_primary "+
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 "+
"FROM musiccredit "+
"JOIN musicrelease AS release ON release=release.id "+
"JOIN artist ON artist=artist.id "+
"WHERE artist=$1 "+
"ORDER BY release_date DESC",
artistID)
"WHERE artist=$1 "
if !show_hidden { query += "AND visible=true " }
query += "ORDER BY release_date DESC"
rows, err := db.Query(query, artistID)
if err != nil {
return nil, err
}
@ -69,6 +69,7 @@ func GetArtistCredits(db *sqlx.DB, artistID string) ([]*model.Credit, error) {
&credit.Release.ID,
&credit.Release.Title,
&credit.Release.Artwork,
&credit.Release.ReleaseDate,
&credit.Artist.ID,
&credit.Artist.Name,
&credit.Artist.Website,

View file

@ -66,17 +66,17 @@ func GetAllReleases(db *sqlx.DB, onlyVisible bool, limit int, full bool) ([]*mod
return nil, err
}
if full {
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)
}
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)
}
if full {
// get tracks
tracks, err := GetReleaseTracks(db, release.ID)
if err != nil {