Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination and filter to the projects endpoint #670

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Project(Base):
autoincrement=True,
nullable=False,
)
activation = Column(Boolean, default=True, nullable=False)
is_active = Column("activation", Boolean, default=True, nullable=False)
init = Column(Date, nullable=True)
end = Column("_end", Date, nullable=True)
invoice = Column(Double, nullable=True)
Expand Down
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
2 changes: 1 addition & 1 deletion api/schemas/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Project(BaseModel):
id: str
activation: bool
is_active: bool
init: Optional[date]
end: Optional[date]
invoice: Optional[float]
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 []