121 lines
3.2 KiB
Python
121 lines
3.2 KiB
Python
"""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
|
|
is_favorited: bool = False
|
|
favorite_count: int = 0
|
|
created_at: str
|
|
updated_at: str | None
|
|
|
|
@classmethod
|
|
def from_playlist(cls, playlist: Playlist, is_favorited: bool = False, favorite_count: int = 0) -> "PlaylistResponse":
|
|
"""Create response from playlist model.
|
|
|
|
Args:
|
|
playlist: The Playlist model
|
|
is_favorited: Whether the playlist is favorited by the current user
|
|
favorite_count: Number of users who favorited this playlist
|
|
|
|
Returns:
|
|
PlaylistResponse instance
|
|
|
|
"""
|
|
if playlist.id is None:
|
|
msg = "Playlist ID cannot be None"
|
|
raise ValueError(msg)
|
|
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,
|
|
is_favorited=is_favorited,
|
|
favorite_count=favorite_count,
|
|
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:
|
|
msg = "Sound ID cannot be None"
|
|
raise ValueError(msg)
|
|
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
|