Skip to content

Commit

Permalink
feat: Add optional filter for projects by current user
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Sep 9, 2024
1 parent 5009c99 commit f8b4724
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
15 changes: 14 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,25 @@ async def generate_presigned_url(
async def read_projects(
db: Annotated[Connection, Depends(database.get_db)],
user_data: Annotated[AuthUser, Depends(login_required)],
filter_by_owner: Optional[bool] = Query(
False, description="Filter projects by authenticated user (creator)"
),
skip: int = 0,
limit: int = 100,
):
"Get all projects with task count."

try:
return await project_schemas.DbProject.all(db, skip, limit)
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
)
if not projects:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="No projects found."
)

return projects
except KeyError as e:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND) from e

Expand Down
23 changes: 15 additions & 8 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,27 @@ async def one(db: Connection, project_id: uuid.UUID):

async def all(
db: Connection,
user_id: Optional[uuid.UUID] = None,
skip: int = 0,
limit: int = 100,
):
"""Get all projects."""
"""
Get all projects. Optionally filter by the project creator (user).
"""
async with db.cursor(row_factory=dict_row) as cur:
await cur.execute(
"""
SELECT id, slug, name, description, per_task_instructions, ST_AsGeoJSON(outline)::jsonb AS outline, requires_approval_from_manager_for_locking
FROM projects
ORDER BY created_at DESC
OFFSET %(skip)s
LIMIT %(limit)s
""",
{"skip": skip, "limit": limit},
SELECT
id, slug, name, description, per_task_instructions,
ST_AsGeoJSON(outline)::jsonb AS outline,
requires_approval_from_manager_for_locking
FROM projects
WHERE (author_id = COALESCE(%(user_id)s, author_id))
ORDER BY created_at DESC
OFFSET %(skip)s
LIMIT %(limit)s
""",
{"skip": skip, "limit": limit, "user_id": user_id},
)
db_projects = await cur.fetchall()
return db_projects
Expand Down

0 comments on commit f8b4724

Please sign in to comment.