from fastapi import APIRouter, status, HTTPException from sqlalchemy.orm import Session from fastapi import Depends from typing import List from db.session import get_db from schemas.vehicle import OutputVehicle, CreateVehicle from db.repository.vehicle import ( create_new_vehicle, assign_vehicle_driver, list_vehicles, ) router = APIRouter() @router.post("/new", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED) async def create_vehicle(vehicle: CreateVehicle, db: Session = Depends(get_db)): vehicle = create_new_vehicle(vehicle=vehicle, db=db) return vehicle # @router.get( # "/assign/{vehicle_id}/{driver_id}", # response_model=OutputVehicle, # status_code=status.HTTP_200_OK, # ) # async def assign_drver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)): # vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db) # if vehicle == "nodriver": # raise HTTPException( # status_code=404, detail=f"Driver with id {driver_id} not found" # ) # if vehicle == "novehicle": # raise HTTPException( # status_code=404, detail=f"Vehicle with id {vehicle_id} not found" # ) # if vehicle == "alreadyassigned": # raise HTTPException( # status_code=400, # detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}", # ) # return vehicle @router.patch( "/{vehicle_id}/driver/{driver_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK, ) async def assign_driver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)): vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=driver_id, db=db) if vehicle == "nodriver": raise HTTPException( status_code=404, detail=f"Driver with id {driver_id} not found" ) if vehicle == "novehicle": raise HTTPException( status_code=404, detail=f"Vehicle with id {vehicle_id} not found" ) if vehicle == "alreadyassigned": raise HTTPException( status_code=400, detail=f"Driver with id {driver_id} is already assigned to vehicle with id {vehicle_id}", ) return vehicle @router.get("/", response_model=List[OutputVehicle], status_code=status.HTTP_200_OK) async def get_all_vehicles(db: Session = Depends(get_db)): vehicles = list_vehicles(db=db) return vehicles