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.
 
 

139 lines
4.4 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. )
  14. from schemas.drivetask import CreateTask
  15. from db.models.user import User
  16. from apis.v1.route_auth import get_current_user
  17. router = APIRouter()
  18. @router.post("/", status_code=status.HTTP_201_CREATED)
  19. def create_task(
  20. task: CreateTask,
  21. db: Session = Depends(get_db),
  22. current_user: User = Depends(get_current_user),
  23. ):
  24. if current_user.Role != "Admin":
  25. raise HTTPException(
  26. status_code=403, detail="You are not authorized to perform this action"
  27. )
  28. task_res = create_new_task(task=task, db=db)
  29. if task_res == "notdriver":
  30. raise HTTPException(
  31. status_code=404, detail=f"Driver with id {task.DriverId} not found"
  32. )
  33. return task
  34. @router.patch("/", status_code=status.HTTP_200_OK)
  35. def changeStatus(
  36. task_id: int,
  37. status: str,
  38. db: Session = Depends(get_db),
  39. current_user: User = Depends(get_current_user),
  40. ):
  41. if current_user.Role == "Admin" or current_user.Role == "Driver":
  42. if status not in settings.ALLOWED_TASK_STATUS:
  43. raise HTTPException(
  44. status_code=400,
  45. detail=f"Status {status} is not allowed. Allowed status are {settings.ALLOWED_TASK_STATUS}",
  46. )
  47. if current_user.Role == "Driver":
  48. verification = get_task_driver(task_id, db)
  49. if verification.Id != current_user.Id:
  50. raise HTTPException(
  51. status_code=403,
  52. detail="You are not authorized to perform this action",
  53. )
  54. task = change_task_status(task_id, status, db)
  55. if task == "notaskfound":
  56. raise HTTPException(
  57. status_code=404, detail=f"Task with id {task_id} not found"
  58. )
  59. return task
  60. else:
  61. raise HTTPException(
  62. status_code=403, detail="You are not authorized to perform this action"
  63. )
  64. @router.get("/", status_code=status.HTTP_200_OK)
  65. def getAllTasks(
  66. db: Session = Depends(get_db),
  67. current_user: User = Depends(get_current_user),
  68. ):
  69. if current_user.Role == "Admin":
  70. tasks = get_all_tasks(db)
  71. return tasks
  72. else:
  73. raise HTTPException(
  74. status_code=403, detail="You are not authorized to perform this action"
  75. )
  76. @router.get("/{task_id}", status_code=status.HTTP_200_OK)
  77. def getTaskById(
  78. task_id: int,
  79. db: Session = Depends(get_db),
  80. current_user: User = Depends(get_current_user),
  81. ):
  82. if current_user.Role != "Admin":
  83. raise HTTPException(
  84. status_code=403, detail="You are not authorized to perform this action"
  85. )
  86. task = get_task_by_id(task_id, db)
  87. if task == "notaskfound":
  88. raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found")
  89. return task
  90. @router.get("/driver/{driver_id}", status_code=status.HTTP_200_OK)
  91. def getTasksByDriver(
  92. driver_id: int,
  93. db: Session = Depends(get_db),
  94. current_user: User = Depends(get_current_user),
  95. ):
  96. if current_user.Role != "Admin" and current_user.Role != "Driver":
  97. raise HTTPException(
  98. status_code=403, detail="You are not authorized to perform this action"
  99. )
  100. if current_user.Role == "Driver":
  101. if current_user.Id != driver_id:
  102. raise HTTPException(
  103. status_code=403, detail="You are not authorized to perform this action"
  104. )
  105. tasks = get_tasks_by_driver(driver_id, db)
  106. if tasks == "notdriver":
  107. raise HTTPException(
  108. status_code=404, detail=f"Driver with id {driver_id} not found"
  109. )
  110. return tasks
  111. @router.put("/{task_id}", status_code=status.HTTP_200_OK)
  112. def updateTask(
  113. task_id: int,
  114. task: CreateTask,
  115. db: Session = Depends(get_db),
  116. current_user: User = Depends(get_current_user),
  117. ):
  118. if current_user.Role != "Admin":
  119. raise HTTPException(
  120. status_code=403, detail="You are not authorized to perform this action"
  121. )
  122. task = edit_task(task_id, task, db)
  123. if task == "notaskfound":
  124. raise HTTPException(
  125. status_code=404, detail=f"Task with id {task_id} not found"
  126. )
  127. return task