您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

187 行
6.4 KiB

  1. from fastapi import APIRouter, status, HTTPException
  2. from sqlalchemy.orm import Session
  3. from fastapi import Depends
  4. from typing import 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.patch("/{vehicle_id}/driver", status_code=status.HTTP_200_OK)
  84. async def unassign_driver(
  85. vehicle_id: int,
  86. db: Session = Depends(get_db),
  87. current_user: User = Depends(get_current_user),
  88. ):
  89. if current_user.Role != "Admin":
  90. raise HTTPException(
  91. status_code=403, detail="You are not authorized to perform this action"
  92. )
  93. vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=None, db=db)
  94. if vehicle == "novehicle":
  95. raise HTTPException(
  96. status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  97. )
  98. return {"msg": "Driver unassigned successfully"}
  99. @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
  100. async def get_all_vehicles(db: Session = Depends(get_db)):
  101. vehicles = list_vehicles(db=db)
  102. if vehicles == []:
  103. raise HTTPException(status_code=404, detail="No vehicles found")
  104. return vehicles
  105. @router.get(
  106. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  107. )
  108. async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
  109. vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
  110. if not vehicle:
  111. raise HTTPException(status_code=404, detail="Vehicle not found")
  112. return vehicle
  113. @router.put(
  114. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  115. )
  116. def update_vehicle(
  117. vehicle_id: int,
  118. vehicle: UpdateVehicle,
  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. vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
  127. if vehicleRes == "vehicleNotFound":
  128. raise HTTPException(status_code=404, detail="Vehicle not found")
  129. elif vehicleRes == "badreq":
  130. raise HTTPException(status_code=502, detail="Bad request")
  131. elif vehicleRes == "driverNotFound":
  132. raise HTTPException(status_code=404, detail="Driver not found")
  133. return vehicleRes
  134. @router.delete("/{vehicle_id}", status_code=status.HTTP_200_OK)
  135. def delete_vehicle(
  136. vehicle_id: int,
  137. db: Session = Depends(get_db),
  138. current_user: User = Depends(get_current_user),
  139. ):
  140. if current_user.Role != "Admin":
  141. raise HTTPException(
  142. status_code=403, detail="You are not authorized to perform this action"
  143. )
  144. result = delete_vehicle_data(id=vehicle_id, db=db)
  145. if result == "vehicleNotFound":
  146. raise HTTPException(status_code=404, detail="Vehicle not found")
  147. return {"msg": "Vehicle deleted successfully"}
  148. @router.post("/{vehicle_id}/location", status_code=status.HTTP_200_OK)
  149. def update_vehicle_location(
  150. vehicle_id: int,
  151. location: VehicleLocation,
  152. current_user: User = Depends(get_current_user),
  153. db: Session = Depends(get_db),
  154. ):
  155. print(current_user)
  156. print(current_user.Name)
  157. if current_user.Role != "Driver" and current_user.Role != "Admin":
  158. raise HTTPException(
  159. status_code=403, detail="You are not authorized to perform this action"
  160. )
  161. if current_user.AssignedVehicle != vehicle_id and current_user.Role != "Admin":
  162. raise HTTPException(
  163. status_code=403, detail="You are not the correct car driver"
  164. )
  165. print("FUNNY")
  166. vehicle = update_vehicle_geoloc(vehicle_id=vehicle_id, location=location, db=db)
  167. if vehicle == "vehiclenotfound":
  168. raise HTTPException(status_code=404, detail="Vehicle not found")
  169. return vehicle