Add database schema, ERD, and repository layer
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type AlbumRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAlbumRepository(pool *pgxpool.Pool) *AlbumRepository {
|
||||
return &AlbumRepository{pool: pool}
|
||||
}
|
||||
|
||||
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)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
ON CONFLICT (external_id) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
album_type = EXCLUDED.album_type,
|
||||
release_date = EXCLUDED.release_date,
|
||||
total_tracks = EXCLUDED.total_tracks,
|
||||
total_discs = EXCLUDED.total_discs,
|
||||
label = EXCLUDED.label,
|
||||
genres = EXCLUDED.genres,
|
||||
cover_url = EXCLUDED.cover_url,
|
||||
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,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating album: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting album: %w", err)
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
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
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("getting album: %w", err)
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
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
|
||||
FROM albums WHERE artist_id = $1 ORDER BY release_date DESC`, artistID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing albums: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
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 {
|
||||
return nil, fmt.Errorf("scanning album: %w", err)
|
||||
}
|
||||
albums = append(albums, a)
|
||||
}
|
||||
return albums, nil
|
||||
}
|
||||
|
||||
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`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing monitored albums: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
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 {
|
||||
return nil, fmt.Errorf("scanning album: %w", err)
|
||||
}
|
||||
albums = append(albums, a)
|
||||
}
|
||||
return albums, nil
|
||||
}
|
||||
|
||||
func (r *AlbumRepository) SetMonitored(ctx context.Context, id string, monitored bool) error {
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`UPDATE albums SET is_monitored = $1, updated_at = NOW() WHERE id = $2`, monitored, id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("updating monitored state: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user