Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

route_fuelingtask.py 3.4 KiB

há 11 meses
há 11 meses
há 11 meses
há 11 meses
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. get_all_fueling_tasks,
  9. get_fuel_tasks_by_creator,
  10. )
  11. from schemas.fuelingtask import (
  12. CreateFuelingTask,
  13. OutputFuelingTask,
  14. OutputFuelingTaskMin,
  15. OutputFuelingTaskList,
  16. )
  17. from db.models.user import User
  18. from apis.v1.route_auth import get_current_user
  19. router = APIRouter()
  20. @router.post("/", response_model=OutputFuelingTask, status_code=status.HTTP_201_CREATED)
  21. def create_fuelingtask(
  22. fuelingtask: CreateFuelingTask = Depends(),
  23. db: Session = Depends(get_db),
  24. current_user: User = Depends(get_current_user),
  25. ):
  26. if current_user.Role != "Fueling" and current_user.Role != "Admin":
  27. raise HTTPException(
  28. status_code=403, detail="You are not authorized to perform this action"
  29. )
  30. fuelingtask_res = create_fueling_task(
  31. fueling_task=fuelingtask, current_user=current_user.Id, db=db
  32. )
  33. if fuelingtask_res == "nodriver":
  34. raise HTTPException(status_code=404, detail="Driver ID not found")
  35. if fuelingtask_res == "novehicle":
  36. raise HTTPException(status_code=404, detail="Vehicle ID not found")
  37. return fuelingtask_res
  38. @router.get(
  39. "/{fueling_task_id}",
  40. response_model=OutputFuelingTask,
  41. status_code=status.HTTP_200_OK,
  42. )
  43. def get_fuelingtask(
  44. fueling_task_id: int,
  45. db: Session = Depends(get_db),
  46. current_user: User = Depends(get_current_user),
  47. ):
  48. if current_user.Role != "Fueling" and current_user.Role != "Admin":
  49. raise HTTPException(
  50. status_code=403, detail="You are not authorized to perform this action"
  51. )
  52. fuelingtask = get_fueling_task_by_id(fueling_task_id, db)
  53. if fuelingtask == "notfound":
  54. raise HTTPException(status_code=404, detail="fuck off")
  55. return fuelingtask
  56. @router.delete("/{fueling_task_id}", status_code=status.HTTP_204_NO_CONTENT)
  57. def delete_fuelingtask(
  58. fueling_task_id: int,
  59. db: Session = Depends(get_db),
  60. current_user: User = Depends(get_current_user),
  61. ):
  62. if current_user.Role != "Fueling" and current_user.Role != "Admin":
  63. raise HTTPException(
  64. status_code=403, detail="You are not authorized to perform this action"
  65. )
  66. if not delete_fueling_task(fueling_task_id, db):
  67. raise HTTPException(status_code=404, detail="Fueling task not found")
  68. @router.get("/", response_model=OutputFuelingTaskList, status_code=status.HTTP_200_OK)
  69. def get_all(
  70. db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
  71. ):
  72. if current_user.Role != "Admin" and current_user.Role != "Fueling":
  73. raise HTTPException(
  74. status_code=403, detail="You are not authorized to perform this action"
  75. )
  76. tasks = get_all_fueling_tasks(db)
  77. return tasks
  78. @router.get("/creator/{creator_id}", response_model=OutputFuelingTaskList, status_code=status.HTTP_200_OK)
  79. def get_fuel_tasks_by_creator_id(
  80. creator_id: int,
  81. db: Session = Depends(get_db),
  82. current_user: User = Depends(get_current_user),
  83. ):
  84. if current_user.Role != "Admin" and current_user.Role != "Fueling":
  85. raise HTTPException(
  86. status_code=403, detail="You are not authorized to perform this action"
  87. )
  88. tasks = get_fuel_tasks_by_creator(creator_id, db)
  89. return tasks