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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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
  7. from db.repository.vehicle import (
  8. create_new_vehicle,
  9. assign_vehicle_driver,
  10. list_vehicles,
  11. )
  12. router = APIRouter()
  13. @router.post("/new", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED)
  14. async def create_vehicle(vehicle: CreateVehicle, db: Session = Depends(get_db)):
  15. vehicle = create_new_vehicle(vehicle=vehicle, db=db)
  16. return vehicle
  17. # @router.get(
  18. # "/assign/{vehicle_id}/{driver_id}",
  19. # response_model=OutputVehicle,
  20. # status_code=status.HTTP_200_OK,
  21. # )
  22. # async def assign_drver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)):
  23. # vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  24. # if vehicle == "nodriver":
  25. # raise HTTPException(
  26. # status_code=404, detail=f"Driver with id {driver_id} not found"
  27. # )
  28. # if vehicle == "novehicle":
  29. # raise HTTPException(
  30. # status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  31. # )
  32. # if vehicle == "alreadyassigned":
  33. # raise HTTPException(
  34. # status_code=400,
  35. # detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  36. # )
  37. # return vehicle
  38. @router.patch(
  39. "/{vehicle_id}/driver/{driver_id}",
  40. response_model=OutputVehicle,
  41. status_code=status.HTTP_200_OK,
  42. )
  43. async def assign_driver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)):
  44. vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  45. if vehicle == "nodriver":
  46. raise HTTPException(
  47. status_code=404, detail=f"Driver with id {driver_id} not found"
  48. )
  49. if vehicle == "novehicle":
  50. raise HTTPException(
  51. status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  52. )
  53. if vehicle == "alreadyassigned":
  54. raise HTTPException(
  55. status_code=400,
  56. detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  57. )
  58. return vehicle
  59. @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
  60. async def get_all_vehicles(db: Session = Depends(get_db)):
  61. vehicles = list_vehicles(db=db)
  62. return vehicles