feat(api): add dbq→ref projection helpers
This commit is contained in:
@@ -2,6 +2,8 @@ package api
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
)
|
||||
|
||||
// uuidToString renders a pgtype.UUID as a canonical hyphenated string.
|
||||
@@ -65,3 +67,51 @@ func coverURL(albumID pgtype.UUID) string {
|
||||
func streamURL(trackID pgtype.UUID) string {
|
||||
return "/api/tracks/" + uuidToString(trackID) + "/stream"
|
||||
}
|
||||
|
||||
// artistRefFrom projects a dbq.Artist into an ArtistRef. albumCount must be
|
||||
// pre-computed by the caller (one query per artist in lists).
|
||||
func artistRefFrom(a dbq.Artist, albumCount int) ArtistRef {
|
||||
return ArtistRef{
|
||||
ID: uuidToString(a.ID),
|
||||
Name: a.Name,
|
||||
AlbumCount: albumCount,
|
||||
}
|
||||
}
|
||||
|
||||
// albumRefFrom projects a dbq.Album into an AlbumRef. artistName must be
|
||||
// pre-resolved because Album only carries artist_id. trackCount and
|
||||
// durationSec may be 0 when the caller does not compute them.
|
||||
func albumRefFrom(a dbq.Album, artistName string, trackCount, durationSec int) AlbumRef {
|
||||
return AlbumRef{
|
||||
ID: uuidToString(a.ID),
|
||||
Title: a.Title,
|
||||
ArtistID: uuidToString(a.ArtistID),
|
||||
ArtistName: artistName,
|
||||
Year: yearFromDate(a.ReleaseDate),
|
||||
TrackCount: trackCount,
|
||||
DurationSec: durationSec,
|
||||
CoverURL: coverURL(a.ID),
|
||||
}
|
||||
}
|
||||
|
||||
// trackRefFrom projects a dbq.Track into a TrackRef. Parent names must be
|
||||
// pre-resolved because Track only carries album_id and artist_id.
|
||||
func trackRefFrom(t dbq.Track, albumTitle, artistName string) TrackRef {
|
||||
ref := TrackRef{
|
||||
ID: uuidToString(t.ID),
|
||||
Title: t.Title,
|
||||
AlbumID: uuidToString(t.AlbumID),
|
||||
AlbumTitle: albumTitle,
|
||||
ArtistID: uuidToString(t.ArtistID),
|
||||
ArtistName: artistName,
|
||||
DurationSec: durationMsToSec(t.DurationMs),
|
||||
StreamURL: streamURL(t.ID),
|
||||
}
|
||||
if t.TrackNumber != nil {
|
||||
ref.TrackNumber = int(*t.TrackNumber)
|
||||
}
|
||||
if t.DiscNumber != nil {
|
||||
ref.DiscNumber = int(*t.DiscNumber)
|
||||
}
|
||||
return ref
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user