From b0943bae7ddd2c52eda020c0803ecd5d82204217 Mon Sep 17 00:00:00 2001 From: Ana Rute Mendes Date: Wed, 13 Sep 2023 19:09:15 +0200 Subject: [PATCH] Add pagination and filter to the projects endpoint --- api/routers/v1/projects.py | 5 ++--- api/services/projects.py | 9 +++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/api/routers/v1/projects.py b/api/routers/v1/projects.py index 573ecab5d..0900ea8c4 100644 --- a/api/routers/v1/projects.py +++ b/api/routers/v1/projects.py @@ -12,10 +12,9 @@ router = APIRouter(prefix="/projects", tags=["projects"]) -# TODO implement pagination @router.get("/", dependencies=[Depends(BearerToken())], response_model=List[ProjectSchema]) -async def get_projects(db: Session = Depends(get_db), skip: int = 0, limit: int = 100): - return ProjectService(db).get_items() +async def get_projects(db: Session = Depends(get_db), offset: int = 0, limit: int = 100, status: str = None): + return ProjectService(db).get_items(offset, limit, status) @router.get("/{project_id}", dependencies=[Depends(BearerToken())], response_model=ProjectSchema) diff --git a/api/services/projects.py b/api/services/projects.py index 9a59cc399..42c325ea5 100644 --- a/api/services/projects.py +++ b/api/services/projects.py @@ -5,5 +5,10 @@ class ProjectService(AppService): - def get_items(self) -> List[Project]: - return self.db.query(Project).all() or [] + def get_items(self, offset, limit, status) -> List[Project]: + query = self.db.query(Project) + if status == "active": + query = query.filter(Project.activation is True) + if status == "inactive": + query = query.filter(Project.activation is False) + return query.offset(offset).limit(limit).all() or []