feat: Implement pagination for extractions and playlists with total count in responses
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user