refactor: Rename global current playlist methods for clarity and consistency
This commit is contained in:
@@ -221,18 +221,18 @@ async def set_current_playlist(
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
) -> PlaylistResponse:
|
||||
"""Set a playlist as the global current playlist."""
|
||||
playlist = await playlist_service.set_current_playlist_global(playlist_id)
|
||||
"""Set a playlist as the current playlist."""
|
||||
playlist = await playlist_service.set_current_playlist(playlist_id)
|
||||
return PlaylistResponse.from_playlist(playlist)
|
||||
|
||||
|
||||
@router.delete("/current")
|
||||
async def unset_current_playlist(
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
|
||||
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||
) -> MessageResponse:
|
||||
"""Unset the global current playlist."""
|
||||
await playlist_service.unset_current_playlist_global()
|
||||
"""Unset the current playlist."""
|
||||
await playlist_service.unset_current_playlist()
|
||||
return MessageResponse(message="Current playlist unset successfully")
|
||||
|
||||
|
||||
|
||||
@@ -53,20 +53,7 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
||||
logger.exception("Failed to get main playlist")
|
||||
raise
|
||||
|
||||
async def get_current_playlist(self, user_id: int) -> Playlist | None:
|
||||
"""Get the user's current playlist."""
|
||||
try:
|
||||
statement = select(Playlist).where(
|
||||
Playlist.user_id == user_id,
|
||||
Playlist.is_current == True, # noqa: E712
|
||||
)
|
||||
result = await self.session.exec(statement)
|
||||
return result.first()
|
||||
except Exception:
|
||||
logger.exception("Failed to get current playlist for user: %s", user_id)
|
||||
raise
|
||||
|
||||
async def get_global_current_playlist(self) -> Playlist | None:
|
||||
async def get_current_playlist(self) -> Playlist | None:
|
||||
"""Get the global current playlist (app-wide)."""
|
||||
try:
|
||||
statement = select(Playlist).where(
|
||||
@@ -75,7 +62,7 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
||||
result = await self.session.exec(statement)
|
||||
return result.first()
|
||||
except Exception:
|
||||
logger.exception("Failed to get global current playlist")
|
||||
logger.exception("Failed to get current playlist")
|
||||
raise
|
||||
|
||||
async def search_by_name(
|
||||
|
||||
@@ -189,9 +189,7 @@ class PlayerService:
|
||||
await self._broadcast_state()
|
||||
|
||||
sound_name = (
|
||||
self.state.current_sound.name
|
||||
if self.state.current_sound
|
||||
else "Unknown"
|
||||
self.state.current_sound.name if self.state.current_sound else "Unknown"
|
||||
)
|
||||
logger.info("Resumed playing sound: %s", sound_name)
|
||||
else:
|
||||
@@ -388,7 +386,7 @@ class PlayerService:
|
||||
session = self.db_session_factory()
|
||||
try:
|
||||
playlist_repo = PlaylistRepository(session)
|
||||
current_playlist = await playlist_repo.get_main_playlist()
|
||||
current_playlist = await playlist_repo.get_current_playlist()
|
||||
|
||||
if current_playlist and current_playlist.id:
|
||||
sounds = await playlist_repo.get_playlist_sounds(current_playlist.id)
|
||||
|
||||
@@ -56,7 +56,7 @@ class PlaylistService:
|
||||
|
||||
async def get_current_playlist(self) -> Playlist:
|
||||
"""Get the global current playlist, fallback to main playlist."""
|
||||
current_playlist = await self.playlist_repo.get_global_current_playlist()
|
||||
current_playlist = await self.playlist_repo.get_current_playlist()
|
||||
if current_playlist:
|
||||
return current_playlist
|
||||
|
||||
@@ -85,7 +85,7 @@ class PlaylistService:
|
||||
|
||||
# If this is set as current, unset the previous current playlist
|
||||
if is_current:
|
||||
await self._unset_current_playlist(user_id)
|
||||
await self._unset_current_playlist()
|
||||
|
||||
playlist_data = {
|
||||
"user_id": user_id,
|
||||
@@ -138,7 +138,7 @@ class PlaylistService:
|
||||
|
||||
if is_current is not None:
|
||||
if is_current:
|
||||
await self._unset_current_playlist(user_id)
|
||||
await self._unset_current_playlist()
|
||||
update_data["is_current"] = is_current
|
||||
|
||||
if update_data:
|
||||
@@ -157,15 +157,11 @@ class PlaylistService:
|
||||
detail="This playlist cannot be deleted",
|
||||
)
|
||||
|
||||
# Check if this is the current playlist
|
||||
was_current = playlist.is_current
|
||||
|
||||
await self.playlist_repo.delete(playlist)
|
||||
logger.info("Deleted playlist %s for user %s", playlist_id, user_id)
|
||||
|
||||
# If the deleted playlist was current, set main playlist as current
|
||||
if was_current:
|
||||
await self._set_main_as_current(user_id)
|
||||
# Note: If the deleted playlist was current, main playlist becomes fallback
|
||||
# No action needed as get_current_playlist() handles the fallback automatically
|
||||
|
||||
async def search_playlists(self, query: str, user_id: int) -> list[Playlist]:
|
||||
"""Search user's playlists by name."""
|
||||
@@ -260,26 +256,6 @@ class PlaylistService:
|
||||
await self.playlist_repo.reorder_playlist_sounds(playlist_id, sound_positions)
|
||||
logger.info("Reordered sounds in playlist %s for user %s", playlist_id, user_id)
|
||||
|
||||
async def set_current_playlist(self, playlist_id: int, user_id: int) -> Playlist:
|
||||
"""Set a playlist as the current playlist."""
|
||||
playlist = await self.get_playlist_by_id(playlist_id)
|
||||
|
||||
# Unset previous current playlist
|
||||
await self._unset_current_playlist(user_id)
|
||||
|
||||
# Set new current playlist
|
||||
playlist = await self.playlist_repo.update(playlist, {"is_current": True})
|
||||
logger.info("Set playlist %s as current for user %s", playlist_id, user_id)
|
||||
return playlist
|
||||
|
||||
async def unset_current_playlist(self, user_id: int) -> None:
|
||||
"""Unset the current playlist and set main playlist as current."""
|
||||
await self._unset_current_playlist(user_id)
|
||||
await self._set_main_as_current(user_id)
|
||||
logger.info(
|
||||
"Unset current playlist and set main as current for user %s",
|
||||
user_id,
|
||||
)
|
||||
|
||||
async def get_playlist_stats(self, playlist_id: int) -> dict[str, Any]:
|
||||
"""Get statistics for a playlist."""
|
||||
@@ -317,42 +293,27 @@ class PlaylistService:
|
||||
user_id,
|
||||
)
|
||||
|
||||
# Global current playlist methods
|
||||
async def set_current_playlist_global(self, playlist_id: int) -> Playlist:
|
||||
"""Set a playlist as the global current playlist (app-wide)."""
|
||||
# Current playlist methods (global by default)
|
||||
async def set_current_playlist(self, playlist_id: int) -> Playlist:
|
||||
"""Set a playlist as the current playlist (app-wide)."""
|
||||
playlist = await self.get_playlist_by_id(playlist_id)
|
||||
|
||||
# Unset any existing current playlist globally
|
||||
await self._unset_global_current_playlist()
|
||||
await self._unset_current_playlist()
|
||||
|
||||
# Set new current playlist
|
||||
playlist = await self.playlist_repo.update(playlist, {"is_current": True})
|
||||
logger.info("Set playlist %s as global current playlist", playlist_id)
|
||||
logger.info("Set playlist %s as current playlist", playlist_id)
|
||||
return playlist
|
||||
|
||||
async def unset_current_playlist_global(self) -> None:
|
||||
"""Unset the global current playlist (main playlist becomes fallback)."""
|
||||
await self._unset_global_current_playlist()
|
||||
logger.info("Unset global current playlist, main playlist is now fallback")
|
||||
async def unset_current_playlist(self) -> None:
|
||||
"""Unset the current playlist (main playlist becomes fallback)."""
|
||||
await self._unset_current_playlist()
|
||||
logger.info("Unset current playlist, main playlist is now fallback")
|
||||
|
||||
async def _unset_global_current_playlist(self) -> None:
|
||||
async def _unset_current_playlist(self) -> None:
|
||||
"""Unset any current playlist globally."""
|
||||
current_playlist = await self.playlist_repo.get_global_current_playlist()
|
||||
current_playlist = await self.playlist_repo.get_current_playlist()
|
||||
if current_playlist:
|
||||
await self.playlist_repo.update(current_playlist, {"is_current": False})
|
||||
|
||||
async def _unset_current_playlist(self, user_id: int) -> None:
|
||||
"""Unset the current playlist for a user."""
|
||||
current_playlist = await self.playlist_repo.get_current_playlist(user_id)
|
||||
if current_playlist:
|
||||
await self.playlist_repo.update(current_playlist, {"is_current": False})
|
||||
|
||||
async def _set_main_as_current(self, user_id: int) -> None:
|
||||
"""Unset current playlist so main playlist becomes the fallback current."""
|
||||
# Just ensure no user playlist is marked as current
|
||||
# The get_current_playlist method will fallback to main playlist
|
||||
await self._unset_current_playlist(user_id)
|
||||
logger.info(
|
||||
"Unset current playlist for user %s, main playlist is now fallback",
|
||||
user_id,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user