Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 

43 rader
1.3 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_object = Vehicle(
  7. **vehicle.model_dump(),
  8. Fuel=0,
  9. AssignedDriverIds=[],
  10. CurrentLocation=[],
  11. MaintenanceNotes=[]
  12. )
  13. db.add(vehicle_object)
  14. db.commit()
  15. db.refresh(vehicle_object)
  16. return vehicle
  17. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  18. vehicledb = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  19. vehicle = vehicledb.first()
  20. if not vehicle:
  21. return "novehicle"
  22. if driver_id in vehicle.AssignedDriverIds:
  23. return "alreadyassigned"
  24. if verify_driver_exists(driver_id=driver_id, db=db):
  25. print(vehicle.AssignedDriverIds)
  26. vehicledb.update({"AssignedDriverIds": vehicle.AssignedDriverIds + [driver_id]})
  27. print(vehicle.AssignedDriverIds)
  28. db.add(vehicle)
  29. db.commit()
  30. db.refresh(vehicle)
  31. return vehicle
  32. # return a 404 error if the driver does not exist
  33. return "nodriver"
  34. def list_vehicles(db: Session):
  35. vehicles = db.query(Vehicle).all()
  36. return vehicles