|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 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, UpdateVehicle
- from db.repository.vehicle import (
- create_new_vehicle,
- assign_vehicle_driver,
- list_vehicles,
- get_vehicle_by_id,
- replace_vehicle_data,
- )
-
- router = APIRouter()
-
-
- @router.post("/", 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
-
-
- @router.get(
- "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
- )
- async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)):
- vehicle = get_vehicle_by_id(vehicle_id=vehicle_id, db=db)
- if not vehicle:
- raise HTTPException(status_code=404, detail="Vehicle not found")
- return vehicle
-
-
- @router.put(
- "/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK
- )
- def update_vehicle(
- vehicle_id: int, vehicle: UpdateVehicle, db: Session = Depends(get_db)
- ):
- vehicleRes = replace_vehicle_data(id=vehicle_id, vehicle=vehicle, db=db)
- if vehicleRes == "vehicleNotFound":
- raise HTTPException(status_code=404, detail="Vehicle not found")
- elif vehicleRes == "badreq":
- raise HTTPException(status_code=502, detail="Bad request")
- return vehicleRes
|