選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

92 行
2.8 KiB

  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 assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  19. driver = (
  20. db.query(User).filter((User.Id == driver_id) & (User.Role == "Driver")).first()
  21. )
  22. print(driver)
  23. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  24. if not vehicle:
  25. return "novehicle"
  26. if not driver:
  27. return "nodriver"
  28. driver.AssignedVehicle = vehicle_id
  29. db.add(driver)
  30. db.commit()
  31. return vehicle
  32. def list_vehicles(db: Session):
  33. vehicles = db.query(Vehicle).all()
  34. # assign AssignedDriver to all vehicles based on their drivers
  35. for vehicle in vehicles:
  36. driver = get_car_driver(vehicle.Id, db)
  37. if driver:
  38. vehicle.AssignedDriver = driver.Id
  39. else:
  40. vehicle.AssignedDriver = None
  41. return vehicles
  42. def get_vehicle_by_id(vehicle_id: int, db: Session):
  43. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  44. driver = get_car_driver(vehicle.Id, db)
  45. if driver:
  46. vehicle.AssignedDriver = driver.Id
  47. else:
  48. vehicle.AssignedDriver = None
  49. return vehicle
  50. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  51. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  52. vehicle_object = vehicle_db.first()
  53. if not vehicle_object:
  54. return "vehiclenotfound"
  55. vehicle_object.CurrentLocation = vehicle.CurrentLocation
  56. vehicle_object.Fuel = vehicle.Fuel
  57. vehicle_object.LicensePlate = vehicle.LicensePlate
  58. vehicle_object.MaintenanceNotes = vehicle.MaintenanceNotes
  59. vehicle_object.Mileage = vehicle.Mileage
  60. vehicle_object.Model = vehicle.Model
  61. vehicle_object.Status = vehicle.Status
  62. res = assign_vehicle_driver(id, vehicle.AssignedDriver, db)
  63. if res == "nodriver":
  64. return "driverNotFound"
  65. vehicle_object.Capacity = vehicle.Capacity
  66. vehicle_object.Year = vehicle.Year
  67. print(vehicle_object)
  68. db.add(vehicle_object)
  69. db.commit()
  70. vehicle_object.AssignedDriver = vehicle.AssignedDriver
  71. return vehicle_object
  72. def delete_vehicle_data(id: int, db: Session):
  73. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  74. vehicle_object = vehicle_db.first()
  75. if not vehicle_object:
  76. return "vehiclenotfound"
  77. db.delete(vehicle_object)
  78. db.commit()
  79. return vehicle_object