|
|
@@ -10,13 +10,18 @@ from db.repository.vehicle import ( |
|
|
|
list_vehicles, |
|
|
|
get_vehicle_by_id, |
|
|
|
replace_vehicle_data, |
|
|
|
delete_vehicle_data, |
|
|
|
) |
|
|
|
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)): |
|
|
|
async def create_vehicle(vehicle: CreateVehicle, 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 = create_new_vehicle(vehicle=vehicle, db=db) |
|
|
|
return vehicle |
|
|
|
|
|
|
@@ -47,7 +52,9 @@ async def create_vehicle(vehicle: CreateVehicle, db: Session = Depends(get_db)): |
|
|
|
response_model=OutputVehicle, |
|
|
|
status_code=status.HTTP_200_OK, |
|
|
|
) |
|
|
|
async def assign_driver(vehicle_id: int, driver_id: int, db: Session = Depends(get_db)): |
|
|
|
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( |
|
|
@@ -85,11 +92,23 @@ async def get_vehicle(vehicle_id: int, db: Session = Depends(get_db)): |
|
|
|
"/{vehicle_id}", response_model=OutputVehicle, status_code=status.HTTP_200_OK |
|
|
|
) |
|
|
|
def update_vehicle( |
|
|
|
vehicle_id: int, vehicle: UpdateVehicle, db: Session = Depends(get_db) |
|
|
|
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") |
|
|
|
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"} |