refactor: Rename global current playlist methods for clarity and consistency
This commit is contained in:
@@ -221,8 +221,8 @@ async def set_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)],
|
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||||
) -> PlaylistResponse:
|
) -> PlaylistResponse:
|
||||||
"""Set a playlist as the global current playlist."""
|
"""Set a playlist as the current playlist."""
|
||||||
playlist = await playlist_service.set_current_playlist_global(playlist_id)
|
playlist = await playlist_service.set_current_playlist(playlist_id)
|
||||||
return PlaylistResponse.from_playlist(playlist)
|
return PlaylistResponse.from_playlist(playlist)
|
||||||
|
|
||||||
|
|
||||||
@@ -231,8 +231,8 @@ 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)],
|
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
|
||||||
) -> MessageResponse:
|
) -> MessageResponse:
|
||||||
"""Unset the global current playlist."""
|
"""Unset the current playlist."""
|
||||||
await playlist_service.unset_current_playlist_global()
|
await playlist_service.unset_current_playlist()
|
||||||
return MessageResponse(message="Current playlist unset successfully")
|
return MessageResponse(message="Current playlist unset successfully")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -53,20 +53,7 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
|||||||
logger.exception("Failed to get main playlist")
|
logger.exception("Failed to get main playlist")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def get_current_playlist(self, user_id: int) -> Playlist | None:
|
async def get_current_playlist(self) -> 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:
|
|
||||||
"""Get the global current playlist (app-wide)."""
|
"""Get the global current playlist (app-wide)."""
|
||||||
try:
|
try:
|
||||||
statement = select(Playlist).where(
|
statement = select(Playlist).where(
|
||||||
@@ -75,7 +62,7 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
|||||||
result = await self.session.exec(statement)
|
result = await self.session.exec(statement)
|
||||||
return result.first()
|
return result.first()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to get global current playlist")
|
logger.exception("Failed to get current playlist")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def search_by_name(
|
async def search_by_name(
|
||||||
|
|||||||
@@ -189,9 +189,7 @@ class PlayerService:
|
|||||||
await self._broadcast_state()
|
await self._broadcast_state()
|
||||||
|
|
||||||
sound_name = (
|
sound_name = (
|
||||||
self.state.current_sound.name
|
self.state.current_sound.name if self.state.current_sound else "Unknown"
|
||||||
if self.state.current_sound
|
|
||||||
else "Unknown"
|
|
||||||
)
|
)
|
||||||
logger.info("Resumed playing sound: %s", sound_name)
|
logger.info("Resumed playing sound: %s", sound_name)
|
||||||
else:
|
else:
|
||||||
@@ -388,7 +386,7 @@ class PlayerService:
|
|||||||
session = self.db_session_factory()
|
session = self.db_session_factory()
|
||||||
try:
|
try:
|
||||||
playlist_repo = PlaylistRepository(session)
|
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:
|
if current_playlist and current_playlist.id:
|
||||||
sounds = await playlist_repo.get_playlist_sounds(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:
|
async def get_current_playlist(self) -> Playlist:
|
||||||
"""Get the global current playlist, fallback to main 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:
|
if current_playlist:
|
||||||
return current_playlist
|
return current_playlist
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ class PlaylistService:
|
|||||||
|
|
||||||
# If this is set as current, unset the previous current playlist
|
# If this is set as current, unset the previous current playlist
|
||||||
if is_current:
|
if is_current:
|
||||||
await self._unset_current_playlist(user_id)
|
await self._unset_current_playlist()
|
||||||
|
|
||||||
playlist_data = {
|
playlist_data = {
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
@@ -138,7 +138,7 @@ class PlaylistService:
|
|||||||
|
|
||||||
if is_current is not None:
|
if is_current is not None:
|
||||||
if is_current:
|
if is_current:
|
||||||
await self._unset_current_playlist(user_id)
|
await self._unset_current_playlist()
|
||||||
update_data["is_current"] = is_current
|
update_data["is_current"] = is_current
|
||||||
|
|
||||||
if update_data:
|
if update_data:
|
||||||
@@ -157,15 +157,11 @@ class PlaylistService:
|
|||||||
detail="This playlist cannot be deleted",
|
detail="This playlist cannot be deleted",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if this is the current playlist
|
|
||||||
was_current = playlist.is_current
|
|
||||||
|
|
||||||
await self.playlist_repo.delete(playlist)
|
await self.playlist_repo.delete(playlist)
|
||||||
logger.info("Deleted playlist %s for user %s", playlist_id, user_id)
|
logger.info("Deleted playlist %s for user %s", playlist_id, user_id)
|
||||||
|
|
||||||
# If the deleted playlist was current, set main playlist as current
|
# Note: If the deleted playlist was current, main playlist becomes fallback
|
||||||
if was_current:
|
# No action needed as get_current_playlist() handles the fallback automatically
|
||||||
await self._set_main_as_current(user_id)
|
|
||||||
|
|
||||||
async def search_playlists(self, query: str, user_id: int) -> list[Playlist]:
|
async def search_playlists(self, query: str, user_id: int) -> list[Playlist]:
|
||||||
"""Search user's playlists by name."""
|
"""Search user's playlists by name."""
|
||||||
@@ -260,26 +256,6 @@ class PlaylistService:
|
|||||||
await self.playlist_repo.reorder_playlist_sounds(playlist_id, sound_positions)
|
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)
|
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]:
|
async def get_playlist_stats(self, playlist_id: int) -> dict[str, Any]:
|
||||||
"""Get statistics for a playlist."""
|
"""Get statistics for a playlist."""
|
||||||
@@ -317,42 +293,27 @@ class PlaylistService:
|
|||||||
user_id,
|
user_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Global current playlist methods
|
# Current playlist methods (global by default)
|
||||||
async def set_current_playlist_global(self, playlist_id: int) -> Playlist:
|
async def set_current_playlist(self, playlist_id: int) -> Playlist:
|
||||||
"""Set a playlist as the global current playlist (app-wide)."""
|
"""Set a playlist as the current playlist (app-wide)."""
|
||||||
playlist = await self.get_playlist_by_id(playlist_id)
|
playlist = await self.get_playlist_by_id(playlist_id)
|
||||||
|
|
||||||
# Unset any existing current playlist globally
|
# Unset any existing current playlist globally
|
||||||
await self._unset_global_current_playlist()
|
await self._unset_current_playlist()
|
||||||
|
|
||||||
# Set new current playlist
|
# Set new current playlist
|
||||||
playlist = await self.playlist_repo.update(playlist, {"is_current": True})
|
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
|
return playlist
|
||||||
|
|
||||||
async def unset_current_playlist_global(self) -> None:
|
async def unset_current_playlist(self) -> None:
|
||||||
"""Unset the global current playlist (main playlist becomes fallback)."""
|
"""Unset the current playlist (main playlist becomes fallback)."""
|
||||||
await self._unset_global_current_playlist()
|
await self._unset_current_playlist()
|
||||||
logger.info("Unset global current playlist, main playlist is now fallback")
|
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."""
|
"""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:
|
if current_playlist:
|
||||||
await self.playlist_repo.update(current_playlist, {"is_current": False})
|
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,
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -222,28 +222,11 @@ class TestPlaylistRepository:
|
|||||||
test_session: AsyncSession,
|
test_session: AsyncSession,
|
||||||
ensure_plans: Any,
|
ensure_plans: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test getting current playlist when none is set."""
|
"""Test getting current playlist when none is set globally."""
|
||||||
# Create test user within this test
|
# Test the repository method - should return None when no current playlist is set globally
|
||||||
user = User(
|
playlist = await playlist_repository.get_current_playlist()
|
||||||
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)
|
|
||||||
|
|
||||||
# Extract user ID immediately after refresh
|
# Should return None since no playlist is marked as current globally
|
||||||
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
|
|
||||||
assert playlist is None
|
assert playlist is None
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|||||||
@@ -464,7 +464,7 @@ class TestPlaylistService:
|
|||||||
|
|
||||||
# Verify main playlist is now fallback current (main playlist doesn't have is_current=True)
|
# 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
|
# 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
|
assert current.is_main is True
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@@ -754,7 +754,7 @@ class TestPlaylistService:
|
|||||||
|
|
||||||
# Set test_playlist as current
|
# Set test_playlist as current
|
||||||
updated_playlist = await playlist_service.set_current_playlist(
|
updated_playlist = await playlist_service.set_current_playlist(
|
||||||
test_playlist_id, user_id,
|
test_playlist_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert updated_playlist.is_current is True
|
assert updated_playlist.is_current is True
|
||||||
@@ -804,10 +804,10 @@ class TestPlaylistService:
|
|||||||
assert current_playlist.is_current is True
|
assert current_playlist.is_current is True
|
||||||
|
|
||||||
# Unset current playlist
|
# 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
|
# 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.id == main_playlist_id
|
||||||
assert current.is_main is True
|
assert current.is_main is True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user