Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

vehicle.py 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from sqlalchemy.orm import Session
  2. from schemas.vehicle import CreateVehicle, UpdateVehicle
  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. MaintenanceNotes=[]
  13. )
  14. db.add(vehicle_object)
  15. db.commit()
  16. db.refresh(vehicle_object)
  17. return vehicle_object
  18. def update_vehicle_location(vehicle_id: int, location: list[str], db: Session):
  19. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  20. vehicle_object = vehicle_db.first()
  21. if not vehicle_object:
  22. return "vehiclenotfound"
  23. vehicle_object.CurrentLocation = location
  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. driver = get_car_driver(vehicle.Id, db)
  55. if driver:
  56. vehicle.AssignedDriver = driver.Id
  57. else:
  58. vehicle.AssignedDriver = None
  59. return vehicle
  60. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, 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. vehicle_object.CurrentLocation = vehicle.CurrentLocation
  66. vehicle_object.Fuel = vehicle.Fuel
  67. vehicle_object.LicensePlate = vehicle.LicensePlate
  68. vehicle_object.MaintenanceNotes = vehicle.MaintenanceNotes
  69. vehicle_object.Mileage = vehicle.Mileage
  70. vehicle_object.Model = vehicle.Model
  71. vehicle_object.Status = vehicle.Status
  72. res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  73. if res == "nodriver":
  74. return "driverNotFound"
  75. vehicle_object.Capacity = vehicle.Capacity
  76. vehicle_object.Year = vehicle.Year
  77. print(vehicle_object)
  78. db.add(vehicle_object)
  79. db.commit()
  80. vehicle_object.AssignedDriver = vehicle.AssignedDriver
  81. return vehicle_object
  82. def delete_vehicle_data(id: int, db: Session):
  83. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  84. vehicle_object = vehicle_db.first()
  85. if not vehicle_object:
  86. return "vehiclenotfound"
  87. db.delete(vehicle_object)
  88. db.commit()
  89. return vehicle_object