mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-06 10:23:21 +03:00
Refactor getArtist
This commit is contained in:
parent
ef81caf3ed
commit
e344f616b3
@ -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) {
|
func (c *BrowsingController) GetArtist(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||||
id := utils.ParamString(r, "id")
|
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 {
|
switch {
|
||||||
case err == model.ErrNotFound:
|
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")
|
return nil, NewError(responses.ErrorDataNotFound, "Artist not found")
|
||||||
case err != nil:
|
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")
|
return nil, NewError(responses.ErrorGeneric, "Internal Error")
|
||||||
}
|
}
|
||||||
|
|
||||||
response := NewResponse()
|
response := NewResponse()
|
||||||
response.ArtistWithAlbumsID3 = c.buildArtist(r.Context(), dir)
|
response.ArtistWithAlbumsID3 = c.buildArtist(ctx, artist, albums)
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,17 +242,16 @@ func (c *BrowsingController) buildDirectory(ctx context.Context, d *engine.Direc
|
|||||||
return dir
|
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 := &responses.ArtistWithAlbumsID3{}
|
||||||
dir.Id = d.Id
|
dir.Id = artist.ID
|
||||||
dir.Name = d.Name
|
dir.Name = artist.Name
|
||||||
dir.AlbumCount = d.AlbumCount
|
dir.AlbumCount = artist.AlbumCount
|
||||||
dir.CoverArt = d.CoverArt
|
if artist.Starred {
|
||||||
if !d.Starred.IsZero() {
|
dir.Starred = &artist.StarredAt
|
||||||
dir.Starred = &d.Starred
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dir.Album = ToAlbums(ctx, d.Entries)
|
dir.Album = ChildrenFromAlbums(ctx, albums)
|
||||||
return dir
|
return dir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,9 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Browser interface {
|
type Browser interface {
|
||||||
// Deprecated
|
|
||||||
Directory(ctx context.Context, id string) (*DirectoryInfo, error)
|
Directory(ctx context.Context, id string) (*DirectoryInfo, error)
|
||||||
Artist(ctx context.Context, id string) (*DirectoryInfo, error)
|
|
||||||
Album(ctx context.Context, id string) (*DirectoryInfo, error)
|
Album(ctx context.Context, id string) (*DirectoryInfo, error)
|
||||||
GetSong(ctx context.Context, id string) (*Entry, error)
|
GetSong(ctx context.Context, id string) (*Entry, error)
|
||||||
GetGenres(ctx context.Context) (model.Genres, error)
|
GetGenres(ctx context.Context) (model.Genres, error)
|
||||||
|
@ -215,3 +215,32 @@ func ChildrenFromMediaFiles(ctx context.Context, mfs model.MediaFiles) []respons
|
|||||||
}
|
}
|
||||||
return children
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user