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

added search field to search projects from the db #991

Merged
merged 2 commits into from
Nov 20, 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
13 changes: 11 additions & 2 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def get_projects(
limit: int = 100,
db_objects: bool = False,
hashtags: List[str] = None,
search: str = None,
):
filters = []
if user_id:
Expand All @@ -89,6 +90,9 @@ def get_projects(
if hashtags:
filters.append(db_models.DbProject.hashtags.op("&&")(hashtags))

if search:
filters.append(db_models.DbProject.project_name_prefix.ilike(f"%{search}%"))

if len(filters) > 0:
db_projects = (
db.query(db_models.DbProject)
Expand All @@ -113,7 +117,12 @@ def get_projects(


def get_project_summaries(
db: Session, user_id: int, skip: int = 0, limit: int = 100, hashtags: str = None
db: Session,
user_id: int,
skip: int = 0,
limit: int = 100,
hashtags: str = None,
search: str = None,
):
# TODO: Just get summaries, something like:
# db_projects = db.query(db_models.DbProject).with_entities(
Expand All @@ -130,7 +139,7 @@ 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)
db_projects = get_projects(db, user_id, skip, limit, True, hashtags, search)
return convert_to_project_summaries(db_projects)


Expand Down
5 changes: 4 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ 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
Expand All @@ -135,7 +136,9 @@ async def read_project_summaries(
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)
projects = project_crud.get_project_summaries(
db, user_id, skip, limit, hashtags, search
)
project_summaries = [
project_schemas.ProjectSummary.from_db_project(project) for project in projects
]
Expand Down