選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

route_vehicle.py 5.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from fastapi import APIRouter, status, HTTPException
  2. from sqlalchemy.orm import Session
  3. from fastapi import Depends
  4. from typing import Annotated, List
  5. from db.session import get_db
  6. from schemas.vehicle import OutputVehicle, CreateVehicle, UpdateVehicle, VehicleLocation
  7. from db.repository.vehicle import (
  8. create_new_vehicle,
  9. assign_vehicle_driver,
  10. list_vehicles,
  11. get_vehicle_by_id,
  12. replace_vehicle_data,
  13. delete_vehicle_data,
  14. update_vehicle_geoloc,
  15. )
  16. from db.models.user import User
  17. from apis.v1.route_auth import get_current_user
  18. router = APIRouter()
  19. @router.post("/", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED)
  20. async def create_vehicle(
  21. vehicle: CreateVehicle,
  22. db: Session = Depends(get_db),
  23. current_user: User = Depends(get_current_user),
  24. ):
  25. print(current_user.Role)
  26. if current_user.Role != "Admin":
  27. raise HTTPException(
  28. status_code=403, detail="You are not authorized to perform this action"
  29. )
  30. vehicle = create_new_vehicle(vehicle=vehicle, db=db)
  31. return vehicle
  32. # @router.get(
  33. # "/assign/{vehicle_id}/{driver_id}",
  34. # response_model=OutputVehicle,
  35. # status_code=status.HTTP_200_OK,
  36. # )
  37. # async def assign_drver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)):
  38. # vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  39. # if vehicle == "nodriver":
  40. # raise HTTPException(
  41. # status_code=404, detail=f"Driver with id {driver_id} not found"
  42. # )
  43. # if vehicle == "novehicle":
  44. # raise HTTPException(
  45. # status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  46. # )
  47. # if vehicle == "alreadyassigned":
  48. # raise HTTPException(
  49. # status_code=400,
  50. # detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  51. # )
  52. # return vehicle
  53. @router.patch(
  54. "/{vehicle_id}/driver/{driver_id}",
  55. response_model=OutputVehicle,
  56. status_code=status.HTTP_200_OK,
  57. )
  58. async def assign_driver(
  59. vehicle_id: int,
  60. driver_id: int,
  61. db: Session = Depends(get_db),
  62. current_user: User = Depends(get_current_user),
  63. ):
  64. if current_user.Role != "Admin":
  65. raise HTTPException(
  66. status_code=403, detail="You are not authorized to perform this action"
  67. )
  68. vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  69. if vehicle == "nodriver":
  70. raise HTTPException(
  71. status_code=404, detail=f"Driver with id {driver_id} not found"
  72. )
  73. if vehicle == "novehicle":
  74. raise HTTPException(
  75. status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  76. )
  77. if vehicle == "alreadyassigned":
  78. raise HTTPException(
  79. status_code=400,
  80. detail=f"A driver is already assigned to vehicle with id {vehicle_id}",
  81. )
  82. return vehicle
  83. @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
  84. async def get_all_vehicles(db: Session = Depends(get_db)):
  85. vehicles = list_vehicles(db=db)
  86. return vehicles
  87. @router.get(
  88. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  89. )
  90. async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
  91. vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
  92. if not vehicle:
  93. raise HTTPException(status_code=404, detail="Vehicle not found")
  94. return vehicle
  95. @router.put(
  96. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  97. )
  98. def update_vehicle(
  99. vehicle_id: int,
  100. vehicle: UpdateVehicle,
  101. db: Session = Depends(get_db),
  102. current_user: User = Depends(get_current_user),
  103. ):
  104. if current_user.Role != "Admin":
  105. raise HTTPException(
  106. status_code=403, detail="You are not authorized to perform this action"
  107. )
  108. vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
  109. if vehicleRes == "vehicleNotFound":
  110. raise HTTPException(status_code=404, detail="Vehicle not found")
  111. elif vehicleRes == "badreq":
  112. raise HTTPException(status_code=502, detail="Bad request")
  113. elif vehicleRes == "driverNotFound":
  114. raise HTTPException(status_code=404, detail="Driver not found")
  115. return vehicleRes
  116. @router.delete("/{vehicle_id}", status_code=status.HTTP_200_OK)
  117. def delete_vehicle(
  118. vehicle_id: int,
  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. result = delete_vehicle_data(id=vehicle_id, db=db)
  127. if result == "vehicleNotFound":
  128. raise HTTPException(status_code=404, detail="Vehicle not found")
  129. return {"msg": "Vehicle deleted successfully"}
  130. @router.post("/{vehicle_id}/location", status_code=status.HTTP_200_OK)
  131. def update_vehicle_location(
  132. vehicle_id: int,
  133. location: VehicleLocation,
  134. current_user: User = Depends(get_current_user),
  135. db: Session = Depends(get_db),
  136. ):
  137. print(current_user)
  138. print(current_user.Name)
  139. if current_user.Role != "Driver":
  140. raise HTTPException(
  141. status_code=403, detail="You are not authorized to perform this action"
  142. )
  143. if current_user.AssignedVehicle != vehicle_id:
  144. raise HTTPException(
  145. status_code=403, detail="You are not the correct car driver"
  146. )
  147. print("FUNNY")
  148. vehicle = update_vehicle_geoloc(vehicle_id=vehicle_id, location=location, db=db)
  149. if vehicle == "vehiclenotfound":
  150. raise HTTPException(status_code=404, detail="Vehicle not found")
  151. return vehicle