feat: Enhance sound addition and removal in playlists with position handling and reordering
This commit is contained in:
@@ -264,12 +264,24 @@ class PlaylistService:
|
|||||||
detail="Sound is already in this playlist",
|
detail="Sound is already in this playlist",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# If position is None or beyond current positions, place at the end
|
||||||
|
if position is None:
|
||||||
|
current_sounds = await self.playlist_repo.get_playlist_sounds(playlist_id)
|
||||||
|
position = len(current_sounds)
|
||||||
|
else:
|
||||||
|
# Ensure position doesn't create gaps - if position is too high, place at end
|
||||||
|
current_sounds = await self.playlist_repo.get_playlist_sounds(playlist_id)
|
||||||
|
max_position = len(current_sounds)
|
||||||
|
if position > max_position:
|
||||||
|
position = max_position
|
||||||
|
|
||||||
await self.playlist_repo.add_sound_to_playlist(playlist_id, sound_id, position)
|
await self.playlist_repo.add_sound_to_playlist(playlist_id, sound_id, position)
|
||||||
logger.info(
|
logger.info(
|
||||||
"Added sound %s to playlist %s for user %s",
|
"Added sound %s to playlist %s for user %s at position %s",
|
||||||
sound_id,
|
sound_id,
|
||||||
playlist_id,
|
playlist_id,
|
||||||
user_id,
|
user_id,
|
||||||
|
position,
|
||||||
)
|
)
|
||||||
|
|
||||||
# If this is the current playlist, reload player
|
# If this is the current playlist, reload player
|
||||||
@@ -294,8 +306,12 @@ class PlaylistService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
await self.playlist_repo.remove_sound_from_playlist(playlist_id, sound_id)
|
await self.playlist_repo.remove_sound_from_playlist(playlist_id, sound_id)
|
||||||
|
|
||||||
|
# Reorder remaining sounds to eliminate gaps
|
||||||
|
await self._reorder_playlist_positions(playlist_id)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
"Removed sound %s from playlist %s for user %s",
|
"Removed sound %s from playlist %s for user %s and reordered positions",
|
||||||
sound_id,
|
sound_id,
|
||||||
playlist_id,
|
playlist_id,
|
||||||
user_id,
|
user_id,
|
||||||
@@ -305,6 +321,17 @@ class PlaylistService:
|
|||||||
if await _is_current_playlist(self.session, playlist_id):
|
if await _is_current_playlist(self.session, playlist_id):
|
||||||
await _reload_player_playlist()
|
await _reload_player_playlist()
|
||||||
|
|
||||||
|
async def _reorder_playlist_positions(self, playlist_id: int) -> None:
|
||||||
|
"""Reorder all sounds in a playlist to eliminate position gaps."""
|
||||||
|
sounds = await self.playlist_repo.get_playlist_sounds(playlist_id)
|
||||||
|
if not sounds:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Create sequential positions: 0, 1, 2, 3...
|
||||||
|
sound_positions = [(sound.id, index) for index, sound in enumerate(sounds)]
|
||||||
|
await self.playlist_repo.reorder_playlist_sounds(playlist_id, sound_positions)
|
||||||
|
logger.debug("Reordered %s sounds in playlist %s to eliminate gaps", len(sounds), playlist_id)
|
||||||
|
|
||||||
async def reorder_playlist_sounds(
|
async def reorder_playlist_sounds(
|
||||||
self,
|
self,
|
||||||
playlist_id: int,
|
playlist_id: int,
|
||||||
|
|||||||
Reference in New Issue
Block a user