92 lines
3.5 KiB
Go
92 lines
3.5 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type AlbumRelease struct {
|
|
ID string
|
|
AlbumID string
|
|
DownloadID string
|
|
Format string
|
|
BitDepth *int
|
|
SampleRate *int
|
|
Channels int
|
|
IsLossless bool
|
|
Source *string
|
|
TotalSize int64
|
|
TotalDurationMs int
|
|
TrackCount int
|
|
HasCoverArt bool
|
|
HasCueSheet bool
|
|
HasRipLog bool
|
|
Path string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
type AlbumReleaseRepository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewAlbumReleaseRepository(pool *pgxpool.Pool) *AlbumReleaseRepository {
|
|
return &AlbumReleaseRepository{pool: pool}
|
|
}
|
|
|
|
func (r *AlbumReleaseRepository) Create(ctx context.Context, ar *AlbumRelease) error {
|
|
err := r.pool.QueryRow(ctx,
|
|
`INSERT INTO album_releases (album_id, download_id, format, bit_depth, sample_rate, channels, is_lossless, source, total_size, total_duration_ms, track_count, has_cover_art, has_cue_sheet, has_rip_log, path)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
|
RETURNING id, created_at`,
|
|
ar.AlbumID, ar.DownloadID, ar.Format, ar.BitDepth, ar.SampleRate, ar.Channels, ar.IsLossless, ar.Source, ar.TotalSize, ar.TotalDurationMs, ar.TrackCount, ar.HasCoverArt, ar.HasCueSheet, ar.HasRipLog, ar.Path,
|
|
).Scan(&ar.ID, &ar.CreatedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("creating album release: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *AlbumReleaseRepository) GetByAlbumID(ctx context.Context, albumID string) ([]*AlbumRelease, error) {
|
|
rows, err := r.pool.Query(ctx,
|
|
`SELECT id, album_id, download_id, format, bit_depth, sample_rate, channels, is_lossless, source, total_size, total_duration_ms, track_count, has_cover_art, has_cue_sheet, has_rip_log, path, created_at
|
|
FROM album_releases WHERE album_id = $1 ORDER BY created_at DESC`, albumID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("listing album releases: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var releases []*AlbumRelease
|
|
for rows.Next() {
|
|
ar := &AlbumRelease{}
|
|
if err := rows.Scan(&ar.ID, &ar.AlbumID, &ar.DownloadID, &ar.Format, &ar.BitDepth, &ar.SampleRate, &ar.Channels, &ar.IsLossless, &ar.Source, &ar.TotalSize, &ar.TotalDurationMs, &ar.TrackCount, &ar.HasCoverArt, &ar.HasCueSheet, &ar.HasRipLog, &ar.Path, &ar.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("scanning album release: %w", err)
|
|
}
|
|
releases = append(releases, ar)
|
|
}
|
|
return releases, nil
|
|
}
|
|
|
|
func (r *AlbumReleaseRepository) GetByDownloadID(ctx context.Context, downloadID string) (*AlbumRelease, error) {
|
|
ar := &AlbumRelease{}
|
|
err := r.pool.QueryRow(ctx,
|
|
`SELECT id, album_id, download_id, format, bit_depth, sample_rate, channels, is_lossless, source, total_size, total_duration_ms, track_count, has_cover_art, has_cue_sheet, has_rip_log, path, created_at
|
|
FROM album_releases WHERE download_id = $1`, downloadID,
|
|
).Scan(&ar.ID, &ar.AlbumID, &ar.DownloadID, &ar.Format, &ar.BitDepth, &ar.SampleRate, &ar.Channels, &ar.IsLossless, &ar.Source, &ar.TotalSize, &ar.TotalDurationMs, &ar.TrackCount, &ar.HasCoverArt, &ar.HasCueSheet, &ar.HasRipLog, &ar.Path, &ar.CreatedAt)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("getting album release by download: %w", err)
|
|
}
|
|
return ar, nil
|
|
}
|
|
|
|
func (r *AlbumReleaseRepository) DeleteByDownloadID(ctx context.Context, downloadID string) error {
|
|
_, err := r.pool.Exec(ctx, `DELETE FROM album_releases WHERE download_id = $1`, downloadID)
|
|
if err != nil {
|
|
return fmt.Errorf("deleting album release by download: %w", err)
|
|
}
|
|
return nil
|
|
}
|