Refactor `getArtist`

This commit is contained in:
Deluan 2020-08-13 18:37:54 -04:00 committed by Deluan Quintão
parent ef81caf3ed
commit e344f616b3
3 changed files with 48 additions and 14 deletions

View File

@ -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
}

View File

@ -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)

View File

@ -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
}