Skip to content

Commit

Permalink
Merge pull request #991 from hotosm/search-projects
Browse files Browse the repository at this point in the history
added search field to search projects from the db
  • Loading branch information
nrjadkry authored Nov 20, 2023
2 parents 12ecbc7 + 674bb37 commit d4f803c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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

0 comments on commit d4f803c

Please sign in to comment.