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.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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(), Fuel=0, Status="Inactive", CurrentLocation=[]
  9. )
  10. db.add(vehicle_object)
  11. db.commit()
  12. db.refresh(vehicle_object)
  13. return vehicle_object
  14. def update_vehicle_geoloc(vehicle_id: int, location: VehicleLocation, db: Session):
  15. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  16. vehicle_object = vehicle_db.first()
  17. if not vehicle_object:
  18. return "vehiclenotfound"
  19. print("Location: " + str(location.CurrentLocation))
  20. vehicle_object.CurrentLocation = location.CurrentLocation
  21. db.add(vehicle_object)
  22. db.commit()
  23. db.refresh(vehicle_object)
  24. return vehicle_object
  25. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  26. if driver_id != None:
  27. driver = (
  28. db.query(User)
  29. .filter((User.Id == driver_id) & (User.Role == "Driver"))
  30. .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. else:
  40. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  41. if not vehicle:
  42. return "novehicle"
  43. if vehicle.Status == "Active":
  44. return "alreadyassigned"
  45. driver = get_car_driver(vehicle_id, db)
  46. if driver:
  47. driver.AssignedVehicle = None
  48. else:
  49. return "nodriver"
  50. db.add(driver)
  51. db.commit()
  52. return vehicle
  53. def list_vehicles(db: Session):
  54. vehicles = db.query(Vehicle).all()
  55. # assign AssignedDriver to all vehicles based on their drivers
  56. for vehicle in vehicles:
  57. driver = get_car_driver(vehicle.Id, db)
  58. if driver:
  59. vehicle.AssignedDriver = driver.__dict__
  60. else:
  61. vehicle.AssignedDriver = None
  62. return vehicles
  63. def get_vehicle_by_id(vehicle_id: int, db: Session):
  64. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  65. if not vehicle:
  66. return None
  67. driver = get_car_driver(vehicle.Id, db)
  68. if driver:
  69. vehicle.AssignedDriver = driver.__dict__
  70. else:
  71. vehicle.AssignedDriver = None
  72. return vehicle
  73. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  74. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  75. vehicle_object = vehicle_db.first()
  76. if not vehicle_object:
  77. return "vehiclenotfound"
  78. #vehicle_object.CurrentLocation = vehicle.CurrentLocation
  79. #vehicle_object.Fuel = vehicle.Fuel
  80. #vehicle_object.LicensePlate = vehicle.LicensePlate
  81. #vehicle_object.DriverHistory = vehicle.DriverHistory
  82. #vehicle_object.Type = vehicle.Type
  83. vehicle_object.Mileage = vehicle.Mileage
  84. #vehicle_object.Model = vehicle.Model
  85. vehicle_object.Status = vehicle.Status
  86. #res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  87. #if res == "nodriver":
  88. # return "driverNotFound"
  89. #vehicle_object.Capacity = vehicle.Capacity
  90. #vehicle_object.Year = vehicle.Year
  91. print(vehicle_object)
  92. db.add(vehicle_object)
  93. db.commit()
  94. #vehicle_object.AssignedDriver = vehicle.AssignedDriver
  95. return vehicle_object
  96. def delete_vehicle_data(id: int, db: Session):
  97. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  98. vehicle_object = vehicle_db.first()
  99. if not vehicle_object:
  100. return "vehiclenotfound"
  101. db.delete(vehicle_object)
  102. db.commit()
  103. return vehicle_object