Skip to content

Commit

Permalink
Add pagination and filter to the projects endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
anarute committed Sep 18, 2023
1 parent 6e05e8d commit b0943ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 2 additions & 3 deletions api/routers/v1/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions api/services/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []

0 comments on commit b0943ba

Please sign in to comment.