From e69cf78e57e2bc2dc11d9f073e0fe8e08bcba6f3 Mon Sep 17 00:00:00 2001 From: ari melody Date: Thu, 12 Sep 2024 09:46:40 +0100 Subject: [PATCH 1/3] add artists list to /api/v1/music --- .air.toml | 2 +- api/release.go | 7 +++++++ music/controller/release.go | 20 ++++++++++---------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/.air.toml b/.air.toml index ea2b4d5..f8a1acf 100644 --- a/.air.toml +++ b/.air.toml @@ -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 diff --git a/api/release.go b/api/release.go index 763383f..3027a8c 100644 --- a/api/release.go +++ b/api/release.go @@ -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, diff --git a/music/controller/release.go b/music/controller/release.go index 4bd2cd6..2aeb236 100644 --- a/music/controller/release.go +++ b/music/controller/release.go @@ -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 { From 1846203076f5995fd3aaaf4b5c9e63fe3957ceac Mon Sep 17 00:00:00 2001 From: ari melody Date: Thu, 12 Sep 2024 09:56:22 +0100 Subject: [PATCH 2/3] hide hidden releases from unauthorised /api/v1/artist/{id} --- admin/artisthttp.go | 2 +- api/artist.go | 5 ++++- music/controller/artist.go | 12 ++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/admin/artisthttp.go b/admin/artisthttp.go index 31a7c9c..4f66798 100644 --- a/admin/artisthttp.go +++ b/admin/artisthttp.go @@ -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) diff --git a/api/artist.go b/api/artist.go index 2d5f5e6..00f185d 100644 --- a/api/artist.go +++ b/api/artist.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strings" + "arimelody-web/admin" "arimelody-web/global" db "arimelody-web/music/controller" music "arimelody-web/music/controller" @@ -46,8 +47,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) diff --git a/music/controller/artist.go b/music/controller/artist.go index 2ae92e7..bc9e656 100644 --- a/music/controller/artist.go +++ b/music/controller/artist.go @@ -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,release.title,release.artwork,artist.id,artist.name,artist.website,artist.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 } From e2ec7311091b7d540217085b4720626e13b9e47e Mon Sep 17 00:00:00 2001 From: ari melody Date: Sun, 20 Oct 2024 20:05:30 +0100 Subject: [PATCH 3/3] add more detail to credits on /api/v1/artist/{id} --- api/artist.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/api/artist.go b/api/artist.go index 00f185d..90bd18a 100644 --- a/api/artist.go +++ b/api/artist.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "strings" + "time" "arimelody-web/admin" "arimelody-web/global" @@ -38,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 @@ -60,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, }