Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
1 год назад
1 год назад
1 год назад
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from sqlalchemy.orm import Session
  2. from schemas.vehicle import CreateVehicle, UpdateVehicle
  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(
  27. {"AssignedDriverIds": vehicle.AssignedDriverIds + [driver_id]}
  28. )
  29. print(vehicle.AssignedDriverIds)
  30. db.add(vehicle)
  31. db.commit()
  32. db.refresh(vehicle)
  33. return vehicle
  34. # return a 404 error if the driver does not exist
  35. return "nodriver"
  36. def list_vehicles(db: Session):
  37. vehicles = db.query(Vehicle).all()
  38. return vehicles
  39. def get_vehicle_by_id(vehicle_id: int, db: Session):
  40. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  41. return vehicle
  42. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  43. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  44. vehicle_object = vehicle_db.first()
  45. if not vehicle_object:
  46. return "vehiclenotfound"
  47. vehicle_object.AssignedDriverIds = vehicle.AssignedDriverIds
  48. vehicle_object.CurrentLocation = vehicle.CurrentLocation
  49. vehicle_object.Fuel = vehicle.Fuel
  50. vehicle_object.LicensePlate = vehicle.LicensePlate
  51. vehicle_object.MaintenanceNotes = vehicle.MaintenanceNotes
  52. vehicle_object.Mileage = vehicle.Mileage
  53. vehicle_object.Model = vehicle.Model
  54. vehicle_object.Type = vehicle.Type
  55. vehicle_object.Year = vehicle.Year
  56. print(vehicle_object)
  57. db.add(vehicle_object)
  58. db.commit()
  59. return vehicle_object
  60. def delete_vehicle_data(id: int, db: Session):
  61. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  62. vehicle_object = vehicle_db.first()
  63. if not vehicle_object:
  64. return "vehiclenotfound"
  65. db.delete(vehicle_object)
  66. db.commit()
  67. return vehicle_object