feat: Implement pagination for extractions and playlists with total count in responses

This commit is contained in:
JSC
2025-08-17 11:21:55 +02:00
parent f598ec2c12
commit 99c757a073
6 changed files with 177 additions and 51 deletions

View File

@@ -343,7 +343,9 @@ class PlaylistRepository(BaseRepository[Playlist]):
offset: int = 0,
favorites_only: bool = False,
current_user_id: int | None = None,
) -> list[dict]:
*,
return_count: bool = False,
) -> list[dict] | tuple[list[dict], int]:
"""Search and sort playlists with optional statistics."""
try:
if include_stats and sort_by in (
@@ -491,6 +493,14 @@ class PlaylistRepository(BaseRepository[Playlist]):
# Default sorting by name ascending
subquery = subquery.order_by(Playlist.name.asc())
# Get total count if requested
total_count = 0
if return_count:
# Create count query from the subquery before pagination
count_query = select(func.count()).select_from(subquery.subquery())
count_result = await self.session.exec(count_query)
total_count = count_result.one()
# Apply pagination
if offset > 0:
subquery = subquery.offset(offset)
@@ -532,4 +542,6 @@ class PlaylistRepository(BaseRepository[Playlist]):
)
raise
else:
if return_count:
return playlists, total_count
return playlists