Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

route_fuelingtask.py 2.4 KiB

pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
pirms 1 gada
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, OutputFuelingTask
  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}", response_model=OutputFuelingTask, 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. if fuelingtask == "notfound":
  47. raise HTTPException(
  48. status_code=404, detail="fuck off"
  49. )
  50. return fuelingtask
  51. @router.delete("/{fueling_task_id}", status_code=status.HTTP_204_NO_CONTENT)
  52. def delete_fuelingtask(
  53. fueling_task_id: int,
  54. db: Session = Depends(get_db),
  55. current_user: User = Depends(get_current_user),
  56. ):
  57. if current_user.Role != "FuelingPerson" and current_user.Role != "Admin":
  58. raise HTTPException(
  59. status_code=403, detail="You are not authorized to perform this action"
  60. )
  61. if not delete_fueling_task(fueling_task_id, db):
  62. raise HTTPException(
  63. status_code=404, detail="Fueling task not found"
  64. )