Skip to content

🎧 Your Personal Streaming Service

Refactor `getArtist`

Deluane344f616b3b5
diff --git a/server/subsonic/browsing.go b/server/subsonic/browsing.go
index 2651e5df8..b84826d45 100644
--- a/server/subsonic/browsing.go
+++ b/server/subsonic/browsing.go
@@ -122,18 +122,26 @@ func (c *BrowsingController) GetMusicDirectory(w http.ResponseWriter, r *http.Re
func (c *BrowsingController) GetArtist(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := utils.ParamString(r, "id")
- dir, err := c.browser.Artist(r.Context(), id)
+ ctx := r.Context()
+
+ artist, err := c.ds.Artist(ctx).Get(id)
switch {
case err == model.ErrNotFound:
- log.Error(r, "Requested ArtistID not found ", "id", id)
+ log.Error(ctx, "Requested ArtistID not found ", "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Artist not found")
case err != nil:
- log.Error(r, err)
+ log.Error(ctx, "Error retrieving artist", "id", id, err)
+ return nil, NewError(responses.ErrorGeneric, "Internal Error")
+ }
+
+ albums, err := c.ds.Album(ctx).FindByArtist(id)
+ if err != nil {
+ log.Error(ctx, "Error retrieving albums by artist", "id", id, "name", artist.Name, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
response := NewResponse()
- response.ArtistWithAlbumsID3 = c.buildArtist(r.Context(), dir)
+ response.ArtistWithAlbumsID3 = c.buildArtist(ctx, artist, albums)
return response, nil
}
@@ -234,17 +242,16 @@ func (c *BrowsingController) buildDirectory(ctx context.Context, d *engine.Direc
return dir
}
-func (c *BrowsingController) buildArtist(ctx context.Context, d *engine.DirectoryInfo) *responses.ArtistWithAlbumsID3 {
+func (c *BrowsingController) buildArtist(ctx context.Context, artist *model.Artist, albums model.Albums) *responses.ArtistWithAlbumsID3 {
dir := &responses.ArtistWithAlbumsID3{}
- dir.Id = d.Id
- dir.Name = d.Name
- dir.AlbumCount = d.AlbumCount
- dir.CoverArt = d.CoverArt
- if !d.Starred.IsZero() {
- dir.Starred = &d.Starred
+ dir.Id = artist.ID
+ dir.Name = artist.Name
+ dir.AlbumCount = artist.AlbumCount
+ if artist.Starred {
+ dir.Starred = &artist.StarredAt
}
- dir.Album = ToAlbums(ctx, d.Entries)
+ dir.Album = ChildrenFromAlbums(ctx, albums)
return dir
}
diff --git a/server/subsonic/engine/browser.go b/server/subsonic/engine/browser.go
index d7b98e1dc..635d7483c 100644
--- a/server/subsonic/engine/browser.go
+++ b/server/subsonic/engine/browser.go
@@ -12,9 +12,7 @@ import (
)
type Browser interface {
- // Deprecated
Directory(ctx context.Context, id string) (*DirectoryInfo, error)
- Artist(ctx context.Context, id string) (*DirectoryInfo, error)
Album(ctx context.Context, id string) (*DirectoryInfo, error)
GetSong(ctx context.Context, id string) (*Entry, error)
GetGenres(ctx context.Context) (model.Genres, error)
diff --git a/server/subsonic/helpers.go b/server/subsonic/helpers.go
index 15950a2ba..8d253b30b 100644
--- a/server/subsonic/helpers.go
+++ b/server/subsonic/helpers.go
@@ -215,3 +215,32 @@ func ChildrenFromMediaFiles(ctx context.Context, mfs model.MediaFiles) []respons
}
return children
}
+
+func ChildFromAlbum(ctx context.Context, al model.Album) responses.Child {
+ child := responses.Child{}
+ child.Id = al.ID
+ child.IsDir = true
+ child.Name = al.Name
+ child.Artist = al.AlbumArtist
+ child.Year = al.MaxYear
+ child.Genre = al.Genre
+ child.CoverArt = al.CoverArtId
+ child.Created = &al.CreatedAt
+ child.ArtistId = al.AlbumArtistID
+ child.Duration = int(al.Duration)
+ child.SongCount = al.SongCount
+ if al.Starred {
+ child.Starred = &al.StarredAt
+ }
+ child.PlayCount = al.PlayCount
+ child.UserRating = al.Rating
+ return child
+}
+
+func ChildrenFromAlbums(ctx context.Context, als model.Albums) []responses.Child {
+ children := make([]responses.Child, len(als))
+ for i, al := range als {
+ children[i] = ChildFromAlbum(ctx, al)
+ }
+ return children
+}