diff --git a/src/api/qualicharge/api/v1/routers/static.py b/src/api/qualicharge/api/v1/routers/static.py index 359b61ff..b8f16110 100644 --- a/src/api/qualicharge/api/v1/routers/static.py +++ b/src/api/qualicharge/api/v1/routers/static.py @@ -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 @@ -48,12 +48,19 @@ 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 @@ -61,15 +68,24 @@ async def list( 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, )