feat: Add load_playlist method to PlayerService and update task handlers for playlist management

This commit is contained in:
JSC
2025-08-28 22:50:57 +02:00
parent 03abed6d39
commit 6e74d9b940
2 changed files with 34 additions and 9 deletions

View File

@@ -429,6 +429,26 @@ class PlayerService:
await self._broadcast_state()
async def load_playlist(self, playlist_id: int) -> None:
"""Load a specific playlist by ID."""
session = self.db_session_factory()
try:
playlist_repo = PlaylistRepository(session)
playlist = await playlist_repo.get_by_id(playlist_id)
if playlist and playlist.id:
sounds = await playlist_repo.get_playlist_sounds(playlist.id)
await self._handle_playlist_reload(playlist, sounds)
logger.info(
"Loaded playlist: %s (%s sounds)",
playlist.name,
len(sounds),
)
else:
logger.warning("Playlist not found: %s", playlist_id)
finally:
await session.close()
await self._broadcast_state()
async def _handle_playlist_reload(
self,
current_playlist: Playlist,

View File

@@ -1,6 +1,5 @@
"""Task execution handlers for different task types."""
import uuid
from typing import Any, Dict, Optional
from sqlmodel.ext.asyncio.session import AsyncSession
@@ -66,8 +65,12 @@ class TaskHandlerRegistry:
if user_id:
# Recharge specific user
user_uuid = uuid.UUID(user_id) if isinstance(user_id, str) else user_id
stats = await self.credit_service.recharge_user_credits(user_uuid)
try:
user_id_int = int(user_id)
except (ValueError, TypeError) as e:
raise TaskExecutionError(f"Invalid user_id format: {user_id}") from e
stats = await self.credit_service.recharge_user_credits(user_id_int)
logger.info(f"Recharged credits for user {user_id}: {stats}")
else:
# Recharge all users (system task)
@@ -83,12 +86,13 @@ class TaskHandlerRegistry:
raise TaskExecutionError("sound_id parameter is required for PLAY_SOUND tasks")
try:
sound_uuid = uuid.UUID(sound_id) if isinstance(sound_id, str) else sound_id
# Handle both integer and string sound IDs
sound_id_int = int(sound_id)
except (ValueError, TypeError) as e:
raise TaskExecutionError(f"Invalid sound_id format: {sound_id}") from e
# Get the sound from database
sound = await self.sound_repository.get_by_id(sound_uuid)
sound = await self.sound_repository.get_by_id(sound_id_int)
if not sound:
raise TaskExecutionError(f"Sound not found: {sound_id}")
@@ -111,17 +115,18 @@ class TaskHandlerRegistry:
raise TaskExecutionError("playlist_id parameter is required for PLAY_PLAYLIST tasks")
try:
playlist_uuid = uuid.UUID(playlist_id) if isinstance(playlist_id, str) else playlist_id
# Handle both integer and string playlist IDs
playlist_id_int = int(playlist_id)
except (ValueError, TypeError) as e:
raise TaskExecutionError(f"Invalid playlist_id format: {playlist_id}") from e
# Get the playlist from database
playlist = await self.playlist_repository.get_by_id(playlist_uuid)
playlist = await self.playlist_repository.get_by_id(playlist_id_int)
if not playlist:
raise TaskExecutionError(f"Playlist not found: {playlist_id}")
# Load playlist in player
await self.player_service.load_playlist(playlist_uuid)
await self.player_service.load_playlist(playlist_id_int)
# Set play mode if specified
if play_mode in ["continuous", "loop", "loop_one", "random", "single"]:
@@ -132,6 +137,6 @@ class TaskHandlerRegistry:
self.player_service.set_shuffle(True)
# Start playing
self.player_service.play()
await self.player_service.play()
logger.info(f"Started playing playlist {playlist.name} via scheduled task")