|
- from fastapi import Depends, APIRouter, HTTPException, status
- from sqlalchemy.orm import Session
- from db.session import get_db
- from db.repository.fuelingtask import (
- create_fueling_task,
- delete_fueling_task,
- get_fueling_task_by_id,
- get_all_fueling_tasks,
- get_fuel_tasks_by_creator,
- )
- from schemas.fuelingtask import (
- CreateFuelingTask,
- OutputFuelingTask,
- OutputFuelingTaskMin,
- OutputFuelingTaskList,
- )
- from db.models.user import User
- from apis.v1.route_auth import get_current_user
-
- router = APIRouter()
-
-
- @router.post("/", response_model=OutputFuelingTask, status_code=status.HTTP_201_CREATED)
- def create_fuelingtask(
- fuelingtask: CreateFuelingTask = Depends(),
- db: Session = Depends(get_db),
- current_user: User = Depends(get_current_user),
- ):
- if current_user.Role != "Fueling" and current_user.Role != "Admin":
- raise HTTPException(
- status_code=403, detail="You are not authorized to perform this action"
- )
-
- fuelingtask_res = create_fueling_task(
- fueling_task=fuelingtask, current_user=current_user.Id, db=db
- )
- if fuelingtask_res == "nodriver":
- raise HTTPException(status_code=404, detail="Driver ID not found")
-
- if fuelingtask_res == "novehicle":
- raise HTTPException(status_code=404, detail="Vehicle ID not found")
- return fuelingtask_res
-
-
- @router.get(
- "/{fueling_task_id}",
- response_model=OutputFuelingTask,
- status_code=status.HTTP_200_OK,
- )
- def get_fuelingtask(
- fueling_task_id: int,
- db: Session = Depends(get_db),
- current_user: User = Depends(get_current_user),
- ):
- if current_user.Role != "Fueling" and current_user.Role != "Admin":
- raise HTTPException(
- status_code=403, detail="You are not authorized to perform this action"
- )
-
- fuelingtask = get_fueling_task_by_id(fueling_task_id, db)
- if fuelingtask == "notfound":
- raise HTTPException(status_code=404, detail="fuck off")
- return fuelingtask
-
-
- @router.delete("/{fueling_task_id}", status_code=status.HTTP_204_NO_CONTENT)
- def delete_fuelingtask(
- fueling_task_id: int,
- db: Session = Depends(get_db),
- current_user: User = Depends(get_current_user),
- ):
- if current_user.Role != "Fueling" and current_user.Role != "Admin":
- raise HTTPException(
- status_code=403, detail="You are not authorized to perform this action"
- )
-
- if not delete_fueling_task(fueling_task_id, db):
- raise HTTPException(status_code=404, detail="Fueling task not found")
-
-
- @router.get("/", response_model=OutputFuelingTaskList, status_code=status.HTTP_200_OK)
- def get_all(
- 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_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
|