Add GetArtists RPC with artist monitor state (monitored/unmonitored/excluded)
This commit is contained in:
@@ -8,16 +8,25 @@ 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
|
||||
Name string
|
||||
ArtistType string
|
||||
Country string
|
||||
Genres []string
|
||||
ImageURL string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ID string
|
||||
ExternalID string
|
||||
Name string
|
||||
ArtistType string
|
||||
Country string
|
||||
Genres []string
|
||||
ImageURL string
|
||||
MonitorState ArtistMonitorState
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type ArtistRepository struct {
|
||||
@@ -30,17 +39,21 @@ func NewArtistRepository(pool *pgxpool.Pool) *ArtistRepository {
|
||||
|
||||
func (r *ArtistRepository) Create(ctx context.Context, a *Artist) error {
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`INSERT INTO artists (external_id, name, artist_type, country, genres, image_url)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
`INSERT INTO artists (external_id, name, artist_type, country, genres, image_url, monitor_state)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (external_id) DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
artist_type = EXCLUDED.artist_type,
|
||||
country = EXCLUDED.country,
|
||||
genres = EXCLUDED.genres,
|
||||
image_url = EXCLUDED.image_url,
|
||||
monitor_state = CASE
|
||||
WHEN artists.monitor_state = 'excluded' THEN artists.monitor_state
|
||||
ELSE EXCLUDED.monitor_state
|
||||
END,
|
||||
updated_at = NOW()
|
||||
RETURNING id, created_at, updated_at`,
|
||||
a.ExternalID, a.Name, a.ArtistType, a.Country, a.Genres, a.ImageURL,
|
||||
a.ExternalID, a.Name, a.ArtistType, a.Country, a.Genres, a.ImageURL, a.MonitorState,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating artist: %w", err)
|
||||
@@ -51,21 +64,42 @@ func (r *ArtistRepository) Create(ctx context.Context, a *Artist) error {
|
||||
func (r *ArtistRepository) GetByExternalID(ctx context.Context, externalID string) (*Artist, error) {
|
||||
a := &Artist{}
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, external_id, name, artist_type, country, genres, image_url, created_at, updated_at
|
||||
`SELECT id, external_id, name, artist_type, country, genres, image_url, monitor_state, created_at, updated_at
|
||||
FROM artists WHERE external_id = $1`, externalID,
|
||||
).Scan(&a.ID, &a.ExternalID, &a.Name, &a.ArtistType, &a.Country, &a.Genres, &a.ImageURL, &a.CreatedAt, &a.UpdatedAt)
|
||||
).Scan(&a.ID, &a.ExternalID, &a.Name, &a.ArtistType, &a.Country, &a.Genres, &a.ImageURL, &a.MonitorState, &a.CreatedAt, &a.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting artist: %w", err)
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (r *ArtistRepository) GetAll(ctx context.Context) ([]*Artist, error) {
|
||||
rows, err := r.pool.Query(ctx,
|
||||
`SELECT id, external_id, name, artist_type, country, genres, image_url, monitor_state, created_at, updated_at
|
||||
FROM artists ORDER BY name ASC`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing artists: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var artists []*Artist
|
||||
for rows.Next() {
|
||||
a := &Artist{}
|
||||
if err := rows.Scan(&a.ID, &a.ExternalID, &a.Name, &a.ArtistType, &a.Country, &a.Genres, &a.ImageURL, &a.MonitorState, &a.CreatedAt, &a.UpdatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scanning artist: %w", err)
|
||||
}
|
||||
artists = append(artists, a)
|
||||
}
|
||||
return artists, nil
|
||||
}
|
||||
|
||||
func (r *ArtistRepository) GetByID(ctx context.Context, id string) (*Artist, error) {
|
||||
a := &Artist{}
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, external_id, name, artist_type, country, genres, image_url, created_at, updated_at
|
||||
`SELECT id, external_id, name, artist_type, country, genres, image_url, monitor_state, created_at, updated_at
|
||||
FROM artists WHERE id = $1`, id,
|
||||
).Scan(&a.ID, &a.ExternalID, &a.Name, &a.ArtistType, &a.Country, &a.Genres, &a.ImageURL, &a.CreatedAt, &a.UpdatedAt)
|
||||
).Scan(&a.ID, &a.ExternalID, &a.Name, &a.ArtistType, &a.Country, &a.Genres, &a.ImageURL, &a.MonitorState, &a.CreatedAt, &a.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting artist: %w", err)
|
||||
}
|
||||
|
||||
@@ -62,12 +62,13 @@ func (s *MetadataService) persistArtist(ctx context.Context, album *metadataPb.A
|
||||
}
|
||||
|
||||
err := s.artists.Create(ctx, &database.Artist{
|
||||
ExternalID: artist.GetId(),
|
||||
Name: artist.GetName(),
|
||||
ArtistType: artist.GetArtistType(),
|
||||
Country: artist.GetCountry(),
|
||||
Genres: genres,
|
||||
ImageURL: artist.GetImageUrl(),
|
||||
ExternalID: artist.GetId(),
|
||||
Name: artist.GetName(),
|
||||
ArtistType: artist.GetArtistType(),
|
||||
Country: artist.GetCountry(),
|
||||
Genres: genres,
|
||||
ImageURL: artist.GetImageUrl(),
|
||||
MonitorState: database.ArtistMonitored,
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("name", artist.GetName()).Msg("failed to persist artist")
|
||||
|
||||
@@ -29,6 +29,10 @@ func NewMusicAgregatorServer(cfg config.Config, riverClient *river.Client[pgx.Tx
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MusicAgregatorServer) GetArtists(ctx context.Context, req *pb.GetArtistsRequest) (*pb.GetArtistsResponse, error) {
|
||||
return s.service.GetArtists(ctx, req)
|
||||
}
|
||||
|
||||
func (s *MusicAgregatorServer) MonitorAlbum(ctx context.Context, req *pb.MonitorAlbumRequest) (*pb.MonitorAlbumResponse, error) {
|
||||
return s.service.MonitorAlbum(ctx, req)
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ type MusicAgregatorService struct {
|
||||
riverClient *river.Client[pgx.Tx]
|
||||
torrents *database.TorrentRepository
|
||||
downloads *database.DownloadRepository
|
||||
artists *database.ArtistRepository
|
||||
}
|
||||
|
||||
func NewMusicAgregatorService(cfg config.Config, riverClient *river.Client[pgx.Tx], db *database.DB) (*MusicAgregatorService, error) {
|
||||
@@ -76,6 +77,7 @@ func NewMusicAgregatorService(cfg config.Config, riverClient *river.Client[pgx.T
|
||||
riverClient: riverClient,
|
||||
torrents: database.NewTorrentRepository(db.Pool),
|
||||
downloads: database.NewDownloadRepository(db.Pool),
|
||||
artists: database.NewArtistRepository(db.Pool),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -85,6 +87,30 @@ func (s *MusicAgregatorService) Close() {
|
||||
}
|
||||
}
|
||||
|
||||
func (service *MusicAgregatorService) GetArtists(ctx context.Context, _ *pb.GetArtistsRequest) (*pb.GetArtistsResponse, error) {
|
||||
dbArtists, err := service.artists.GetAll(ctx)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to list artists")
|
||||
return nil, fmt.Errorf("listing artists: %w", err)
|
||||
}
|
||||
|
||||
artists := make([]*pb.ArtistSummary, 0, len(dbArtists))
|
||||
for _, a := range dbArtists {
|
||||
artists = append(artists, &pb.ArtistSummary{
|
||||
Id: a.ID,
|
||||
ExternalId: a.ExternalID,
|
||||
Name: a.Name,
|
||||
ArtistType: a.ArtistType,
|
||||
Country: a.Country,
|
||||
Genres: a.Genres,
|
||||
ImageUrl: a.ImageURL,
|
||||
MonitorState: toProtoMonitorState(a.MonitorState),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.GetArtistsResponse{Artists: artists}, 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 {
|
||||
@@ -378,6 +404,19 @@ func buildMonitoredRelease(p parsedItem) *pb.MonitoredRelease {
|
||||
}
|
||||
}
|
||||
|
||||
func toProtoMonitorState(state database.ArtistMonitorState) pb.ArtistMonitorState {
|
||||
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
|
||||
default:
|
||||
return pb.ArtistMonitorState_ARTIST_MONITOR_STATE_UNSPECIFIED
|
||||
}
|
||||
}
|
||||
|
||||
func downloadTorrentData(url string) ([]byte, error) {
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
resp, err := client.Get(url)
|
||||
|
||||
Reference in New Issue
Block a user