Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

route_task.py 3.9 KiB

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