feat: Enhance timestamp management in BaseModel and PlaylistRepository; add automatic updates and improve code readability
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Playlist repository for database operations."""
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from enum import Enum
|
||||
|
||||
from sqlalchemy import func, update
|
||||
@@ -42,6 +43,23 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
||||
"""Initialize the playlist repository."""
|
||||
super().__init__(Playlist, session)
|
||||
|
||||
async def _update_playlist_timestamp(self, playlist_id: int) -> None:
|
||||
"""Update the playlist's updated_at timestamp."""
|
||||
try:
|
||||
update_stmt = (
|
||||
update(Playlist)
|
||||
.where(Playlist.id == playlist_id)
|
||||
.values(updated_at=datetime.now(UTC))
|
||||
)
|
||||
await self.session.exec(update_stmt)
|
||||
# Note: No commit here - let the calling method handle transaction
|
||||
# management
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to update playlist timestamp for playlist: %s", playlist_id,
|
||||
)
|
||||
raise
|
||||
|
||||
async def get_by_name(self, name: str) -> Playlist | None:
|
||||
"""Get a playlist by name."""
|
||||
try:
|
||||
@@ -133,7 +151,8 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
||||
return list(result.all())
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to get playlist sound entries for playlist: %s", playlist_id,
|
||||
"Failed to get playlist sound entries for playlist: %s",
|
||||
playlist_id,
|
||||
)
|
||||
raise
|
||||
|
||||
@@ -188,6 +207,9 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
||||
position=position,
|
||||
)
|
||||
self.session.add(playlist_sound)
|
||||
|
||||
# Update playlist timestamp before commit
|
||||
await self._update_playlist_timestamp(playlist_id)
|
||||
await self.session.commit()
|
||||
await self.session.refresh(playlist_sound)
|
||||
except Exception:
|
||||
@@ -219,6 +241,9 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
||||
|
||||
if playlist_sound:
|
||||
await self.session.delete(playlist_sound)
|
||||
|
||||
# Update playlist timestamp before commit
|
||||
await self._update_playlist_timestamp(playlist_id)
|
||||
await self.session.commit()
|
||||
logger.info("Removed sound %s from playlist %s", sound_id, playlist_id)
|
||||
except Exception:
|
||||
@@ -268,6 +293,8 @@ class PlaylistRepository(BaseRepository[Playlist]):
|
||||
if playlist_sound:
|
||||
playlist_sound.position = new_position
|
||||
|
||||
# Update playlist timestamp before commit
|
||||
await self._update_playlist_timestamp(playlist_id)
|
||||
await self.session.commit()
|
||||
logger.info("Reordered sounds in playlist %s", playlist_id)
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user