From 5fca855975d5aa37964a9b1db84f4efea1452659 Mon Sep 17 00:00:00 2001 From: Saruar Date: Sat, 25 Nov 2023 20:37:07 +0600 Subject: [PATCH] Adding: Getting Fuel Tasks by Creator ID --- app/apis/base.py | 1 + app/apis/v1/route_fuelingtask.py | 15 +++++++++++++++ app/db/repository/fuelingtask.py | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/app/apis/base.py b/app/apis/base.py index 91fc0ca..8210c1c 100644 --- a/app/apis/base.py +++ b/app/apis/base.py @@ -23,3 +23,4 @@ api_router.include_router( api_router.include_router(route_fuelingtask.router, prefix="/fuel", tags=["fueltasks"]) api_router.include_router(route_auction.router, prefix="/auction", tags=["auctions"]) api_router.include_router(route_report.router, prefix="/report", tags=["reports"]) + diff --git a/app/apis/v1/route_fuelingtask.py b/app/apis/v1/route_fuelingtask.py index 6a39d23..3ec36a8 100644 --- a/app/apis/v1/route_fuelingtask.py +++ b/app/apis/v1/route_fuelingtask.py @@ -6,6 +6,7 @@ from db.repository.fuelingtask import ( delete_fueling_task, get_fueling_task_by_id, get_all_fueling_tasks, + get_fuel_tasks_by_creator, ) from schemas.fuelingtask import ( CreateFuelingTask, @@ -87,3 +88,17 @@ def get_all( ) tasks = get_all_fueling_tasks(db) return tasks + +@router.get("/creator/{creator_id}", response_model=OutputFuelingTaskList, status_code=status.HTTP_200_OK) +def get_fuel_tasks_by_creator_id( + creator_id: int, + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user), +): + if current_user.Role != "Admin" and current_user.Role != "Fueling": + raise HTTPException( + status_code=403, detail="You are not authorized to perform this action" + ) + + tasks = get_fuel_tasks_by_creator(creator_id, db) + return tasks diff --git a/app/db/repository/fuelingtask.py b/app/db/repository/fuelingtask.py index fca0ebc..e37b255 100644 --- a/app/db/repository/fuelingtask.py +++ b/app/db/repository/fuelingtask.py @@ -102,3 +102,21 @@ def get_all_fueling_tasks(db: Session): result.append(res) x = {"FuelingTasks": result} return x + +def get_fuel_tasks_by_creator(creator_id: int, db: Session): + print(f"Retrieving all fueling tasks created by user ID: {creator_id}") + fueling_tasks = db.query(FuelingTask).filter(FuelingTask.CreatedById == creator_id).all() + result = [] + + for task in fueling_tasks: + res = task.__dict__ + res["ImageBefore"] = None + res["ImageAfter"] = None + res["Creator"] = task.CreatedById + driver = get_user_by_id(task.DriverId, role="Driver", db=db) + driver_obj = driver.__dict__ + res["Driver"] = driver_obj + res["Driver"]["AssignedVehicle"] = driver.vehicle + result.append(res) + + return {"FuelingTasks": result}