refactor(server/sql): unify *ForUser track queries via nullable user_id (A1)

This commit is contained in:
2026-05-08 08:15:19 -04:00
parent b1ef3c3688
commit bffa397250
7 changed files with 49 additions and 163 deletions
+17 -115
View File
@@ -37,17 +37,6 @@ func (q *Queries) CountTracksByArtist(ctx context.Context, artistID pgtype.UUID)
}
const countTracksMatching = `-- name: CountTracksMatching :one
SELECT COUNT(*) FROM tracks WHERE title ILIKE '%' || $1::text || '%'
`
func (q *Queries) CountTracksMatching(ctx context.Context, dollar_1 string) (int64, error) {
row := q.db.QueryRow(ctx, countTracksMatching, dollar_1)
var count int64
err := row.Scan(&count)
return count, err
}
const countTracksMatchingForUser = `-- name: CountTracksMatchingForUser :one
SELECT COUNT(*) FROM tracks
WHERE title ILIKE '%' || $1::text || '%'
AND NOT EXISTS (
@@ -56,14 +45,14 @@ WHERE title ILIKE '%' || $1::text || '%'
)
`
type CountTracksMatchingForUserParams struct {
type CountTracksMatchingParams struct {
Column1 string
UserID pgtype.UUID
}
// $1 = title query, $2 = user_id.
func (q *Queries) CountTracksMatchingForUser(ctx context.Context, arg CountTracksMatchingForUserParams) (int64, error) {
row := q.db.QueryRow(ctx, countTracksMatchingForUser, arg.Column1, arg.UserID)
// $1 = title query, $2 = user_id (NULL to skip quarantine filter).
func (q *Queries) CountTracksMatching(ctx context.Context, arg CountTracksMatchingParams) (int64, error) {
row := q.db.QueryRow(ctx, countTracksMatching, arg.Column1, arg.UserID)
var count int64
err := row.Scan(&count)
return count, err
@@ -225,46 +214,6 @@ func (q *Queries) ListArtistTracksForUser(ctx context.Context, arg ListArtistTra
}
const listTracksByAlbum = `-- name: ListTracksByAlbum :many
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at FROM tracks WHERE album_id = $1 ORDER BY disc_number NULLS LAST, track_number NULLS LAST
`
func (q *Queries) ListTracksByAlbum(ctx context.Context, albumID pgtype.UUID) ([]Track, error) {
rows, err := q.db.Query(ctx, listTracksByAlbum, albumID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Track
for rows.Next() {
var i Track
if err := rows.Scan(
&i.ID,
&i.Title,
&i.AlbumID,
&i.ArtistID,
&i.TrackNumber,
&i.DiscNumber,
&i.DurationMs,
&i.FilePath,
&i.FileSize,
&i.FileFormat,
&i.Bitrate,
&i.Mbid,
&i.Genre,
&i.AddedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listTracksByAlbumForUser = `-- name: ListTracksByAlbumForUser :many
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at FROM tracks
WHERE album_id = $1
AND NOT EXISTS (
@@ -274,15 +223,16 @@ WHERE album_id = $1
ORDER BY disc_number NULLS LAST, track_number NULLS LAST
`
type ListTracksByAlbumForUserParams struct {
type ListTracksByAlbumParams struct {
AlbumID pgtype.UUID
UserID pgtype.UUID
}
// Same as ListTracksByAlbum but excludes tracks the user has quarantined.
// $1 = album_id, $2 = user_id.
func (q *Queries) ListTracksByAlbumForUser(ctx context.Context, arg ListTracksByAlbumForUserParams) ([]Track, error) {
rows, err := q.db.Query(ctx, listTracksByAlbumForUser, arg.AlbumID, arg.UserID)
// $1 = album_id, $2 = user_id. Pass pgtype.UUID{Valid: false} (NULL)
// to skip the per-user quarantine filter; the NOT EXISTS clause on
// a NULL user_id never matches a row, so every track passes through.
func (q *Queries) ListTracksByAlbum(ctx context.Context, arg ListTracksByAlbumParams) ([]Track, error) {
rows, err := q.db.Query(ctx, listTracksByAlbum, arg.AlbumID, arg.UserID)
if err != nil {
return nil, err
}
@@ -319,56 +269,7 @@ func (q *Queries) ListTracksByAlbumForUser(ctx context.Context, arg ListTracksBy
const searchTracks = `-- name: SearchTracks :many
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at FROM tracks
WHERE title ILIKE '%' || $1 || '%'
ORDER BY title
LIMIT $2 OFFSET $3
`
type SearchTracksParams struct {
Column1 *string
Limit int32
Offset int32
}
func (q *Queries) SearchTracks(ctx context.Context, arg SearchTracksParams) ([]Track, error) {
rows, err := q.db.Query(ctx, searchTracks, arg.Column1, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Track
for rows.Next() {
var i Track
if err := rows.Scan(
&i.ID,
&i.Title,
&i.AlbumID,
&i.ArtistID,
&i.TrackNumber,
&i.DiscNumber,
&i.DurationMs,
&i.FilePath,
&i.FileSize,
&i.FileFormat,
&i.Bitrate,
&i.Mbid,
&i.Genre,
&i.AddedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const searchTracksForUser = `-- name: SearchTracksForUser :many
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at FROM tracks
WHERE title ILIKE '%' || $1 || '%'
WHERE title ILIKE '%' || $1::text || '%'
AND NOT EXISTS (
SELECT 1 FROM lidarr_quarantine q
WHERE q.user_id = $2 AND q.track_id = tracks.id
@@ -377,16 +278,17 @@ ORDER BY title
LIMIT $3 OFFSET $4
`
type SearchTracksForUserParams struct {
Column1 *string
type SearchTracksParams struct {
Column1 string
UserID pgtype.UUID
Limit int32
Offset int32
}
// $1 = title query, $2 = user_id, $3 = limit, $4 = offset.
func (q *Queries) SearchTracksForUser(ctx context.Context, arg SearchTracksForUserParams) ([]Track, error) {
rows, err := q.db.Query(ctx, searchTracksForUser,
// $1 = title query, $2 = user_id (NULL to skip quarantine filter),
// $3 = limit, $4 = offset.
func (q *Queries) SearchTracks(ctx context.Context, arg SearchTracksParams) ([]Track, error) {
rows, err := q.db.Query(ctx, searchTracks,
arg.Column1,
arg.UserID,
arg.Limit,