From 5cea65a90456e4db7e13bbeaa0d1984e9d0a905a Mon Sep 17 00:00:00 2001 From: Pradip-p Date: Fri, 4 Oct 2024 11:31:36 +0545 Subject: [PATCH] feat: added search features in projects read endpoit --- src/backend/app/projects/project_routes.py | 3 ++- src/backend/app/projects/project_schemas.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index a10b7fb7..422f62b7 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -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, ): @@ -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 [] diff --git a/src/backend/app/projects/project_schemas.py b/src/backend/app/projects/project_schemas.py index 13902a21..d280410c 100644 --- a/src/backend/app/projects/project_schemas.py +++ b/src/backend/app/projects/project_schemas.py @@ -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( """ @@ -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