refactor: Organize and implement player and playlist schemas
This commit is contained in:
104
app/schemas/playlist.py
Normal file
104
app/schemas/playlist.py
Normal file
@@ -0,0 +1,104 @@
|
||||
"""Playlist schemas."""
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.sound import Sound
|
||||
|
||||
|
||||
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."""
|
||||
if playlist.id is None:
|
||||
raise ValueError("Playlist ID cannot be None")
|
||||
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 PlaylistSoundResponse(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) -> "PlaylistSoundResponse":
|
||||
"""Create response from sound model."""
|
||||
if sound.id is None:
|
||||
raise ValueError("Sound ID cannot be None")
|
||||
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 PlaylistAddSoundRequest(BaseModel):
|
||||
"""Request model for adding a sound to a playlist."""
|
||||
|
||||
sound_id: int
|
||||
position: int | None = None
|
||||
|
||||
|
||||
class PlaylistReorderRequest(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
|
||||
Reference in New Issue
Block a user