You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

91 regels
2.9 KiB

  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. )
  10. from schemas.fuelingtask import (
  11. CreateFuelingTask,
  12. OutputFuelingTask,
  13. OutputFuelingTaskMin,
  14. OutputFuelingTaskList,
  15. )
  16. from db.models.user import User
  17. from apis.v1.route_auth import get_current_user
  18. router = APIRouter()
  19. @router.post("/", response_model=OutputFuelingTask, status_code=status.HTTP_201_CREATED)
  20. def create_fuelingtask(
  21. fuelingtask: CreateFuelingTask = Depends(),
  22. db: Session = Depends(get_db),
  23. current_user: User = Depends(get_current_user),
  24. ):
  25. if current_user.Role != "FuelingPerson" and current_user.Role != "Admin":
  26. raise HTTPException(
  27. status_code=403, detail="You are not authorized to perform this action"
  28. )
  29. fuelingtask_res = create_fueling_task(
  30. fueling_task=fuelingtask, current_user=current_user.Id, db=db
  31. )
  32. print("Created FuelTask")
  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 != "FuelingPerson" 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