25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

49 satır
1.6 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
  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.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK)
  39. async def get_all_vehicles(db: Session = Depends(get_db)):
  40. vehicles = list_vehicles(db=db)
  41. return vehicles