Skip to content

Commit

Permalink
WIP: add statique list full pagination support
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaupetit committed May 1, 2024
1 parent 6ddd405 commit 81a49b1
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/api/qualicharge/api/v1/routers/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Annotated, List, Optional

from annotated_types import Len
from fastapi import APIRouter, Depends, HTTPException, Path, Query, status
from fastapi import APIRouter, Depends, HTTPException, Path, Query, Request, status
from pydantic import AnyHttpUrl, BaseModel, computed_field
from sqlmodel import Session, select

Expand Down Expand Up @@ -48,28 +48,44 @@ class PaginatedStatiqueListResponse(BaseModel):
next: Optional[AnyHttpUrl]
items: List[Statique]

@computed_field # type: ignore[misc]
@property
def size(self) -> int:
"""The number of items created."""
return len(self.items)


BulkStatiqueList = Annotated[List[Statique], Len(2, settings.API_BULK_CREATE_MAX_SIZE)]


@router.get("/")
async def list(
request: Request,
offset: int = 0,
limit: int = Query(
default=settings.API_BULK_CREATE_MAX_SIZE, le=settings.API_BULK_CREATE_MAX_SIZE
),
session: Session = Depends(get_session),
) -> PaginatedStatiqueListResponse:
"""List statique items."""
logger.debug(f"{offset=}")
next_url = "http://localhost"
previous_url = "http://localhost"
current_url = request.url
previous_url = next_url = None

statiques = [statique for statique in list_statique(session, offset, limit)]

previous_offset = offset - limit if offset > limit else 0
if offset:
previous_url = str(current_url.include_query_params(offset=previous_offset))

if not limit > len(statiques):
next_url = str(current_url.include_query_params(offset=offset + limit))

return PaginatedStatiqueListResponse(
limit=limit,
offset=offset,
previous=previous_url,
next=next_url,
items=[statique for statique in list_statique(session, offset, limit)],
items=statiques,
)


Expand Down

0 comments on commit 81a49b1

Please sign in to comment.