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.

route_vehicle.py 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  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. )
  15. from db.models.user import User
  16. from apis.v1.route_auth import get_current_user
  17. router = APIRouter()
  18. @router.post("/", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED)
  19. async def create_vehicle(vehicle: CreateVehicle, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
  20. if current_user.Role != "Admin":
  21. raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  22. vehicle = create_new_vehicle(vehicle=vehicle, db=db)
  23. return vehicle
  24. # @router.get(
  25. # "/assign/{vehicle_id}/{driver_id}",
  26. # response_model=OutputVehicle,
  27. # status_code=status.HTTP_200_OK,
  28. # )
  29. # async def assign_drver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)):
  30. # vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  31. # if vehicle == "nodriver":
  32. # raise HTTPException(
  33. # status_code=404, detail=f"Driver with id {driver_id} not found"
  34. # )
  35. # if vehicle == "novehicle":
  36. # raise HTTPException(
  37. # status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  38. # )
  39. # if vehicle == "alreadyassigned":
  40. # raise HTTPException(
  41. # status_code=400,
  42. # detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  43. # )
  44. # return vehicle
  45. @router.patch(
  46. "/{vehicle_id}/driver/{driver_id}",
  47. response_model=OutputVehicle,
  48. status_code=status.HTTP_200_OK,
  49. )
  50. async def assign_driver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
  51. if current_user.Role != "Admin":
  52. raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  53. vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  54. if vehicle == "nodriver":
  55. raise HTTPException(
  56. status_code=404, detail=f"Driver with id {driver_id} not found"
  57. )
  58. if vehicle == "novehicle":
  59. raise HTTPException(
  60. status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  61. )
  62. if vehicle == "alreadyassigned":
  63. raise HTTPException(
  64. status_code=400,
  65. detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  66. )
  67. return vehicle
  68. @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
  69. async def get_all_vehicles(db: Session = Depends(get_db)):
  70. vehicles = list_vehicles(db=db)
  71. return vehicles
  72. @router.get(
  73. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  74. )
  75. async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
  76. vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
  77. if not vehicle:
  78. raise HTTPException(status_code=404, detail="Vehicle not found")
  79. return vehicle
  80. @router.put(
  81. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  82. )
  83. def update_vehicle(
  84. vehicle_id: int, vehicle: UpdateVehicle, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
  85. ):
  86. if current_user.Role != "Admin":
  87. raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  88. vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
  89. if vehicleRes == "vehicleNotFound":
  90. raise HTTPException(status_code=404, detail="Vehicle not found")
  91. elif vehicleRes == "badreq":
  92. raise HTTPException(status_code=502, detail="Bad request")
  93. return vehicleRes
  94. @router.delete("/{vehicle_id}", status_code=status.HTTP_200_OK)
  95. def delete_vehicle(vehicle_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
  96. if current_user.Role != "Admin":
  97. raise HTTPException(status_code=403, detail="You are not authorized to perform this action")
  98. result = delete_vehicle_data(id=vehicle_id, db=db)
  99. if result == "vehicleNotFound":
  100. raise HTTPException(status_code=404, detail="Vehicle not found")
  101. return {"msg": "Vehicle deleted successfully"}