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

solve pagination error #997

Merged
merged 10 commits into from
Nov 23, 2023
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