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, VehicleLocation from db.repository.vehicle import ( create_new_vehicle, assign_vehicle_driver, list_vehicles, get_vehicle_by_id, replace_vehicle_data, delete_vehicle_data, update_vehicle_geoloc, ) from db.models.user import User from apis.v1.route_auth import get_current_user router = APIRouter() @router.post("/", response_model=OutputVehicle, status_code=status.HTTP_201_CREATED) async def create_vehicle( vehicle: CreateVehicle, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): print(current_user.Role) if current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) 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), current_user: User = Depends(get_current_user), ): if current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) 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"A driver is already assigned to vehicle with id {vehicle_id}", ) return vehicle @router.patch("/{vehicle_id}/driver", status_code=status.HTTP_200_OK) async def unassign_driver( vehicle_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): if current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) vehicle = assign_vehicle_driver(vehicle_id=vehicle_id, driver_id=None, db=db) if vehicle == "novehicle": raise HTTPException( status_code=404, detail=f"Vehicle with id {vehicle_id} not found" ) return {"msg": "Driver unassigned successfully"} @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) if vehicles == []: raise HTTPException(status_code=404, detail="No vehicles found") 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), current_user: User = Depends(get_current_user), ): if current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) 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") elif vehicleRes == "driverNotFound": raise HTTPException(status_code=404, detail="Driver not found") return vehicleRes @router.delete("/{vehicle_id}", status_code=status.HTTP_200_OK) def delete_vehicle( vehicle_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user), ): if current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) result = delete_vehicle_data(id=vehicle_id, db=db) if result == "vehicleNotFound": raise HTTPException(status_code=404, detail="Vehicle not found") return {"msg": "Vehicle deleted successfully"} @router.post("/{vehicle_id}/location", status_code=status.HTTP_200_OK) def update_vehicle_location( vehicle_id: int, location: VehicleLocation, current_user: User = Depends(get_current_user), db: Session = Depends(get_db), ): print(current_user) print(current_user.Name) if current_user.Role != "Driver" and current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not authorized to perform this action" ) if current_user.AssignedVehicle != vehicle_id and current_user.Role != "Admin": raise HTTPException( status_code=403, detail="You are not the correct car driver" ) print("FUNNY") vehicle = update_vehicle_geoloc(vehicle_id=vehicle_id, location=location, db=db) if vehicle == "vehiclenotfound": raise HTTPException(status_code=404, detail="Vehicle not found") return vehicle