stuff
This commit is contained in:
@@ -29,3 +29,7 @@ class PlaylistUpdate(BaseModel):
|
||||
class PlaylistAddTrack(BaseModel):
|
||||
track_id: uuid.UUID
|
||||
position: float | None = None
|
||||
|
||||
|
||||
class PlaylistReorder(BaseModel):
|
||||
track_ids: list[uuid.UUID]
|
||||
|
||||
+34
-3
@@ -13,11 +13,17 @@ from app.api.deps import (
|
||||
TrackRepoDep,
|
||||
)
|
||||
from app.api.schemas.pagination import PagedResponse
|
||||
from app.api.schemas.playlist import PlaylistAddTrack, PlaylistCreate, PlaylistOut, PlaylistUpdate
|
||||
from app.api.schemas.playlist import (
|
||||
PlaylistAddTrack,
|
||||
PlaylistCreate,
|
||||
PlaylistOut,
|
||||
PlaylistReorder,
|
||||
PlaylistUpdate,
|
||||
)
|
||||
from app.api.schemas.track import TrackOut
|
||||
from app.api.v1.tracks import _build_track_out
|
||||
from app.domain.entities.playlist import Playlist
|
||||
from app.domain.errors import NotFoundError, PermissionDeniedError
|
||||
from app.domain.errors import NotFoundError, PermissionDeniedError, ValidationError
|
||||
from app.infrastructure.db.repositories.playlist_repository import SqlAlchemyPlaylistRepository
|
||||
|
||||
router = APIRouter(prefix="/playlists", tags=["playlists"])
|
||||
@@ -182,7 +188,32 @@ async def remove_playlist_track(
|
||||
|
||||
|
||||
@router.put("/{playlist_id}/tracks/reorder")
|
||||
async def reorder_playlist_tracks(playlist_id: uuid.UUID, _: CurrentUser) -> Any: ...
|
||||
async def reorder_playlist_tracks(
|
||||
playlist_id: uuid.UUID,
|
||||
body: PlaylistReorder,
|
||||
playlist_repo: PlaylistRepoDep,
|
||||
user: CurrentUser,
|
||||
) -> PlaylistOut:
|
||||
playlist = await playlist_repo.get_by_id(playlist_id)
|
||||
if playlist is None:
|
||||
raise NotFoundError(f"Playlist {playlist_id} not found.")
|
||||
if playlist.owner_id != user.id:
|
||||
raise PermissionDeniedError("You don't own this playlist.")
|
||||
|
||||
total = await playlist_repo.get_track_total(playlist_id)
|
||||
current_tracks = (
|
||||
await playlist_repo.get_tracks(playlist_id, limit=total, offset=0) if total else []
|
||||
)
|
||||
current_ids = {t.id for t in current_tracks}
|
||||
given_ids = body.track_ids
|
||||
if len(given_ids) != len(set(given_ids)) or set(given_ids) != current_ids:
|
||||
raise ValidationError("track_ids must be a permutation of the playlist's current tracks.")
|
||||
|
||||
await playlist_repo.reorder_tracks(playlist_id, given_ids)
|
||||
updated = await playlist_repo.get_by_id(playlist_id)
|
||||
assert updated is not None
|
||||
items = await _build_playlist_out([updated], playlist_repo)
|
||||
return items[0]
|
||||
|
||||
|
||||
@router.get("/{playlist_id}/cover")
|
||||
|
||||
Reference in New Issue
Block a user