Skip to content

Commit

Permalink
fix: solve pagination error (hotosm#997)
Browse files Browse the repository at this point in the history
* fix : pagination error

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

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

* separate pagination function for reusability

* created search api

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

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

* minor fixes

---------

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 and nischalstha9 committed Dec 8, 2023
1 parent f54fa3a commit c88f056
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
31 changes: 27 additions & 4 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def get_projects(
.limit(limit)
.all()
)
project_count = db.query(db_models.DbProject).filter(and_(*filters)).count()

else:
db_projects = (
Expand All @@ -111,9 +112,10 @@ def get_projects(
.limit(limit)
.all()
)
project_count = db.query(db_models.DbProject).count()
if db_objects:
return db_projects
return convert_to_app_projects(db_projects)
return project_count, db_projects
return project_count, convert_to_app_projects(db_projects)


def get_project_summaries(
Expand All @@ -139,8 +141,10 @@ def get_project_summaries(
# .filter(
# db_models.DbProject.author_id == user_id).offset(skip).limit(limit).all()

db_projects = get_projects(db, user_id, skip, limit, True, hashtags, search)
return convert_to_project_summaries(db_projects)
project_count, db_projects = get_projects(
db, user_id, skip, limit, True, hashtags, search
)
return project_count, convert_to_project_summaries(db_projects)


def get_project(db: Session, project_id: int):
Expand Down Expand Up @@ -2854,3 +2858,22 @@ def get_tasks_count(db: Session, project_id: int):
)
task_count = len(db_task.tasks)
return task_count


def get_pagintaion(page: int, count: int, results_per_page: int, total: int):
total_pages = (count + results_per_page - 1) // results_per_page
hasNext = (page * results_per_page) < count
hasPrev = page > 1

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,
)

return pagination
59 changes: 44 additions & 15 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,41 +123,70 @@ async def read_project_summaries(
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),
search: str = None,
):
if hashtags:
hashtags = hashtags.split(",") # create list of hashtags
hashtags = list(
filter(lambda hashtag: hashtag.startswith("#"), hashtags)
) # filter hashtags that do start with #

total_projects = db.query(db_models.DbProject).count()
skip = (page - 1) * results_per_page
limit = results_per_page

project_count, projects = project_crud.get_project_summaries(
db, user_id, skip, limit, hashtags, None
)

pagination = project_crud.get_pagintaion(
page, project_count, results_per_page, total_projects
)
project_summaries = [
project_schemas.ProjectSummary.from_db_project(project) for project in projects
]

response = project_schemas.PaginatedProjectSummaries(
results=project_summaries,
pagination=pagination,
)
return response


@router.get(
"/search_projects", response_model=project_schemas.PaginatedProjectSummaries
)
async def search_project(
search: str,
user_id: int = None,
hashtags: str = None,
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:
hashtags = hashtags.split(",") # create list of hashtags
hashtags = list(
filter(lambda hashtag: hashtag.startswith("#"), hashtags)
) # filter hashtags that do start with #

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
skip = (page - 1) * results_per_page
limit = results_per_page

projects = project_crud.get_project_summaries(
project_count, projects = project_crud.get_project_summaries(
db, user_id, skip, limit, hashtags, search
)

pagination = project_crud.get_pagintaion(
page, project_count, results_per_page, total_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,
),
pagination=pagination,
)
return response

Expand Down

0 comments on commit c88f056

Please sign in to comment.