feat: Implement playlist sound deletion and update current playlist logic on deletion
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
from typing import Any, TypedDict
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlmodel import select
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
from app.core.logging import get_logger
|
||||
from app.models.playlist import Playlist
|
||||
from app.models.playlist_sound import PlaylistSound
|
||||
from app.models.sound import Sound
|
||||
from app.repositories.playlist import PlaylistRepository, PlaylistSortField, SortOrder
|
||||
from app.repositories.sound import SoundRepository
|
||||
@@ -231,11 +233,20 @@ class PlaylistService:
|
||||
# Check if this was the current playlist before deleting
|
||||
was_current = playlist.is_current
|
||||
|
||||
# First, delete all playlist_sound relationships
|
||||
await self._delete_playlist_sounds(playlist_id)
|
||||
|
||||
# Then delete the playlist itself
|
||||
await self.playlist_repo.delete(playlist)
|
||||
logger.info("Deleted playlist %s for user %s", playlist_id, user_id)
|
||||
|
||||
# If the deleted playlist was current, reload player to use main fallback
|
||||
# If the deleted playlist was current, set main playlist as current
|
||||
if was_current:
|
||||
main_playlist = await self.get_main_playlist()
|
||||
await self.playlist_repo.update(main_playlist, {"is_current": True})
|
||||
logger.info("Set main playlist as current after deleting current playlist %s", playlist_id)
|
||||
|
||||
# Reload player to reflect the change
|
||||
await _reload_player_playlist()
|
||||
|
||||
async def search_playlists(self, query: str, user_id: int) -> list[Playlist]:
|
||||
@@ -539,6 +550,20 @@ class PlaylistService:
|
||||
# Reload player playlist to reflect the change (will fallback to main)
|
||||
await _reload_player_playlist()
|
||||
|
||||
async def _delete_playlist_sounds(self, playlist_id: int) -> None:
|
||||
"""Delete all playlist_sound records for a given playlist."""
|
||||
# Get all playlist_sound records for this playlist
|
||||
stmt = select(PlaylistSound).where(PlaylistSound.playlist_id == playlist_id)
|
||||
result = await self.session.exec(stmt)
|
||||
playlist_sounds = result.all()
|
||||
|
||||
# Delete each playlist_sound record
|
||||
for playlist_sound in playlist_sounds:
|
||||
await self.session.delete(playlist_sound)
|
||||
|
||||
await self.session.commit()
|
||||
logger.info("Deleted %d playlist_sound records for playlist %s", len(playlist_sounds), playlist_id)
|
||||
|
||||
async def _unset_current_playlist(self) -> None:
|
||||
"""Unset any current playlist globally."""
|
||||
current_playlist = await self.playlist_repo.get_current_playlist()
|
||||
|
||||
Reference in New Issue
Block a user