Skip to content

Commit

Permalink
Merge pull request #263 from hotosm/feat/search-project
Browse files Browse the repository at this point in the history
feat: added search features in projects lists.
  • Loading branch information
nrjadkry authored Oct 4, 2024
2 parents 4109a1d + 5cea65a commit 17012de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ async def read_projects(
filter_by_owner: Optional[bool] = Query(
False, description="Filter projects by authenticated user (creator)"
),
search: Optional[str] = Query(None, description="Search projects by name"),
skip: int = 0,
limit: int = 100,
):
Expand All @@ -317,7 +318,7 @@ async def read_projects(
try:
user_id = user_data.id if filter_by_owner else None
projects = await project_schemas.DbProject.all(
db, user_id=user_id, skip=skip, limit=limit
db, user_id=user_id, search=search, skip=skip, limit=limit
)
if not projects:
return []
Expand Down
12 changes: 10 additions & 2 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ async def one(db: Connection, project_id: uuid.UUID):
async def all(
db: Connection,
user_id: Optional[str] = None,
search: Optional[str] = None,
skip: int = 0,
limit: int = 100,
):
"""
Get all projects, count total tasks and task states (ongoing, completed, etc.).
Optionally filter by the project creator (user).
Optionally filter by the project creator (user) and search by project name.
"""
search_term = f"%{search}%" if search else "%"
async with db.cursor(row_factory=dict_row) as cur:
await cur.execute(
"""
Expand Down Expand Up @@ -319,12 +321,18 @@ async def all(
) AS te ON te.task_id = t.id
WHERE (p.author_id = COALESCE(%(user_id)s, p.author_id))
AND p.name ILIKE %(search)s
GROUP BY p.id
ORDER BY p.created_at DESC
OFFSET %(skip)s
LIMIT %(limit)s
""",
{"skip": skip, "limit": limit, "user_id": user_id},
{
"skip": skip,
"limit": limit,
"user_id": user_id,
"search": search_term,
},
)
db_projects = await cur.fetchall()
return db_projects
Expand Down

0 comments on commit 17012de

Please sign in to comment.