Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

124 lignes
3.9 KiB

  1. from fastapi import Depends, APIRouter
  2. from sqlalchemy.orm import Session
  3. from fastapi import status, HTTPException
  4. from typing import Annotated
  5. from db.session import get_db
  6. from core.config import settings
  7. from db.repository.drivetask import (
  8. create_new_task,
  9. get_task_driver,
  10. change_task_status,
  11. get_all_tasks,
  12. get_task_by_id,
  13. get_tasks_by_driver,
  14. )
  15. from schemas.drivetask import CreateTask
  16. from db.models.user import User
  17. from apis.v1.route_auth import get_current_user
  18. from db.models.drivetask import DriveTask
  19. router = APIRouter()
  20. @router.post("/", status_code=status.HTTP_201_CREATED)
  21. def create_task(
  22. task: CreateTask,
  23. db: Session = Depends(get_db),
  24. current_user: User = Depends(get_current_user),
  25. ):
  26. if current_user.Role != "Admin":
  27. raise HTTPException(
  28. status_code=403, detail="You are not authorized to perform this action"
  29. )
  30. task_res = create_new_task(task=task, db=db)
  31. if task_res == "notdriver":
  32. raise HTTPException(
  33. status_code=404, detail=f"Driver with id {task.DriverId} not found"
  34. )
  35. return task
  36. @router.patch("/", status_code=status.HTTP_200_OK)
  37. def changeStatus(
  38. task_id: int,
  39. status: str,
  40. db: Session = Depends(get_db),
  41. current_user: User = Depends(get_current_user),
  42. ):
  43. if current_user.Role == "Admin" or current_user.Role == "Driver":
  44. if status not in settings.ALLOWED_TASK_STATUS:
  45. raise HTTPException(
  46. status_code=400,
  47. detail=f"Status {status} is not allowed. Allowed status are {settings.ALLOWED_TASK_STATUS}",
  48. )
  49. if current_user.Role == "Driver":
  50. verification = get_task_driver(task_id, db)
  51. if verification.Id != current_user.Id:
  52. raise HTTPException(
  53. status_code=403,
  54. detail="You are not authorized to perform this action",
  55. )
  56. task = change_task_status(task_id, status, db)
  57. if task == "notaskfound":
  58. raise HTTPException(
  59. status_code=404, detail=f"Task with id {task_id} not found"
  60. )
  61. return task
  62. else:
  63. raise HTTPException(
  64. status_code=403, detail="You are not authorized to perform this action"
  65. )
  66. @router.get("/", status_code=status.HTTP_200_OK)
  67. def getAllTasks(
  68. db: Session = Depends(get_db),
  69. current_user: User = Depends(get_current_user),
  70. ):
  71. if current_user.Role == "Admin":
  72. tasks = get_all_tasks(db)
  73. return tasks
  74. else:
  75. raise HTTPException(
  76. status_code=403, detail="You are not authorized to perform this action"
  77. )
  78. @router.get("/{task_id}", status_code=status.HTTP_200_OK)
  79. def getTaskById(
  80. task_id: int,
  81. db: Session = Depends(get_db),
  82. current_user: User = Depends(get_current_user),
  83. ):
  84. if current_user.Role != "Admin":
  85. raise HTTPException(
  86. status_code=403, detail="You are not authorized to perform this action"
  87. )
  88. task = get_task_by_id(task_id, db)
  89. if task == "notaskfound":
  90. raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found")
  91. return task
  92. @router.get("/driver/{driver_id}", status_code=status.HTTP_200_OK)
  93. def getTasksByDriver(
  94. driver_id: int,
  95. db: Session = Depends(get_db),
  96. current_user: User = Depends(get_current_user),
  97. ):
  98. if current_user.Role != "Admin" and current_user.Role != "Driver":
  99. raise HTTPException(
  100. status_code=403, detail="You are not authorized to perform this action"
  101. )
  102. if current_user.Role == "Driver":
  103. if current_user.Id != driver_id:
  104. raise HTTPException(
  105. status_code=403, detail="You are not authorized to perform this action"
  106. )
  107. tasks = get_tasks_by_driver(driver_id, db)
  108. if tasks == "notdriver":
  109. raise HTTPException(
  110. status_code=404, detail=f"Driver with id {driver_id} not found"
  111. )
  112. return tasks