Skip to content

Commit

Permalink
feat: added pagination on list tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Aug 20, 2024
1 parent 0c564ae commit cee7adc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/backend/app/tasks/task_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ async def get_task_geojson(db: Database, task_id: uuid.UUID):
return json.loads(data["geom"])


async def get_tasks_by_user(user_id: str, db: Database, role: str):
async def get_tasks_by_user(
user_id: str, db: Database, role: str, skip: int = 0, limit: int = 50
):
try:
query = """SELECT DISTINCT ON (tasks.id)
tasks.id AS task_id,
Expand All @@ -61,9 +63,14 @@ async def get_tasks_by_user(user_id: str, db: Database, role: str):
:role != 'DRONE_PILOT' AND task_events.project_id IN (SELECT id FROM projects WHERE author_id = :user_id)
)
ORDER BY
tasks.id, task_events.created_at DESC;
tasks.id, task_events.created_at DESC
OFFSET :skip
LIMIT :limit;
"""
records = await db.fetch_all(query, values={"user_id": user_id, "role": role})
records = await db.fetch_all(
query,
values={"user_id": user_id, "role": role, "skip": skip, "limit": limit},
)
return records

except Exception as e:
Expand Down
4 changes: 3 additions & 1 deletion src/backend/app/tasks/task_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ async def get_task_stats(
async def list_tasks(
db: Database = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),
skip: int = 0,
limit: int = 50,
):
"""Get all tasks for a all user."""

Expand All @@ -130,7 +132,7 @@ async def list_tasks(
if not records:
raise HTTPException(status_code=404, detail="User profile not found")

return await task_crud.get_tasks_by_user(user_id, db, role)
return await task_crud.get_tasks_by_user(user_id, db, role, skip, limit)


@router.get("/states/{project_id}")
Expand Down

0 comments on commit cee7adc

Please sign in to comment.