feat: Implement pagination for extractions and playlists with total count in responses
This commit is contained in:
@@ -38,6 +38,16 @@ class ExtractionInfo(TypedDict):
|
||||
updated_at: str
|
||||
|
||||
|
||||
class PaginatedExtractionsResponse(TypedDict):
|
||||
"""Type definition for paginated extractions response."""
|
||||
|
||||
extractions: list[ExtractionInfo]
|
||||
total: int
|
||||
page: int
|
||||
limit: int
|
||||
total_pages: int
|
||||
|
||||
|
||||
class ExtractionService:
|
||||
"""Service for extracting audio from external services using yt-dlp."""
|
||||
|
||||
@@ -565,17 +575,22 @@ class ExtractionService:
|
||||
sort_by: str = "created_at",
|
||||
sort_order: str = "desc",
|
||||
status_filter: str | None = None,
|
||||
) -> list[ExtractionInfo]:
|
||||
page: int = 1,
|
||||
limit: int = 50,
|
||||
) -> PaginatedExtractionsResponse:
|
||||
"""Get all extractions for a user with filtering, search, and sorting."""
|
||||
extraction_user_tuples = await self.extraction_repo.get_user_extractions_filtered(
|
||||
offset = (page - 1) * limit
|
||||
extraction_user_tuples, total_count = await self.extraction_repo.get_user_extractions_filtered(
|
||||
user_id=user_id,
|
||||
search=search,
|
||||
sort_by=sort_by,
|
||||
sort_order=sort_order,
|
||||
status_filter=status_filter,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
|
||||
return [
|
||||
extractions = [
|
||||
{
|
||||
"id": extraction.id
|
||||
or 0, # Should never be None for existing extraction
|
||||
@@ -594,22 +609,37 @@ class ExtractionService:
|
||||
for extraction, user in extraction_user_tuples
|
||||
]
|
||||
|
||||
total_pages = (total_count + limit - 1) // limit # Ceiling division
|
||||
|
||||
return {
|
||||
"extractions": extractions,
|
||||
"total": total_count,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total_pages": total_pages,
|
||||
}
|
||||
|
||||
async def get_all_extractions(
|
||||
self,
|
||||
search: str | None = None,
|
||||
sort_by: str = "created_at",
|
||||
sort_order: str = "desc",
|
||||
status_filter: str | None = None,
|
||||
) -> list[ExtractionInfo]:
|
||||
page: int = 1,
|
||||
limit: int = 50,
|
||||
) -> PaginatedExtractionsResponse:
|
||||
"""Get all extractions with filtering, search, and sorting."""
|
||||
extraction_user_tuples = await self.extraction_repo.get_all_extractions_filtered(
|
||||
offset = (page - 1) * limit
|
||||
extraction_user_tuples, total_count = await self.extraction_repo.get_all_extractions_filtered(
|
||||
search=search,
|
||||
sort_by=sort_by,
|
||||
sort_order=sort_order,
|
||||
status_filter=status_filter,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
)
|
||||
|
||||
return [
|
||||
extractions = [
|
||||
{
|
||||
"id": extraction.id
|
||||
or 0, # Should never be None for existing extraction
|
||||
@@ -628,6 +658,16 @@ class ExtractionService:
|
||||
for extraction, user in extraction_user_tuples
|
||||
]
|
||||
|
||||
total_pages = (total_count + limit - 1) // limit # Ceiling division
|
||||
|
||||
return {
|
||||
"extractions": extractions,
|
||||
"total": total_count,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total_pages": total_pages,
|
||||
}
|
||||
|
||||
async def get_pending_extractions(self) -> list[ExtractionInfo]:
|
||||
"""Get all pending extractions."""
|
||||
extraction_user_tuples = await self.extraction_repo.get_pending_extractions()
|
||||
|
||||
Reference in New Issue
Block a user