Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

route_fuelingtask.py 2.2 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from fastapi import Depends, APIRouter, HTTPException, status
  2. from sqlalchemy.orm import Session
  3. from db.session import get_db
  4. from db.repository.fuelingtask import (
  5. create_fueling_task,
  6. delete_fueling_task,
  7. get_fueling_task_by_id
  8. )
  9. from schemas.fuelingtask import CreateFuelingTask
  10. from db.models.user import User
  11. from apis.v1.route_auth import get_current_user
  12. router = APIRouter()
  13. @router.post("/", status_code=status.HTTP_201_CREATED)
  14. def create_fuelingtask(
  15. fuelingtask: CreateFuelingTask,
  16. db: Session = Depends(get_db),
  17. current_user: User = Depends(get_current_user),
  18. ):
  19. if current_user.Role != "FuelingPerson" and current_user.Role != "Admin":
  20. raise HTTPException(
  21. status_code=403, detail="You are not authorized to perform this action"
  22. )
  23. fuelingtask_res = create_fueling_task(
  24. fueling_task=fuelingtask, current_user=current_user.Id, db=db
  25. )
  26. if fuelingtask_res == "nodriver":
  27. raise HTTPException(
  28. status_code=404, detail="Driver ID not found"
  29. )
  30. if fuelingtask_res == "novehicle":
  31. raise HTTPException(
  32. status_code=404, detail="Vehicle ID not found"
  33. )
  34. return fuelingtask_res
  35. @router.get("/{fueling_task_id}", status_code=status.HTTP_200_OK)
  36. def get_fuelingtask(
  37. fueling_task_id: int,
  38. db: Session = Depends(get_db),
  39. current_user: User = Depends(get_current_user),
  40. ):
  41. if current_user.Role != "FuelingPerson" and current_user.Role != "Admin":
  42. raise HTTPException(
  43. status_code=403, detail="You are not authorized to perform this action"
  44. )
  45. fuelingtask = get_fueling_task_by_id(fueling_task_id, db)
  46. return fuelingtask
  47. @router.delete("/{fueling_task_id}", status_code=status.HTTP_204_NO_CONTENT)
  48. def delete_fuelingtask(
  49. fueling_task_id: int,
  50. db: Session = Depends(get_db),
  51. current_user: User = Depends(get_current_user),
  52. ):
  53. if current_user.Role != "FuelingPerson" and current_user.Role != "Admin":
  54. raise HTTPException(
  55. status_code=403, detail="You are not authorized to perform this action"
  56. )
  57. if not delete_fueling_task(fueling_task_id, db):
  58. raise HTTPException(
  59. status_code=404, detail="Fueling task not found"
  60. )