Expand GetArtists with album details, download info, and generic MonitorState enum
This commit is contained in:
@@ -9,20 +9,20 @@ import (
|
||||
)
|
||||
|
||||
type Album struct {
|
||||
ID string
|
||||
ExternalID string
|
||||
ArtistID string
|
||||
Title string
|
||||
AlbumType string
|
||||
ReleaseDate *time.Time
|
||||
TotalTracks int
|
||||
TotalDiscs int
|
||||
Label string
|
||||
Genres []string
|
||||
CoverURL string
|
||||
IsMonitored bool
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ID string
|
||||
ExternalID string
|
||||
ArtistID string
|
||||
Title string
|
||||
AlbumType string
|
||||
ReleaseDate *time.Time
|
||||
TotalTracks int
|
||||
TotalDiscs int
|
||||
Label string
|
||||
Genres []string
|
||||
CoverURL string
|
||||
MonitorState MonitorState
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type AlbumRepository struct {
|
||||
@@ -35,7 +35,7 @@ func NewAlbumRepository(pool *pgxpool.Pool) *AlbumRepository {
|
||||
|
||||
func (r *AlbumRepository) Create(ctx context.Context, a *Album) error {
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`INSERT INTO albums (external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, is_monitored)
|
||||
`INSERT INTO albums (external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, monitor_state)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
ON CONFLICT (external_id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
@@ -46,8 +46,12 @@ func (r *AlbumRepository) Create(ctx context.Context, a *Album) error {
|
||||
label = EXCLUDED.label,
|
||||
genres = EXCLUDED.genres,
|
||||
cover_url = EXCLUDED.cover_url,
|
||||
monitor_state = CASE
|
||||
WHEN albums.monitor_state = 'excluded' THEN albums.monitor_state
|
||||
ELSE EXCLUDED.monitor_state
|
||||
END,
|
||||
updated_at = NOW()`,
|
||||
a.ExternalID, a.ArtistID, a.Title, a.AlbumType, a.ReleaseDate, a.TotalTracks, a.TotalDiscs, a.Label, a.Genres, a.CoverURL, a.IsMonitored,
|
||||
a.ExternalID, a.ArtistID, a.Title, a.AlbumType, a.ReleaseDate, a.TotalTracks, a.TotalDiscs, a.Label, a.Genres, a.CoverURL, a.MonitorState,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating album: %w", err)
|
||||
@@ -58,9 +62,9 @@ func (r *AlbumRepository) Create(ctx context.Context, a *Album) error {
|
||||
func (r *AlbumRepository) GetByExternalID(ctx context.Context, externalID string) (*Album, error) {
|
||||
a := &Album{}
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, is_monitored, created_at, updated_at
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, monitor_state, created_at, updated_at
|
||||
FROM albums WHERE external_id = $1`, externalID,
|
||||
).Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.IsMonitored, &a.CreatedAt, &a.UpdatedAt)
|
||||
).Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.MonitorState, &a.CreatedAt, &a.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting album: %w", err)
|
||||
}
|
||||
@@ -70,9 +74,9 @@ func (r *AlbumRepository) GetByExternalID(ctx context.Context, externalID string
|
||||
func (r *AlbumRepository) GetByID(ctx context.Context, id string) (*Album, error) {
|
||||
a := &Album{}
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, is_monitored, created_at, updated_at
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, monitor_state, created_at, updated_at
|
||||
FROM albums WHERE id = $1`, id,
|
||||
).Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.IsMonitored, &a.CreatedAt, &a.UpdatedAt)
|
||||
).Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.MonitorState, &a.CreatedAt, &a.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting album: %w", err)
|
||||
}
|
||||
@@ -81,7 +85,7 @@ func (r *AlbumRepository) GetByID(ctx context.Context, id string) (*Album, error
|
||||
|
||||
func (r *AlbumRepository) GetByArtistID(ctx context.Context, artistID string) ([]*Album, error) {
|
||||
rows, err := r.pool.Query(ctx,
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, is_monitored, created_at, updated_at
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, monitor_state, created_at, updated_at
|
||||
FROM albums WHERE artist_id = $1 ORDER BY release_date DESC`, artistID,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -92,7 +96,7 @@ func (r *AlbumRepository) GetByArtistID(ctx context.Context, artistID string) ([
|
||||
var albums []*Album
|
||||
for rows.Next() {
|
||||
a := &Album{}
|
||||
if err := rows.Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.IsMonitored, &a.CreatedAt, &a.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.MonitorState, &a.CreatedAt, &a.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scanning album: %w", err)
|
||||
}
|
||||
albums = append(albums, a)
|
||||
@@ -102,8 +106,8 @@ func (r *AlbumRepository) GetByArtistID(ctx context.Context, artistID string) ([
|
||||
|
||||
func (r *AlbumRepository) GetMonitored(ctx context.Context) ([]*Album, error) {
|
||||
rows, err := r.pool.Query(ctx,
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, is_monitored, created_at, updated_at
|
||||
FROM albums WHERE is_monitored = TRUE ORDER BY release_date DESC`,
|
||||
`SELECT id, external_id, artist_id, title, album_type, release_date, total_tracks, total_discs, label, genres, cover_url, monitor_state, created_at, updated_at
|
||||
FROM albums WHERE monitor_state = 'monitored' ORDER BY release_date DESC`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing monitored albums: %w", err)
|
||||
@@ -113,7 +117,7 @@ func (r *AlbumRepository) GetMonitored(ctx context.Context) ([]*Album, error) {
|
||||
var albums []*Album
|
||||
for rows.Next() {
|
||||
a := &Album{}
|
||||
if err := rows.Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.IsMonitored, &a.CreatedAt, &a.UpdatedAt); err != nil {
|
||||
if err := rows.Scan(&a.ID, &a.ExternalID, &a.ArtistID, &a.Title, &a.AlbumType, &a.ReleaseDate, &a.TotalTracks, &a.TotalDiscs, &a.Label, &a.Genres, &a.CoverURL, &a.MonitorState, &a.CreatedAt, &a.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scanning album: %w", err)
|
||||
}
|
||||
albums = append(albums, a)
|
||||
@@ -121,12 +125,12 @@ func (r *AlbumRepository) GetMonitored(ctx context.Context) ([]*Album, error) {
|
||||
return albums, nil
|
||||
}
|
||||
|
||||
func (r *AlbumRepository) SetMonitored(ctx context.Context, id string, monitored bool) error {
|
||||
func (r *AlbumRepository) SetMonitorState(ctx context.Context, id string, state MonitorState) error {
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`UPDATE albums SET is_monitored = $1, updated_at = NOW() WHERE id = $2`, monitored, id,
|
||||
`UPDATE albums SET monitor_state = $1, updated_at = NOW() WHERE id = $2`, state, id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("updating monitored state: %w", err)
|
||||
return fmt.Errorf("updating monitor state: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,14 +8,6 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type ArtistMonitorState string
|
||||
|
||||
const (
|
||||
ArtistMonitored ArtistMonitorState = "monitored"
|
||||
ArtistUnmonitored ArtistMonitorState = "unmonitored"
|
||||
ArtistExcluded ArtistMonitorState = "excluded"
|
||||
)
|
||||
|
||||
type Artist struct {
|
||||
ID string
|
||||
ExternalID string
|
||||
@@ -24,7 +16,7 @@ type Artist struct {
|
||||
Country string
|
||||
Genres []string
|
||||
ImageURL string
|
||||
MonitorState ArtistMonitorState
|
||||
MonitorState MonitorState
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
@@ -8,6 +8,14 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type MonitorState string
|
||||
|
||||
const (
|
||||
Monitored MonitorState = "monitored"
|
||||
Unmonitored MonitorState = "unmonitored"
|
||||
Excluded MonitorState = "excluded"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
Pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
@@ -42,6 +42,16 @@ func (s *MetadataService) GetAlbum(ctx context.Context, albumID string) (*metada
|
||||
return album, nil
|
||||
}
|
||||
|
||||
func (s *MetadataService) GetArtistAlbums(ctx context.Context, artistExternalID string) ([]*metadataPb.Album, error) {
|
||||
resp, err := s.client.GetArtistAlbums(ctx, &metadataPb.GetArtistAlbumsRequest{
|
||||
ArtistId: artistExternalID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetching artist albums: %w", err)
|
||||
}
|
||||
return resp.GetAlbums(), nil
|
||||
}
|
||||
|
||||
func (s *MetadataService) GetArtistByExternalID(ctx context.Context, externalID string) (*database.Artist, error) {
|
||||
return s.artists.GetByExternalID(ctx, externalID)
|
||||
}
|
||||
@@ -50,6 +60,10 @@ func (s *MetadataService) GetAlbumByExternalID(ctx context.Context, externalID s
|
||||
return s.albums.GetByExternalID(ctx, externalID)
|
||||
}
|
||||
|
||||
func (s *MetadataService) GetAlbumsByArtistID(ctx context.Context, artistID string) ([]*database.Album, error) {
|
||||
return s.albums.GetByArtistID(ctx, artistID)
|
||||
}
|
||||
|
||||
func (s *MetadataService) persistArtist(ctx context.Context, album *metadataPb.Album) {
|
||||
if len(album.GetArtists()) == 0 {
|
||||
return
|
||||
@@ -68,7 +82,7 @@ func (s *MetadataService) persistArtist(ctx context.Context, album *metadataPb.A
|
||||
Country: artist.GetCountry(),
|
||||
Genres: genres,
|
||||
ImageURL: artist.GetImageUrl(),
|
||||
MonitorState: database.ArtistMonitored,
|
||||
MonitorState: database.Monitored,
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("name", artist.GetName()).Msg("failed to persist artist")
|
||||
@@ -100,16 +114,16 @@ func (s *MetadataService) persistAlbum(ctx context.Context, album *metadataPb.Al
|
||||
}
|
||||
|
||||
err := s.albums.Create(ctx, &database.Album{
|
||||
ExternalID: album.GetId(),
|
||||
ArtistID: artistID,
|
||||
Title: album.GetTitle(),
|
||||
AlbumType: album.GetAlbumType(),
|
||||
TotalTracks: int(album.GetTotalTracks()),
|
||||
TotalDiscs: int(album.GetTotalDiscs()),
|
||||
Label: labelName,
|
||||
Genres: genres,
|
||||
CoverURL: album.GetCoverUrl(),
|
||||
IsMonitored: true,
|
||||
ExternalID: album.GetId(),
|
||||
ArtistID: artistID,
|
||||
Title: album.GetTitle(),
|
||||
AlbumType: album.GetAlbumType(),
|
||||
TotalTracks: int(album.GetTotalTracks()),
|
||||
TotalDiscs: int(album.GetTotalDiscs()),
|
||||
Label: labelName,
|
||||
Genres: genres,
|
||||
CoverURL: album.GetCoverUrl(),
|
||||
MonitorState: database.Monitored,
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("title", album.GetTitle()).Msg("failed to persist album")
|
||||
|
||||
+74
-8
@@ -96,6 +96,11 @@ func (service *MusicAgregatorService) GetArtists(ctx context.Context, _ *pb.GetA
|
||||
|
||||
artists := make([]*pb.ArtistSummary, 0, len(dbArtists))
|
||||
for _, a := range dbArtists {
|
||||
albums, err := service.buildAlbumsForArtist(ctx, a)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("artist", a.Name).Msg("failed to build album details, returning artist without albums")
|
||||
}
|
||||
|
||||
artists = append(artists, &pb.ArtistSummary{
|
||||
Id: a.ID,
|
||||
ExternalId: a.ExternalID,
|
||||
@@ -105,12 +110,73 @@ func (service *MusicAgregatorService) GetArtists(ctx context.Context, _ *pb.GetA
|
||||
Genres: a.Genres,
|
||||
ImageUrl: a.ImageURL,
|
||||
MonitorState: toProtoMonitorState(a.MonitorState),
|
||||
Albums: albums,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.GetArtistsResponse{Artists: artists}, nil
|
||||
}
|
||||
|
||||
func (service *MusicAgregatorService) buildAlbumsForArtist(ctx context.Context, artist *database.Artist) ([]*pb.AlbumDetail, error) {
|
||||
metadataAlbums, err := service.metadata.GetArtistAlbums(ctx, artist.ExternalID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetching metadata albums: %w", err)
|
||||
}
|
||||
|
||||
dbAlbums, err := service.metadata.GetAlbumsByArtistID(ctx, artist.ID)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("artist_id", artist.ID).Msg("failed to get local albums")
|
||||
dbAlbums = nil
|
||||
}
|
||||
|
||||
dbAlbumsByExternalID := make(map[string]*database.Album, len(dbAlbums))
|
||||
for _, a := range dbAlbums {
|
||||
dbAlbumsByExternalID[a.ExternalID] = a
|
||||
}
|
||||
|
||||
albums := make([]*pb.AlbumDetail, 0, len(metadataAlbums))
|
||||
for _, ma := range metadataAlbums {
|
||||
detail := &pb.AlbumDetail{
|
||||
ExternalId: ma.GetId(),
|
||||
Title: ma.GetTitle(),
|
||||
AlbumType: ma.GetAlbumType(),
|
||||
ReleaseDate: ma.GetReleaseDate(),
|
||||
TotalTracks: ma.GetTotalTracks(),
|
||||
TotalDiscs: ma.GetTotalDiscs(),
|
||||
CoverUrl: ma.GetCoverUrl(),
|
||||
}
|
||||
|
||||
if ma.GetLabel() != nil {
|
||||
detail.Label = ma.GetLabel().GetName()
|
||||
}
|
||||
for _, g := range ma.GetGenres() {
|
||||
detail.Genres = append(detail.Genres, g.GetName())
|
||||
}
|
||||
|
||||
if dbAlbum, ok := dbAlbumsByExternalID[ma.GetId()]; ok {
|
||||
detail.Id = dbAlbum.ID
|
||||
detail.MonitorState = toProtoMonitorState(dbAlbum.MonitorState)
|
||||
|
||||
downloads, err := service.downloads.GetByAlbumID(ctx, dbAlbum.ID)
|
||||
if err == nil && len(downloads) > 0 {
|
||||
best := downloads[0]
|
||||
detail.Download = &pb.DownloadInfo{
|
||||
State: best.State,
|
||||
Format: best.Format,
|
||||
Quality: best.Quality,
|
||||
SavePath: best.SavePath,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
detail.MonitorState = pb.MonitorState_MONITOR_STATE_UNMONITORED
|
||||
}
|
||||
|
||||
albums = append(albums, detail)
|
||||
}
|
||||
|
||||
return albums, nil
|
||||
}
|
||||
|
||||
func (service *MusicAgregatorService) MonitorAlbum(ctx context.Context, req *pb.MonitorAlbumRequest) (*pb.MonitorAlbumResponse, error) {
|
||||
album, err := service.metadata.GetAlbum(ctx, req.GetAlbumId())
|
||||
if err != nil {
|
||||
@@ -404,16 +470,16 @@ func buildMonitoredRelease(p parsedItem) *pb.MonitoredRelease {
|
||||
}
|
||||
}
|
||||
|
||||
func toProtoMonitorState(state database.ArtistMonitorState) pb.ArtistMonitorState {
|
||||
func toProtoMonitorState(state database.MonitorState) pb.MonitorState {
|
||||
switch state {
|
||||
case database.ArtistMonitored:
|
||||
return pb.ArtistMonitorState_ARTIST_MONITOR_STATE_MONITORED
|
||||
case database.ArtistUnmonitored:
|
||||
return pb.ArtistMonitorState_ARTIST_MONITOR_STATE_UNMONITORED
|
||||
case database.ArtistExcluded:
|
||||
return pb.ArtistMonitorState_ARTIST_MONITOR_STATE_EXCLUDED
|
||||
case database.Monitored:
|
||||
return pb.MonitorState_MONITOR_STATE_MONITORED
|
||||
case database.Unmonitored:
|
||||
return pb.MonitorState_MONITOR_STATE_UNMONITORED
|
||||
case database.Excluded:
|
||||
return pb.MonitorState_MONITOR_STATE_EXCLUDED
|
||||
default:
|
||||
return pb.ArtistMonitorState_ARTIST_MONITOR_STATE_UNSPECIFIED
|
||||
return pb.MonitorState_MONITOR_STATE_UNSPECIFIED
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user