You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

47 lines
1.4 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.vehicle import CreateVehicle, OutputVehicle
  3. from db.models.vehicle import Vehicle
  4. from db.repository.user import verify_driver_exists
  5. def create_new_vehicle(vehicle: CreateVehicle, db: Session):
  6. vehicle = Vehicle(
  7. Model=vehicle.model,
  8. Year=vehicle.year,
  9. LicensePlate=vehicle.license_plate,
  10. Type=vehicle.type,
  11. Mileage=vehicle.mileage,
  12. AssignedDriverIds=[],
  13. CurrentLocation=[],
  14. Fuel=0,
  15. MaintenanceNotes=[],
  16. )
  17. db.add(vehicle)
  18. db.commit()
  19. db.refresh(vehicle)
  20. return vehicle
  21. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  22. vehicledb = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  23. vehicle = vehicledb.first()
  24. if not vehicle:
  25. return "novehicle"
  26. if driver_id in vehicle.AssignedDriverIds:
  27. return "alreadyassigned"
  28. if verify_driver_exists(driver_id=driver_id, db=db):
  29. print(vehicle.AssignedDriverIds)
  30. vehicledb.update({"AssignedDriverIds": vehicle.AssignedDriverIds + [driver_id]})
  31. print(vehicle.AssignedDriverIds)
  32. db.add(vehicle)
  33. db.commit()
  34. db.refresh(vehicle)
  35. return vehicle
  36. # return a 404 error if the driver does not exist
  37. return "nodriver"
  38. def list_vehicles(db: Session):
  39. vehicles = db.query(Vehicle).all()
  40. return vehicles