Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

route_vehicle.py 4.7 KiB

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