view tracks in playlist view and play playlists
This commit is contained in:
@@ -31,6 +31,28 @@ DELETE FROM playlist_tracks WHERE id = ?;
|
||||
-- name: ClearPlaylistTracks :exec
|
||||
DELETE FROM playlist_tracks WHERE playlist_id = ?;
|
||||
|
||||
-- name: GetPlaylistTracksWithMetadata :many
|
||||
SELECT
|
||||
pt.id,
|
||||
pt.playlist_id,
|
||||
pt.audio_file_id,
|
||||
pt.position,
|
||||
af.file_path,
|
||||
af.length_milliseconds,
|
||||
COALESCE(r.name, '') AS title,
|
||||
COALESCE(ac.text, '') AS artist,
|
||||
COALESCE(rg.name, '') AS album,
|
||||
COALESCE(ca.file_path, '') AS cover_art_path
|
||||
FROM playlist_tracks pt
|
||||
JOIN audio_files af ON pt.audio_file_id = af.id
|
||||
LEFT JOIN recordings r ON af.recording_id = r.id
|
||||
LEFT JOIN artist_credit ac ON r.artist_credit_id = ac.id
|
||||
LEFT JOIN release_group_recordings rgr ON r.id = rgr.recording_id
|
||||
LEFT JOIN release_groups rg ON rgr.release_group_id = rg.id
|
||||
LEFT JOIN cover_art ca ON rg.cover_art_id = ca.id
|
||||
WHERE pt.playlist_id = ?
|
||||
ORDER BY pt.position;
|
||||
|
||||
-- name: GetNextPlaylistTrackPosition :one
|
||||
SELECT COALESCE(MAX(position), -1) + 1 AS next_position
|
||||
FROM playlist_tracks WHERE playlist_id = ?;
|
||||
|
||||
@@ -172,6 +172,76 @@ func (q *Queries) GetPlaylistTracks(ctx context.Context, playlistID int64) ([]Ge
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getPlaylistTracksWithMetadata = `-- name: GetPlaylistTracksWithMetadata :many
|
||||
SELECT
|
||||
pt.id,
|
||||
pt.playlist_id,
|
||||
pt.audio_file_id,
|
||||
pt.position,
|
||||
af.file_path,
|
||||
af.length_milliseconds,
|
||||
COALESCE(r.name, '') AS title,
|
||||
COALESCE(ac.text, '') AS artist,
|
||||
COALESCE(rg.name, '') AS album,
|
||||
COALESCE(ca.file_path, '') AS cover_art_path
|
||||
FROM playlist_tracks pt
|
||||
JOIN audio_files af ON pt.audio_file_id = af.id
|
||||
LEFT JOIN recordings r ON af.recording_id = r.id
|
||||
LEFT JOIN artist_credit ac ON r.artist_credit_id = ac.id
|
||||
LEFT JOIN release_group_recordings rgr ON r.id = rgr.recording_id
|
||||
LEFT JOIN release_groups rg ON rgr.release_group_id = rg.id
|
||||
LEFT JOIN cover_art ca ON rg.cover_art_id = ca.id
|
||||
WHERE pt.playlist_id = ?
|
||||
ORDER BY pt.position
|
||||
`
|
||||
|
||||
type GetPlaylistTracksWithMetadataRow struct {
|
||||
ID int64
|
||||
PlaylistID int64
|
||||
AudioFileID int64
|
||||
Position int64
|
||||
FilePath string
|
||||
LengthMilliseconds int64
|
||||
Title string
|
||||
Artist string
|
||||
Album string
|
||||
CoverArtPath string
|
||||
}
|
||||
|
||||
func (q *Queries) GetPlaylistTracksWithMetadata(ctx context.Context, playlistID int64) ([]GetPlaylistTracksWithMetadataRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getPlaylistTracksWithMetadata, playlistID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetPlaylistTracksWithMetadataRow
|
||||
for rows.Next() {
|
||||
var i GetPlaylistTracksWithMetadataRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.PlaylistID,
|
||||
&i.AudioFileID,
|
||||
&i.Position,
|
||||
&i.FilePath,
|
||||
&i.LengthMilliseconds,
|
||||
&i.Title,
|
||||
&i.Artist,
|
||||
&i.Album,
|
||||
&i.CoverArtPath,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const removePlaylistTrack = `-- name: RemovePlaylistTrack :exec
|
||||
DELETE FROM playlist_tracks WHERE id = ?
|
||||
`
|
||||
|
||||
@@ -6,10 +6,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"yellowjacket/backend/database"
|
||||
"yellowjacket/backend/database/sql/sqlcgen"
|
||||
"yellowjacket/backend/library"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -24,6 +27,19 @@ type Summary struct {
|
||||
Name string `json:"Name"`
|
||||
}
|
||||
|
||||
// Track represents a track within a playlist, including its metadata.
|
||||
type Track struct {
|
||||
ID int64 `json:"ID"`
|
||||
Position int64 `json:"Position"`
|
||||
FilePath string `json:"FilePath"`
|
||||
Title string `json:"Title"`
|
||||
Artist string `json:"Artist"`
|
||||
Album string `json:"Album"`
|
||||
CoverArtPath string `json:"CoverArtPath"`
|
||||
CoverArtThumbnailPath string `json:"CoverArtThumbnailPath"`
|
||||
Duration string `json:"Duration"`
|
||||
}
|
||||
|
||||
// Service manages playlist operations.
|
||||
type Service struct {
|
||||
ctx context.Context
|
||||
@@ -67,6 +83,56 @@ func (s *Service) GetAllPlaylists() ([]Summary, error) {
|
||||
return summaries, nil
|
||||
}
|
||||
|
||||
// GetPlaylistTracks returns all tracks in a playlist with full metadata.
|
||||
func (s *Service) GetPlaylistTracks(
|
||||
playlistID int64,
|
||||
) ([]Track, error) {
|
||||
rows, err := s.db.Queries.GetPlaylistTracksWithMetadata(
|
||||
s.db.Ctx,
|
||||
playlistID,
|
||||
)
|
||||
if err != nil {
|
||||
s.logger.Error(
|
||||
"Failed to get playlist tracks",
|
||||
"playlistId", playlistID,
|
||||
"err", err,
|
||||
)
|
||||
|
||||
return nil, fmt.Errorf(
|
||||
"failed to get playlist tracks: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
tracks := make([]Track, 0, len(rows))
|
||||
|
||||
for _, row := range rows {
|
||||
track := Track{
|
||||
ID: row.ID,
|
||||
Position: row.Position,
|
||||
FilePath: row.FilePath,
|
||||
Title: row.Title,
|
||||
Artist: row.Artist,
|
||||
Album: row.Album,
|
||||
Duration: strconv.FormatInt(
|
||||
row.LengthMilliseconds,
|
||||
10,
|
||||
),
|
||||
}
|
||||
|
||||
if row.CoverArtPath != "" {
|
||||
base := filepath.Base(row.CoverArtPath)
|
||||
track.CoverArtPath = "/covers/" + base
|
||||
track.CoverArtThumbnailPath = "/covers/" +
|
||||
library.ThumbnailFilename(base)
|
||||
}
|
||||
|
||||
tracks = append(tracks, track)
|
||||
}
|
||||
|
||||
return tracks, nil
|
||||
}
|
||||
|
||||
// CreatePlaylist creates a new empty playlist with the given name.
|
||||
func (s *Service) CreatePlaylist(name string) (Summary, error) {
|
||||
trimmed := strings.TrimSpace(name)
|
||||
|
||||
Reference in New Issue
Block a user