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.
 
 

169 lines
5.8 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.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. if vehicles == []:
  87. raise HTTPException(status_code=404, detail="No vehicles found")
  88. return vehicles
  89. @router.get(
  90. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  91. )
  92. async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
  93. vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
  94. if not vehicle:
  95. raise HTTPException(status_code=404, detail="Vehicle not found")
  96. return vehicle
  97. @router.put(
  98. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  99. )
  100. def update_vehicle(
  101. vehicle_id: int,
  102. vehicle: UpdateVehicle,
  103. db: Session = Depends(get_db),
  104. current_user: User = Depends(get_current_user),
  105. ):
  106. if current_user.Role != "Admin":
  107. raise HTTPException(
  108. status_code=403, detail="You are not authorized to perform this action"
  109. )
  110. vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
  111. if vehicleRes == "vehicleNotFound":
  112. raise HTTPException(status_code=404, detail="Vehicle not found")
  113. elif vehicleRes == "badreq":
  114. raise HTTPException(status_code=502, detail="Bad request")
  115. elif vehicleRes == "driverNotFound":
  116. raise HTTPException(status_code=404, detail="Driver not found")
  117. return vehicleRes
  118. @router.delete("/{vehicle_id}", status_code=status.HTTP_200_OK)
  119. def delete_vehicle(
  120. vehicle_id: int,
  121. db: Session = Depends(get_db),
  122. current_user: User = Depends(get_current_user),
  123. ):
  124. if current_user.Role != "Admin":
  125. raise HTTPException(
  126. status_code=403, detail="You are not authorized to perform this action"
  127. )
  128. result = delete_vehicle_data(id=vehicle_id, db=db)
  129. if result == "vehicleNotFound":
  130. raise HTTPException(status_code=404, detail="Vehicle not found")
  131. return {"msg": "Vehicle deleted successfully"}
  132. @router.post("/{vehicle_id}/location", status_code=status.HTTP_200_OK)
  133. def update_vehicle_location(
  134. vehicle_id: int,
  135. location: VehicleLocation,
  136. current_user: User = Depends(get_current_user),
  137. db: Session = Depends(get_db),
  138. ):
  139. print(current_user)
  140. print(current_user.Name)
  141. if current_user.Role != "Driver" and current_user.Role != "Admin":
  142. raise HTTPException(
  143. status_code=403, detail="You are not authorized to perform this action"
  144. )
  145. if current_user.AssignedVehicle != vehicle_id and current_user.Role != "Admin":
  146. raise HTTPException(
  147. status_code=403, detail="You are not the correct car driver"
  148. )
  149. print("FUNNY")
  150. vehicle = update_vehicle_geoloc(vehicle_id=vehicle_id, location=location, db=db)
  151. if vehicle == "vehiclenotfound":
  152. raise HTTPException(status_code=404, detail="Vehicle not found")
  153. return vehicle