9696 5 . [ Alembic Migrations] ( #55-alembic-migrations )
9797 6 . [ CRUD] ( #56-crud )
9898 7 . [ Routes] ( #57-routes )
99+ 1 . [ Paginated Responses] ( #571-paginated-responses )
99100 8 . [ Caching] ( #58-caching )
100101 9 . [ More Advanced Caching] ( #59-more-advanced-caching )
101102 10 . [ ARQ Job Queues] ( #510-arq-job-queues )
@@ -690,10 +691,15 @@ from app.core.database import async_get_db
690691
691692router = fastapi.APIRouter(tags = [" entities" ])
692693
693- @router.get (" /entities" , response_model = List[EntityRead])
694- async def read_entities (db : Annotated[AsyncSession, Depends(async_get_db)]):
695- entities = await crud_entities.get_multi(db = db)
696- return entities
694+ @router.get (" /entities/{id} " , response_model = List[EntityRead])
695+ async def read_entities (
696+ request : Request,
697+ id : int ,
698+ db : Annotated[AsyncSession, Depends(async_get_db)]
699+ ):
700+ entity = await crud_entities.get(db = db, id = id )
701+
702+ return entity
697703
698704...
699705```
@@ -708,6 +714,71 @@ router = APIRouter(prefix="/v1") # this should be there already
708714router.include_router(entity_router)
709715```
710716
717+ #### 5.7.1 Paginated Responses
718+ With the ` get_multi ` method we get a python ` dict ` with full suport for pagination:
719+ ``` javascript
720+ {
721+ " data" : [
722+ {
723+ " id" : 4 ,
724+ " name" : " User Userson" ,
725+ " username" : " userson4" ,
726+ 727+ " profile_image_url" : " https://profileimageurl.com"
728+ },
729+ {
730+ " id" : 5 ,
731+ " name" : " User Userson" ,
732+ " username" : " userson5" ,
733+ 734+ " profile_image_url" : " https://profileimageurl.com"
735+ }
736+ ],
737+ " total_count" : 2 ,
738+ " has_more" : false ,
739+ " page" : 1 ,
740+ " items_per_page" : 10
741+ }
742+ ```
743+
744+ And in the endpoint, we can import from ` app/api/paginated ` the following functions and Pydantic Schema:
745+ ``` python
746+ from app.api.paginated import (
747+ PaginatedListResponse, # What you'll use as a response_model to validate
748+ paginated_response, # Creates a paginated response based on the parameters
749+ compute_offset # Calculate the offset for pagination ((page - 1) * items_per_page)
750+ )
751+ ```
752+
753+ Then let's create the endpoint:
754+ ``` python
755+ import fastapi
756+
757+ from app.schemas.entity imoport EntityRead
758+ ...
759+
760+ @router.get (" /entities" , response_model = PaginatedListResponse[EntityRead])
761+ async def read_entities (
762+ request : Request,
763+ db : Annotated[AsyncSession, Depends(async_get_db)],
764+ page : int = 1 ,
765+ items_per_page : int = 10
766+ ):
767+ entities_data = await crud_entity.get_multi(
768+ db = db,
769+ offset = compute_offset(page, items_per_page),
770+ limit = items_per_page,
771+ schema_to_select = UserRead,
772+ is_deleted = False
773+ )
774+
775+ return paginated_response(
776+ crud_data = entities_data,
777+ page = page,
778+ items_per_page = items_per_page
779+ )
780+ ```
781+
711782### 5.8 Caching
712783The ` cache ` decorator allows you to cache the results of FastAPI endpoint functions, enhancing response times and reducing the load on your application by storing and retrieving data in a cache.
713784
0 commit comments