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.
 
 

187 Zeilen
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. if current_user.Role != "Admin":
  26. raise HTTPException(
  27. status_code=403, detail="You are not authorized to perform this action"
  28. )
  29. vehicle = create_new_vehicle(vehicle=vehicle, db=db)
  30. return vehicle
  31. # @router.get(
  32. # "/assign/{vehicle_id}/{driver_id}",
  33. # response_model=OutputVehicle,
  34. # status_code=status.HTTP_200_OK,
  35. # )
  36. # async def assign_drver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)):
  37. # vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  38. # if vehicle == "nodriver":
  39. # raise HTTPException(
  40. # status_code=404, detail=f"Driver with id {driver_id} not found"
  41. # )
  42. # if vehicle == "novehicle":
  43. # raise HTTPException(
  44. # status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  45. # )
  46. # if vehicle == "alreadyassigned":
  47. # raise HTTPException(
  48. # status_code=400,
  49. # detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  50. # )
  51. # return vehicle
  52. @router.patch(
  53. "/{vehicle_id}/driver/{driver_id}",
  54. response_model=OutputVehicle,
  55. status_code=status.HTTP_200_OK,
  56. )
  57. async def assign_driver(
  58. vehicle_id: int,
  59. driver_id: int,
  60. db: Session = Depends(get_db),
  61. current_user: User = Depends(get_current_user),
  62. ):
  63. if current_user.Role != "Admin":
  64. raise HTTPException(
  65. status_code=403, detail="You are not authorized to perform this action"
  66. )
  67. vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  68. if vehicle == "nodriver":
  69. raise HTTPException(
  70. status_code=404, detail=f"Driver with id {driver_id} not found"
  71. )
  72. if vehicle == "novehicle":
  73. raise HTTPException(
  74. status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  75. )
  76. if vehicle == "alreadyassigned":
  77. raise HTTPException(
  78. status_code=400,
  79. detail=f"A driver is already assigned to vehicle with id {vehicle_id}",
  80. )
  81. return vehicle
  82. @router.patch("/{vehicle_id}/driver", status_code=status.HTTP_200_OK)
  83. async def unassign_driver(
  84. vehicle_id: int,
  85. db: Session = Depends(get_db),
  86. current_user: User = Depends(get_current_user),
  87. ):
  88. if current_user.Role != "Admin":
  89. raise HTTPException(
  90. status_code=403, detail="You are not authorized to perform this action"
  91. )
  92. vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=None, db=db)
  93. if vehicle == "novehicle":
  94. raise HTTPException(
  95. status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  96. )
  97. return {"msg": "Driver unassigned successfully"}
  98. @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
  99. async def get_all_vehicles(db: Session = Depends(get_db)):
  100. vehicles = list_vehicles(db=db)
  101. if vehicles == []:
  102. raise HTTPException(status_code=404, detail="No vehicles found")
  103. return vehicles
  104. @router.get(
  105. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  106. )
  107. async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
  108. vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
  109. if not vehicle:
  110. raise HTTPException(status_code=404, detail="Vehicle not found")
  111. return vehicle
  112. @router.put(
  113. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  114. )
  115. def update_vehicle(
  116. vehicle_id: int,
  117. vehicle: UpdateVehicle,
  118. db: Session = Depends(get_db),
  119. current_user: User = Depends(get_current_user),
  120. ):
  121. if (
  122. current_user.Role != "Admin"
  123. and current_user.Role != "Maintenance"
  124. and current_user.Role != "Fueling"
  125. ):
  126. raise HTTPException(
  127. status_code=403, detail="You are not authorized to perform this action"
  128. )
  129. vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
  130. if vehicleRes == "vehicleNotFound":
  131. raise HTTPException(status_code=404, detail="Vehicle not found")
  132. elif vehicleRes == "badreq":
  133. raise HTTPException(status_code=502, detail="Bad request")
  134. elif vehicleRes == "driverNotFound":
  135. raise HTTPException(status_code=404, detail="Driver not found")
  136. return vehicleRes
  137. @router.delete("/{vehicle_id}", status_code=status.HTTP_200_OK)
  138. def delete_vehicle(
  139. vehicle_id: int,
  140. db: Session = Depends(get_db),
  141. current_user: User = Depends(get_current_user),
  142. ):
  143. if current_user.Role != "Admin":
  144. raise HTTPException(
  145. status_code=403, detail="You are not authorized to perform this action"
  146. )
  147. result = delete_vehicle_data(id=vehicle_id, db=db)
  148. if result == "vehicleNotFound":
  149. raise HTTPException(status_code=404, detail="Vehicle not found")
  150. return {"msg": "Vehicle deleted successfully"}
  151. @router.post("/{vehicle_id}/location", status_code=status.HTTP_200_OK)
  152. def update_vehicle_location(
  153. vehicle_id: int,
  154. location: VehicleLocation,
  155. current_user: User = Depends(get_current_user),
  156. db: Session = Depends(get_db),
  157. ):
  158. if current_user.Role != "Driver" and current_user.Role != "Admin":
  159. raise HTTPException(
  160. status_code=403, detail="You are not authorized to perform this action"
  161. )
  162. if current_user.AssignedVehicle != vehicle_id and current_user.Role != "Admin":
  163. raise HTTPException(
  164. status_code=403, detail="You are not the correct car driver"
  165. )
  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