|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 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
- )
- from schemas.fuelingtask import CreateFuelingTask
- from db.models.user import User
- from apis.v1.route_auth import get_current_user
-
- router = APIRouter()
-
- @router.post("/", status_code=status.HTTP_201_CREATED)
- def create_fuelingtask(
- fuelingtask: CreateFuelingTask,
- db: Session = Depends(get_db),
- current_user: User = Depends(get_current_user),
- ):
- if current_user.Role != "FuelingPerson" 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}", 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 != "FuelingPerson" 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)
- 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 != "FuelingPerson" 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"
- )
|