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.
 
 

68 linhas
2.2 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.vehicle import CreateVehicle, OutputVehicle, UpdateVehicle
  3. from db.models.vehicle import Vehicle
  4. from db.repository.user import verify_driver_exists
  5. def create_new_vehicle(vehicle: CreateVehicle, db: Session):
  6. vehicle_object = Vehicle(
  7. **vehicle.model_dump(),
  8. Fuel=0,
  9. AssignedDriverIds=[],
  10. CurrentLocation=[],
  11. MaintenanceNotes=[]
  12. )
  13. db.add(vehicle_object)
  14. db.commit()
  15. db.refresh(vehicle_object)
  16. return vehicle
  17. def assign_vehicle_driver(vehicle_id: int, driver_id: int, db: Session):
  18. vehicledb = db.query(Vehicle).filter(Vehicle.Id == vehicle_id)
  19. vehicle = vehicledb.first()
  20. if not vehicle:
  21. return "novehicle"
  22. if driver_id in vehicle.AssignedDriverIds:
  23. return "alreadyassigned"
  24. if verify_driver_exists(driver_id=driver_id, db=db):
  25. print(vehicle.AssignedDriverIds)
  26. vehicledb.update({"AssignedDriverIds": vehicle.AssignedDriverIds + [driver_id]})
  27. print(vehicle.AssignedDriverIds)
  28. db.add(vehicle)
  29. db.commit()
  30. db.refresh(vehicle)
  31. return vehicle
  32. # return a 404 error if the driver does not exist
  33. return "nodriver"
  34. def list_vehicles(db: Session):
  35. vehicles = db.query(Vehicle).all()
  36. return vehicles
  37. def get_vehicle_by_id(vehicle_id: int, db: Session):
  38. vehicle = db.query(Vehicle).filter(Vehicle.Id == vehicle_id).first()
  39. return vehicle
  40. def replace_vehicle_data(id: int, vehicle: UpdateVehicle, db: Session):
  41. vehicle_db = db.query(Vehicle).filter(Vehicle.Id == id)
  42. vehicle_object = vehicle_db.first()
  43. if not vehicle_object:
  44. return "vehiclenotfound"
  45. vehicle_object.AssignedDriverIds = vehicle.AssignedDriverIds
  46. vehicle_object.CurrentLocation = vehicle.CurrentLocation
  47. vehicle_object.Fuel = vehicle.Fuel
  48. vehicle_object.LicensePlate = vehicle.LicensePlate
  49. vehicle_object.MaintenanceNotes = vehicle.MaintenanceNotes
  50. vehicle_object.Mileage = vehicle.Mileage
  51. vehicle_object.Model = vehicle.Model
  52. vehicle_object.Type = vehicle.Type
  53. vehicle_object.Year = vehicle.Year
  54. print(vehicle_object)
  55. db.add(vehicle_object)
  56. db.commit()
  57. return vehicle_object