Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

106 linhas
3.3 KiB

  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. MaintenanceNotes=[]
  13. )
  14. db.add(vehicle_object)
  15. db.commit()
  16. db.refresh(vehicle_object)
  17. return vehicle_object
  18. def update_vehicle_geoloc(vehicle_id: int, location: VehicleLocation, 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. print("Location: " + str(location.CurrentLocation))
  24. vehicle_object.CurrentLocation = location.CurrentLocation
  25. db.add(vehicle_object)
  26. db.commit()
  27. db.refresh(vehicle_object)
  28. return vehicle_object
  29. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  30. driver = (
  31. db.query(User).filter((User.Id == driver_id) & (User.Role == "Driver")).first()
  32. )
  33. print(driver)
  34. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  35. if not vehicle:
  36. return "novehicle"
  37. if not driver:
  38. return "nodriver"
  39. driver.AssignedVehicle = vehicle_id
  40. db.add(driver)
  41. db.commit()
  42. return vehicle
  43. def list_vehicles(db: Session):
  44. vehicles = db.query(Vehicle).all()
  45. # assign AssignedDriver to all vehicles based on their drivers
  46. for vehicle in vehicles:
  47. driver = get_car_driver(vehicle.Id, db)
  48. if driver:
  49. vehicle.AssignedDriver = driver.Id
  50. else:
  51. vehicle.AssignedDriver = None
  52. return vehicles
  53. def get_vehicle_by_id(vehicle_id: int, db: Session):
  54. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  55. driver = get_car_driver(vehicle.Id, db)
  56. if driver:
  57. vehicle.AssignedDriver = driver.Id
  58. else:
  59. vehicle.AssignedDriver = None
  60. return vehicle
  61. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  62. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  63. vehicle_object = vehicle_db.first()
  64. if not vehicle_object:
  65. return "vehiclenotfound"
  66. vehicle_object.CurrentLocation = vehicle.CurrentLocation
  67. vehicle_object.Fuel = vehicle.Fuel
  68. vehicle_object.LicensePlate = vehicle.LicensePlate
  69. vehicle_object.DriverHistory = vehicle.DriverHistory
  70. vehicle_object.Type = vehicle.Type
  71. vehicle_object.Mileage = vehicle.Mileage
  72. vehicle_object.Model = vehicle.Model
  73. vehicle_object.Status = vehicle.Status
  74. res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  75. if res == "nodriver":
  76. return "driverNotFound"
  77. vehicle_object.Capacity = vehicle.Capacity
  78. vehicle_object.Year = vehicle.Year
  79. print(vehicle_object)
  80. db.add(vehicle_object)
  81. db.commit()
  82. vehicle_object.AssignedDriver = vehicle.AssignedDriver
  83. return vehicle_object
  84. def delete_vehicle_data(id: int, db: Session):
  85. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  86. vehicle_object = vehicle_db.first()
  87. if not vehicle_object:
  88. return "vehiclenotfound"
  89. db.delete(vehicle_object)
  90. db.commit()
  91. return vehicle_object