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.
 
 

96 Zeilen
3.3 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
  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. )
  14. router = APIRouter()
  15. @router.post("/", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED)
  16. async def create_vehicle(vehicle: CreateVehicle, db: Session = Depends(get_db)):
  17. vehicle = create_new_vehicle(vehicle=vehicle, db=db)
  18. return vehicle
  19. # @router.get(
  20. # "/assign/{vehicle_id}/{driver_id}",
  21. # response_model=OutputVehicle,
  22. # status_code=status.HTTP_200_OK,
  23. # )
  24. # async def assign_drver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)):
  25. # vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  26. # if vehicle == "nodriver":
  27. # raise HTTPException(
  28. # status_code=404, detail=f"Driver with id {driver_id} not found"
  29. # )
  30. # if vehicle == "novehicle":
  31. # raise HTTPException(
  32. # status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  33. # )
  34. # if vehicle == "alreadyassigned":
  35. # raise HTTPException(
  36. # status_code=400,
  37. # detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  38. # )
  39. # return vehicle
  40. @router.patch(
  41. "/{vehicle_id}/driver/{driver_id}",
  42. response_model=OutputVehicle,
  43. status_code=status.HTTP_200_OK,
  44. )
  45. async def assign_driver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)):
  46. vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db)
  47. if vehicle == "nodriver":
  48. raise HTTPException(
  49. status_code=404, detail=f"Driver with id {driver_id} not found"
  50. )
  51. if vehicle == "novehicle":
  52. raise HTTPException(
  53. status_code=404, detail=f"Vehicle with id {vehicle_id} not found"
  54. )
  55. if vehicle == "alreadyassigned":
  56. raise HTTPException(
  57. status_code=400,
  58. detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}",
  59. )
  60. return vehicle
  61. @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
  62. async def get_all_vehicles(db: Session = Depends(get_db)):
  63. vehicles = list_vehicles(db=db)
  64. return vehicles
  65. @router.get(
  66. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  67. )
  68. async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
  69. vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
  70. if not vehicle:
  71. raise HTTPException(status_code=404, detail="Vehicle not found")
  72. return vehicle
  73. @router.put(
  74. "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
  75. )
  76. def update_vehicle(
  77. vehicle_id: int, vehicle: UpdateVehicle, db: Session = Depends(get_db)
  78. ):
  79. vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
  80. if vehicleRes == "vehicleNotFound":
  81. raise HTTPException(status_code=404, detail="Vehicle not found")
  82. elif vehicleRes == "badreq":
  83. raise HTTPException(status_code=502, detail="Bad request")
  84. return vehicleRes