feat: Implement pagination for extractions and playlists with total count in responses
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Playlist management API endpoints."""
|
||||
|
||||
from typing import Annotated
|
||||
from typing import Annotated, Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
@@ -55,35 +55,29 @@ async def get_all_playlists( # noqa: PLR0913
|
||||
SortOrder,
|
||||
Query(description="Sort order (asc or desc)"),
|
||||
] = SortOrder.ASC,
|
||||
limit: Annotated[
|
||||
int | None,
|
||||
Query(description="Maximum number of results", ge=1, le=1000),
|
||||
] = None,
|
||||
offset: Annotated[
|
||||
int,
|
||||
Query(description="Number of results to skip", ge=0),
|
||||
] = 0,
|
||||
page: Annotated[int, Query(description="Page number", ge=1)] = 1,
|
||||
limit: Annotated[int, Query(description="Items per page", ge=1, le=100)] = 50,
|
||||
favorites_only: Annotated[
|
||||
bool,
|
||||
Query(description="Show only favorited playlists"),
|
||||
] = False,
|
||||
) -> list[PlaylistResponse]:
|
||||
) -> dict[str, Any]:
|
||||
"""Get all playlists from all users with search and sorting."""
|
||||
playlists = await playlist_service.search_and_sort_playlists(
|
||||
result = await playlist_service.search_and_sort_playlists_paginated(
|
||||
search_query=search,
|
||||
sort_by=sort_by,
|
||||
sort_order=sort_order,
|
||||
user_id=None,
|
||||
include_stats=True,
|
||||
page=page,
|
||||
limit=limit,
|
||||
offset=offset,
|
||||
favorites_only=favorites_only,
|
||||
current_user_id=current_user.id,
|
||||
)
|
||||
|
||||
# Convert to PlaylistResponse with favorite indicators
|
||||
playlist_responses = []
|
||||
for playlist_dict in playlists:
|
||||
for playlist_dict in result["playlists"]:
|
||||
# The playlist service returns dict, need to create playlist object-like structure
|
||||
is_favorited = await favorite_service.is_playlist_favorited(current_user.id, playlist_dict["id"])
|
||||
favorite_count = await favorite_service.get_playlist_favorite_count(playlist_dict["id"])
|
||||
@@ -98,7 +92,13 @@ async def get_all_playlists( # noqa: PLR0913
|
||||
}
|
||||
playlist_responses.append(playlist_response)
|
||||
|
||||
return playlist_responses
|
||||
return {
|
||||
"playlists": playlist_responses,
|
||||
"total": result["total"],
|
||||
"page": result["page"],
|
||||
"limit": result["limit"],
|
||||
"total_pages": result["total_pages"],
|
||||
}
|
||||
|
||||
|
||||
@router.get("/user")
|
||||
|
||||
Reference in New Issue
Block a user