Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

189 righe
6.1 KiB

  1. from fastapi import Depends, APIRouter, HTTPException, status
  2. from sqlalchemy.orm import Session
  3. from db.session import get_db
  4. from core.config import settings
  5. from db.repository.drivetask import (
  6. create_new_task,
  7. get_task_driver,
  8. change_task_status,
  9. get_all_tasks,
  10. get_task_by_id,
  11. get_tasks_by_driver,
  12. edit_task,
  13. get_active_route_by_driver,
  14. get_my_routes,
  15. )
  16. from schemas.drivetask import CreateTask, ShowTask
  17. from db.models.user import User
  18. from apis.v1.route_auth import get_current_user
  19. from typing import List
  20. router = APIRouter()
  21. @router.post("/", status_code=status.HTTP_201_CREATED)
  22. def create_task(
  23. task: CreateTask,
  24. db: Session = Depends(get_db),
  25. current_user: User = Depends(get_current_user),
  26. ):
  27. if current_user.Role != "Admin":
  28. raise HTTPException(
  29. status_code=403, detail="You are not authorized to perform this action"
  30. )
  31. task_res = create_new_task(task=task, db=db)
  32. if task_res == "notdriver":
  33. raise HTTPException(
  34. status_code=404, detail=f"Driver with id {task.DriverId} not found"
  35. )
  36. return task
  37. @router.patch("/", status_code=status.HTTP_200_OK)
  38. def changeStatus(
  39. task_id: int,
  40. status: str,
  41. distance: float = 0,
  42. db: Session = Depends(get_db),
  43. current_user: User = Depends(get_current_user),
  44. ):
  45. if current_user.Role == "Admin" or current_user.Role == "Driver":
  46. if status not in settings.ALLOWED_TASK_STATUS:
  47. raise HTTPException(
  48. status_code=400,
  49. detail=f"Status {status} is not allowed. Allowed status are {settings.ALLOWED_TASK_STATUS}",
  50. )
  51. if current_user.Role == "Driver":
  52. verification = get_task_driver(task_id, db)
  53. if verification.Id != current_user.Id:
  54. raise HTTPException(
  55. status_code=403,
  56. detail="You are not authorized to perform this action",
  57. )
  58. task = change_task_status(task_id, status, distance, db)
  59. if task == "notaskfound":
  60. raise HTTPException(
  61. status_code=404, detail=f"Task with id {task_id} not found"
  62. )
  63. return task
  64. else:
  65. raise HTTPException(
  66. status_code=403, detail="You are not authorized to perform this action"
  67. )
  68. @router.get("/", response_model=List[ShowTask], status_code=status.HTTP_200_OK)
  69. def getAllTasks(
  70. status: str = "Any",
  71. db: Session = Depends(get_db),
  72. current_user: User = Depends(get_current_user),
  73. ):
  74. if current_user.Role == "Admin":
  75. tasks = get_all_tasks(status, db)
  76. return tasks
  77. else:
  78. raise HTTPException(
  79. status_code=403, detail="You are not authorized to perform this action"
  80. )
  81. @router.get("/{task_id}", response_model=ShowTask, status_code=status.HTTP_200_OK)
  82. def getTaskById(
  83. task_id: int,
  84. db: Session = Depends(get_db),
  85. current_user: User = Depends(get_current_user),
  86. ):
  87. if current_user.Role != "Admin":
  88. raise HTTPException(
  89. status_code=403, detail="You are not authorized to perform this action"
  90. )
  91. task = get_task_by_id(task_id, db)
  92. if task == "notaskfound":
  93. raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found")
  94. return task
  95. @router.get("/driver/{driver_id}", status_code=status.HTTP_200_OK)
  96. def getTasksByDriver(
  97. driver_id: int,
  98. db: Session = Depends(get_db),
  99. current_user: User = Depends(get_current_user),
  100. ):
  101. if current_user.Role != "Admin" and current_user.Role != "Driver":
  102. raise HTTPException(
  103. status_code=403, detail="You are not authorized to perform this action"
  104. )
  105. if current_user.Role == "Driver":
  106. if current_user.Id != driver_id:
  107. raise HTTPException(
  108. status_code=403, detail="You are not authorized to perform this action"
  109. )
  110. tasks = get_tasks_by_driver(driver_id, db)
  111. if tasks == "notdriver":
  112. raise HTTPException(
  113. status_code=404, detail=f"Driver with id {driver_id} not found"
  114. )
  115. return tasks
  116. @router.put("/{task_id}", status_code=status.HTTP_200_OK)
  117. def updateTask(
  118. task_id: int,
  119. task: CreateTask,
  120. db: Session = Depends(get_db),
  121. current_user: User = Depends(get_current_user),
  122. ):
  123. if current_user.Role != "Admin":
  124. raise HTTPException(
  125. status_code=403, detail="You are not authorized to perform this action"
  126. )
  127. task = edit_task(task_id, task, db)
  128. if task == "notaskfound":
  129. raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found")
  130. return task
  131. @router.get("/active/{driver_id}", status_code=status.HTTP_200_OK)
  132. def getActiveRoute(
  133. driver_id: int,
  134. db: Session = Depends(get_db),
  135. current_user: User = Depends(get_current_user),
  136. ):
  137. if current_user.Role != "Admin" and current_user.Id != driver_id:
  138. raise HTTPException(
  139. status_code=403, detail="You are not authorized to perform this action"
  140. )
  141. route = get_active_route_by_driver(driver_id, db)
  142. if route == "notdriver":
  143. raise HTTPException(
  144. status_code=404, detail=f"Driver with id {driver_id} not found"
  145. )
  146. if route == "noroute":
  147. raise HTTPException(
  148. status_code=404, detail=f"Driver with id {driver_id} has no active route"
  149. )
  150. return route
  151. @router.get("/myroutes/", status_code=status.HTTP_200_OK)
  152. def getMyRoutes(
  153. db: Session = Depends(get_db),
  154. current_user: User = Depends(get_current_user),
  155. ):
  156. print("here")
  157. if current_user.Role != "Driver":
  158. raise HTTPException(
  159. status_code=403,
  160. detail="You are not a driver, you can't have routes, silly!",
  161. )
  162. routes = get_my_routes(current_user.Id, db)
  163. if routes == "notdriver":
  164. raise HTTPException(
  165. status_code=404, detail=f"Driver with id {current_user.Id} not found"
  166. )
  167. if not routes:
  168. raise HTTPException(
  169. status_code=404, detail=f"Driver with id {current_user.Id} has no routes"
  170. )
  171. return routes