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.

vehicle.py 3.3 KiB

1 jaar geleden
1 jaar geleden
1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from sqlalchemy.orm import Session
  2. from schemas.vehicle import CreateVehicle, UpdateVehicle, VehicleLocation
  3. from db.models.vehicle import Vehicle
  4. from db.base import User
  5. from db.repository.user import get_car_driver
  6. def create_new_vehicle(vehicle: CreateVehicle, db: Session):
  7. vehicle_object = Vehicle(
  8. **vehicle.model_dump(),
  9. Fuel=0,
  10. Status="Inactive",
  11. CurrentLocation=[]
  12. )
  13. db.add(vehicle_object)
  14. db.commit()
  15. db.refresh(vehicle_object)
  16. return vehicle_object
  17. def update_vehicle_geoloc(vehicle_id: int, location: VehicleLocation, db: Session):
  18. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  19. vehicle_object = vehicle_db.first()
  20. if not vehicle_object:
  21. return "vehiclenotfound"
  22. print("Location: " + str(location.CurrentLocation))
  23. vehicle_object.CurrentLocation = location.CurrentLocation
  24. db.add(vehicle_object)
  25. db.commit()
  26. db.refresh(vehicle_object)
  27. return vehicle_object
  28. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  29. driver = (
  30. db.query(User).filter((User.Id == driver_id) & (User.Role == "Driver")).first()
  31. )
  32. print(driver)
  33. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  34. if not vehicle:
  35. return "novehicle"
  36. if not driver:
  37. return "nodriver"
  38. driver.AssignedVehicle = vehicle_id
  39. db.add(driver)
  40. db.commit()
  41. return vehicle
  42. def list_vehicles(db: Session):
  43. vehicles = db.query(Vehicle).all()
  44. # assign AssignedDriver to all vehicles based on their drivers
  45. for vehicle in vehicles:
  46. driver = get_car_driver(vehicle.Id, db)
  47. if driver:
  48. vehicle.AssignedDriver = driver.Id
  49. else:
  50. vehicle.AssignedDriver = None
  51. return vehicles
  52. def get_vehicle_by_id(vehicle_id: int, db: Session):
  53. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  54. if not vehicle:
  55. return None
  56. driver = get_car_driver(vehicle.Id, db)
  57. if driver:
  58. vehicle.AssignedDriver = driver.Id
  59. else:
  60. vehicle.AssignedDriver = None
  61. return vehicle
  62. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  63. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  64. vehicle_object = vehicle_db.first()
  65. if not vehicle_object:
  66. return "vehiclenotfound"
  67. vehicle_object.CurrentLocation = vehicle.CurrentLocation
  68. vehicle_object.Fuel = vehicle.Fuel
  69. vehicle_object.LicensePlate = vehicle.LicensePlate
  70. vehicle_object.DriverHistory = vehicle.DriverHistory
  71. vehicle_object.Type = vehicle.Type
  72. vehicle_object.Mileage = vehicle.Mileage
  73. vehicle_object.Model = vehicle.Model
  74. vehicle_object.Status = vehicle.Status
  75. res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  76. if res == "nodriver":
  77. return "driverNotFound"
  78. vehicle_object.Capacity = vehicle.Capacity
  79. vehicle_object.Year = vehicle.Year
  80. print(vehicle_object)
  81. db.add(vehicle_object)
  82. db.commit()
  83. vehicle_object.AssignedDriver = vehicle.AssignedDriver
  84. return vehicle_object
  85. def delete_vehicle_data(id: int, db: Session):
  86. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  87. vehicle_object = vehicle_db.first()
  88. if not vehicle_object:
  89. return "vehiclenotfound"
  90. db.delete(vehicle_object)
  91. db.commit()
  92. return vehicle_object