|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- from sqlalchemy.orm import Session
- from schemas.vehicle import CreateVehicle, OutputVehicle
- from db.models.vehicle import Vehicle
- from db.repository.user import verify_driver_exists
-
-
- def create_new_vehicle(vehicle: CreateVehicle, db: Session):
- vehicle = Vehicle(
- Model=vehicle.model,
- Year=vehicle.year,
- LicensePlate=vehicle.license_plate,
- Type=vehicle.type,
- Mileage=vehicle.mileage,
- AssignedDriverIds=[],
- CurrentLocation=[],
- Fuel=0,
- MaintenanceNotes=[],
- )
- db.add(vehicle)
- db.commit()
- db.refresh(vehicle)
- return vehicle
-
-
- def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
- vehicledb = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
- vehicle = vehicledb.first()
- if not vehicle:
- return "novehicle"
- if driver_id in vehicle.AssignedDriverIds:
- return "alreadyassigned"
- if verify_driver_exists(driver_id=driver_id, db=db):
- print(vehicle.AssignedDriverIds)
- vehicledb.update({"AssignedDriverIds": vehicle.AssignedDriverIds + [driver_id]})
- print(vehicle.AssignedDriverIds)
- db.add(vehicle)
- db.commit()
- db.refresh(vehicle)
- return vehicle
- # return a 404 error if the driver does not exist
- return "nodriver"
-
-
- def list_vehicles(db: Session):
- vehicles = db.query(Vehicle).all()
- return vehicles
|