Skip to content

Commit

Permalink
feat: added Pagination info in the project summary (#959)
Browse files Browse the repository at this point in the history
* feat: paginated summary of projects

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: sujanadh <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 7, 2023
1 parent 176ade8 commit 02d1d42
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ def get_task(lat: float, long: float, user_id: int = None):
return "Coming..."


@router.get("/summaries", response_model=List[project_schemas.ProjectSummary])
@router.get("/summaries", response_model=project_schemas.PaginatedProjectSummaries)
async def read_project_summaries(
user_id: int = None,
hashtags: str = None,
skip: int = 0,
limit: int = 100,
page: int = Query(1, ge=1), # Default to page 1, must be greater than or equal to 1
results_per_page: int = Query(13, le=100),
db: Session = Depends(database.get_db),
):
if hashtags:
Expand All @@ -123,8 +123,33 @@ async def read_project_summaries(
filter(lambda hashtag: hashtag.startswith("#"), hashtags)
) # filter hashtags that do start with #

skip = (page - 1) * results_per_page
limit = results_per_page

total_projects = db.query(db_models.DbProject).count()
hasNext = (page * results_per_page) < total_projects
hasPrev = page > 1
total_pages = (total_projects + results_per_page - 1) // results_per_page

projects = project_crud.get_project_summaries(db, user_id, skip, limit, hashtags)
return projects
project_summaries = [
project_schemas.ProjectSummary.from_db_project(project) for project in projects
]

response = project_schemas.PaginatedProjectSummaries(
results=project_summaries,
pagination=project_schemas.PaginationInfo(
hasNext=hasNext,
hasPrev=hasPrev,
nextNum=page + 1 if hasNext else None,
page=page,
pages=total_pages,
prevNum=page - 1 if hasPrev else None,
perPage=results_per_page,
total=total_projects,
),
)
return response


@router.get("/{project_id}", response_model=project_schemas.ProjectOut)
Expand Down
40 changes: 40 additions & 0 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from geojson_pydantic import Feature as GeojsonFeature
from pydantic import BaseModel

from ..db import db_models
from ..models.enums import ProjectPriority, ProjectStatus
from ..tasks import tasks_schemas
from ..users.user_schemas import User
Expand Down Expand Up @@ -76,6 +77,45 @@ class ProjectSummary(BaseModel):
organisation_id: Optional[int] = None
organisation_logo: Optional[str] = None

@classmethod
def from_db_project(
cls,
project: db_models.DbProject,
) -> "ProjectSummary":
priority = project.priority
return cls(
id=project.id,
priority=priority,
priority_str=priority.name,
title=project.title,
location_str=project.location_str,
description=project.description,
total_tasks=project.total_tasks,
tasks_mapped=project.tasks_mapped,
num_contributors=project.num_contributors,
tasks_validated=project.tasks_validated,
tasks_bad=project.tasks_bad,
hashtags=project.hashtags,
organisation_id=project.organisation_id,
organisation_logo=project.organisation_logo,
)


class PaginationInfo(BaseModel):
hasNext: bool
hasPrev: bool
nextNum: Optional[int]
page: int
pages: int
prevNum: Optional[int]
perPage: int
total: int


class PaginatedProjectSummaries(BaseModel):
results: List[ProjectSummary]
pagination: PaginationInfo


class ProjectBase(BaseModel):
id: int
Expand Down

0 comments on commit 02d1d42

Please sign in to comment.