refactor: Rename global current playlist methods for clarity and consistency

This commit is contained in:
JSC
2025-08-01 17:12:56 +02:00
parent c0f51b2e23
commit 0575d12b0e
6 changed files with 33 additions and 104 deletions

View File

@@ -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")

View File

@@ -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(

View File

@@ -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)

View File

@@ -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,
)

View File

@@ -222,28 +222,11 @@ class TestPlaylistRepository:
test_session: AsyncSession,
ensure_plans: Any,
) -> None:
"""Test getting current playlist when none is set."""
# Create test user within this test
user = User(
email="test2@example.com",
name="Test User 2",
password_hash=PasswordUtils.hash_password("password123"),
role="user",
is_active=True,
plan_id=ensure_plans[0].id,
credits=100,
)
test_session.add(user)
await test_session.commit()
await test_session.refresh(user)
"""Test getting current playlist when none is set globally."""
# Test the repository method - should return None when no current playlist is set globally
playlist = await playlist_repository.get_current_playlist()
# Extract user ID immediately after refresh
user_id = user.id
# Test the repository method - should return None when no current playlist
playlist = await playlist_repository.get_current_playlist(user_id)
# Should return None since no user playlist is marked as current
# Should return None since no playlist is marked as current globally
assert playlist is None
@pytest.mark.asyncio

View File

@@ -464,7 +464,7 @@ class TestPlaylistService:
# Verify main playlist is now fallback current (main playlist doesn't have is_current=True)
# The service returns main playlist when no current is set
current = await playlist_service.get_current_playlist(user_id)
current = await playlist_service.get_current_playlist()
assert current.is_main is True
@pytest.mark.asyncio
@@ -754,7 +754,7 @@ class TestPlaylistService:
# Set test_playlist as current
updated_playlist = await playlist_service.set_current_playlist(
test_playlist_id, user_id,
test_playlist_id,
)
assert updated_playlist.is_current is True
@@ -804,10 +804,10 @@ class TestPlaylistService:
assert current_playlist.is_current is True
# Unset current playlist
await playlist_service.unset_current_playlist(user_id)
await playlist_service.unset_current_playlist()
# Verify get_current_playlist returns main playlist as fallback
current = await playlist_service.get_current_playlist(user_id)
current = await playlist_service.get_current_playlist()
assert current.id == main_playlist_id
assert current.is_main is True