refactor: Organize and implement player and playlist schemas
This commit is contained in:
@@ -3,113 +3,25 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.core.dependencies import get_current_active_user_flexible
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.sound import Sound
|
||||
from app.models.user import User
|
||||
from app.schemas.playlist import (
|
||||
PlaylistAddSoundRequest,
|
||||
PlaylistCreateRequest,
|
||||
PlaylistReorderRequest,
|
||||
PlaylistResponse,
|
||||
PlaylistSoundResponse,
|
||||
PlaylistStatsResponse,
|
||||
PlaylistUpdateRequest,
|
||||
)
|
||||
from app.services.playlist import PlaylistService
|
||||
|
||||
router = APIRouter(prefix="/playlists", tags=["playlists"])
|
||||
|
||||
|
||||
class PlaylistCreateRequest(BaseModel):
|
||||
"""Request model for creating a playlist."""
|
||||
|
||||
name: str
|
||||
description: str | None = None
|
||||
genre: str | None = None
|
||||
|
||||
|
||||
class PlaylistUpdateRequest(BaseModel):
|
||||
"""Request model for updating a playlist."""
|
||||
|
||||
name: str | None = None
|
||||
description: str | None = None
|
||||
genre: str | None = None
|
||||
is_current: bool | None = None
|
||||
|
||||
|
||||
class PlaylistResponse(BaseModel):
|
||||
"""Response model for playlist data."""
|
||||
|
||||
id: int
|
||||
name: str
|
||||
description: str | None
|
||||
genre: str | None
|
||||
is_main: bool
|
||||
is_current: bool
|
||||
is_deletable: bool
|
||||
created_at: str
|
||||
updated_at: str | None
|
||||
|
||||
@classmethod
|
||||
def from_playlist(cls, playlist: Playlist) -> "PlaylistResponse":
|
||||
"""Create response from playlist model."""
|
||||
return cls(
|
||||
id=playlist.id,
|
||||
name=playlist.name,
|
||||
description=playlist.description,
|
||||
genre=playlist.genre,
|
||||
is_main=playlist.is_main,
|
||||
is_current=playlist.is_current,
|
||||
is_deletable=playlist.is_deletable,
|
||||
created_at=playlist.created_at.isoformat(),
|
||||
updated_at=playlist.updated_at.isoformat() if playlist.updated_at else None,
|
||||
)
|
||||
|
||||
|
||||
class SoundResponse(BaseModel):
|
||||
"""Response model for sound data in playlists."""
|
||||
|
||||
id: int
|
||||
name: str
|
||||
filename: str
|
||||
type: str
|
||||
duration: int | None
|
||||
size: int | None
|
||||
play_count: int
|
||||
created_at: str
|
||||
|
||||
@classmethod
|
||||
def from_sound(cls, sound: Sound) -> "SoundResponse":
|
||||
"""Create response from sound model."""
|
||||
return cls(
|
||||
id=sound.id,
|
||||
name=sound.name,
|
||||
filename=sound.filename,
|
||||
type=sound.type,
|
||||
duration=sound.duration,
|
||||
size=sound.size,
|
||||
play_count=sound.play_count,
|
||||
created_at=sound.created_at.isoformat(),
|
||||
)
|
||||
|
||||
|
||||
class AddSoundRequest(BaseModel):
|
||||
"""Request model for adding a sound to a playlist."""
|
||||
|
||||
sound_id: int
|
||||
position: int | None = None
|
||||
|
||||
|
||||
class ReorderRequest(BaseModel):
|
||||
"""Request model for reordering sounds in a playlist."""
|
||||
|
||||
sound_positions: list[tuple[int, int]]
|
||||
|
||||
|
||||
class PlaylistStatsResponse(BaseModel):
|
||||
"""Response model for playlist statistics."""
|
||||
|
||||
sound_count: int
|
||||
total_duration_ms: int
|
||||
total_play_count: int
|
||||
|
||||
|
||||
async def get_playlist_service(
|
||||
session: Annotated[AsyncSession, Depends(get_db)],
|
||||
) -> PlaylistService:
|
||||
@@ -241,16 +153,16 @@ async def get_playlist_sounds(
|
||||
playlist_id: int,
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
) -> list[SoundResponse]:
|
||||
) -> list[PlaylistSoundResponse]:
|
||||
"""Get all sounds in a playlist."""
|
||||
sounds = await playlist_service.get_playlist_sounds(playlist_id)
|
||||
return [SoundResponse.from_sound(sound) for sound in sounds]
|
||||
return [PlaylistSoundResponse.from_sound(sound) for sound in sounds]
|
||||
|
||||
|
||||
@router.post("/{playlist_id}/sounds")
|
||||
async def add_sound_to_playlist(
|
||||
playlist_id: int,
|
||||
request: AddSoundRequest,
|
||||
request: PlaylistAddSoundRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
) -> dict[str, str]:
|
||||
@@ -283,7 +195,7 @@ async def remove_sound_from_playlist(
|
||||
@router.put("/{playlist_id}/sounds/reorder")
|
||||
async def reorder_playlist_sounds(
|
||||
playlist_id: int,
|
||||
request: ReorderRequest,
|
||||
request: PlaylistReorderRequest,
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)],
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
) -> dict[str, str]:
|
||||
|
||||
Reference in New Issue
Block a user