Commit 31e1b608 authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Add GET /playlists/{id} - playlist detail with ordered tracks

The existing playlist endpoints (list, create, add-track) had no way to
actually view what's in a playlist - GET /playlists only returns the
playlist rows themselves, not their tracks. Joins PlaylistTrack to
LibrarySong, ordered by track order, scoped to the owner. Needed to build
a real Playlist UI on the frontend (playlists have had a working backend
with zero UI since they were added).
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent ab7b741e
Pipeline #1092 passed with stage
in 3 minutes and 16 seconds
...@@ -47,6 +47,20 @@ async def get_playlists(db: Session = Depends(get_db), current_user: User = Depe ...@@ -47,6 +47,20 @@ async def get_playlists(db: Session = Depends(get_db), current_user: User = Depe
playlists = db.query(Playlist).filter(Playlist.user_id == current_user.id).order_by(Playlist.created_at.desc()).all() playlists = db.query(Playlist).filter(Playlist.user_id == current_user.id).order_by(Playlist.created_at.desc()).all()
return {"status": "success", "playlists": playlists} return {"status": "success", "playlists": playlists}
@router.get("/playlists/{playlist_id}")
async def get_playlist(playlist_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
pl = db.query(Playlist).filter(Playlist.id == playlist_id, Playlist.user_id == current_user.id).first()
if not pl:
raise HTTPException(status_code=404, detail="Playlist not found")
tracks = (
db.query(LibrarySong)
.join(PlaylistTrack, PlaylistTrack.song_id == LibrarySong.id)
.filter(PlaylistTrack.playlist_id == playlist_id)
.order_by(PlaylistTrack.order)
.all()
)
return {"status": "success", "playlist": pl, "tracks": tracks}
@router.post("/playlists") @router.post("/playlists")
async def create_playlist(data: PlaylistCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)): async def create_playlist(data: PlaylistCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
new_pl = Playlist(name=data.name, user_id=current_user.id) new_pl = Playlist(name=data.name, user_id=current_user.id)
......
...@@ -175,6 +175,49 @@ def test_playlists_scoped_to_current_user(client, auth_as): ...@@ -175,6 +175,49 @@ def test_playlists_scoped_to_current_user(client, auth_as):
assert response_b.json()["playlists"] == [] assert response_b.json()["playlists"] == []
def test_get_playlist_detail_returns_tracks_in_order(client, db_session, auth_as):
headers, user = auth_as()
song_a, path_a = _make_library_song(db_session, user.id, title="Track A")
song_b, path_b = _make_library_song(db_session, user.id, title="Track B")
try:
playlist_id = client.post("/playlists", json={"name": "Ordered Playlist"}, headers=headers).json()["playlist"]["id"]
client.post(f"/playlists/{playlist_id}/add", data={"song_id": song_b.id}, headers=headers)
client.post(f"/playlists/{playlist_id}/add", data={"song_id": song_a.id}, headers=headers)
response = client.get(f"/playlists/{playlist_id}", headers=headers)
assert response.status_code == 200
body = response.json()
assert body["playlist"]["id"] == playlist_id
assert [t["title"] for t in body["tracks"]] == ["Track B", "Track A"]
finally:
os.remove(path_a)
os.remove(path_b)
def test_get_playlist_detail_empty_playlist(client, auth_as):
headers, _ = auth_as()
playlist_id = client.post("/playlists", json={"name": "Empty Playlist"}, headers=headers).json()["playlist"]["id"]
response = client.get(f"/playlists/{playlist_id}", headers=headers)
assert response.status_code == 200
assert response.json()["tracks"] == []
def test_get_playlist_detail_404_for_unknown_id(client, auth_as):
headers, _ = auth_as()
response = client.get("/playlists/999999", headers=headers)
assert response.status_code == 404
def test_get_playlist_detail_404_for_another_users_playlist(client, auth_as):
headers_a, _ = auth_as()
headers_b, _ = auth_as()
playlist_id = client.post("/playlists", json={"name": "User A's playlist"}, headers=headers_a).json()["playlist"]["id"]
response = client.get(f"/playlists/{playlist_id}", headers=headers_b)
assert response.status_code == 404
def test_playlist_add_invalid(client, auth_as): def test_playlist_add_invalid(client, auth_as):
headers, _ = auth_as() headers, _ = auth_as()
response = client.post("/playlists/99999/add", data={"song_id": 99999}, headers=headers) response = client.post("/playlists/99999/add", data={"song_id": 99999}, headers=headers)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment