25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

186 lines
6.0 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. db: Session = Depends(get_db),
  42. current_user: User = Depends(get_current_user),
  43. ):
  44. if current_user.Role == "Admin" or current_user.Role == "Driver":
  45. if status not in settings.ALLOWED_TASK_STATUS:
  46. raise HTTPException(
  47. status_code=400,
  48. detail=f"Status {status} is not allowed. Allowed status are {settings.ALLOWED_TASK_STATUS}",
  49. )
  50. if current_user.Role == "Driver":
  51. verification = get_task_driver(task_id, db)
  52. if verification.Id != current_user.Id:
  53. raise HTTPException(
  54. status_code=403,
  55. detail="You are not authorized to perform this action",
  56. )
  57. task = change_task_status(task_id, status, db)
  58. if task == "notaskfound":
  59. raise HTTPException(
  60. status_code=404, detail=f"Task with id {task_id} not found"
  61. )
  62. return task
  63. else:
  64. raise HTTPException(
  65. status_code=403, detail="You are not authorized to perform this action"
  66. )
  67. @router.get("/", response_model=List[ShowTask], status_code=status.HTTP_200_OK)
  68. def getAllTasks(
  69. status: str = "Any",
  70. db: Session = Depends(get_db),
  71. current_user: User = Depends(get_current_user),
  72. ):
  73. if current_user.Role == "Admin":
  74. tasks = get_all_tasks(status, db)
  75. return tasks
  76. else:
  77. raise HTTPException(
  78. status_code=403, detail="You are not authorized to perform this action"
  79. )
  80. @router.get("/{task_id}", response_model=ShowTask, status_code=status.HTTP_200_OK)
  81. def getTaskById(
  82. task_id: int,
  83. db: Session = Depends(get_db),
  84. current_user: User = Depends(get_current_user),
  85. ):
  86. if current_user.Role != "Admin":
  87. raise HTTPException(
  88. status_code=403, detail="You are not authorized to perform this action"
  89. )
  90. task = get_task_by_id(task_id, db)
  91. if task == "notaskfound":
  92. raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found")
  93. return task
  94. @router.get("/driver/{driver_id}", status_code=status.HTTP_200_OK)
  95. def getTasksByDriver(
  96. driver_id: int,
  97. db: Session = Depends(get_db),
  98. current_user: User = Depends(get_current_user),
  99. ):
  100. if current_user.Role != "Admin" and current_user.Role != "Driver":
  101. raise HTTPException(
  102. status_code=403, detail="You are not authorized to perform this action"
  103. )
  104. if current_user.Role == "Driver":
  105. if current_user.Id != driver_id:
  106. raise HTTPException(
  107. status_code=403, detail="You are not authorized to perform this action"
  108. )
  109. tasks = get_tasks_by_driver(driver_id, db)
  110. if tasks == "notdriver":
  111. raise HTTPException(
  112. status_code=404, detail=f"Driver with id {driver_id} not found"
  113. )
  114. return tasks
  115. @router.put("/{task_id}", status_code=status.HTTP_200_OK)
  116. def updateTask(
  117. task_id: int,
  118. task: CreateTask,
  119. db: Session = Depends(get_db),
  120. current_user: User = Depends(get_current_user),
  121. ):
  122. if current_user.Role != "Admin":
  123. raise HTTPException(
  124. status_code=403, detail="You are not authorized to perform this action"
  125. )
  126. task = edit_task(task_id, task, db)
  127. if task == "notaskfound":
  128. raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found")
  129. return task
  130. @router.get("/active/{driver_id}", status_code=status.HTTP_200_OK)
  131. def getActiveRoute(
  132. driver_id: int,
  133. db: Session = Depends(get_db),
  134. current_user: User = Depends(get_current_user),
  135. ):
  136. if current_user.Role != "Admin" and current_user.Id != driver_id:
  137. raise HTTPException(
  138. status_code=403, detail="You are not authorized to perform this action"
  139. )
  140. route = get_active_route_by_driver(driver_id, db)
  141. if route == "notdriver":
  142. raise HTTPException(
  143. status_code=404, detail=f"Driver with id {driver_id} not found"
  144. )
  145. if route == "noroute":
  146. raise HTTPException(
  147. status_code=404, detail=f"Driver with id {driver_id} has no active route"
  148. )
  149. return route
  150. @router.get("/myroutes", status_code=status.HTTP_200_OK)
  151. def getMyRoutes(
  152. db: Session = Depends(get_db),
  153. current_user: User = Depends(get_current_user),
  154. ):
  155. if current_user.Role != "Driver":
  156. raise HTTPException(
  157. status_code=403, detail="You are not authorized to perform this action"
  158. )
  159. routes = get_my_routes(current_user.Id, db)
  160. if routes == "notdriver":
  161. raise HTTPException(
  162. status_code=404, detail=f"Driver with id {current_user.Id} not found"
  163. )
  164. if not routes:
  165. raise HTTPException(
  166. status_code=404, detail=f"Driver with id {current_user.Id} has no routes"
  167. )
  168. return routes