No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

54 líneas
1.8 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.fuelingtask import CreateFuelingTask
  3. from db.models.fuelingtask import FuelingTask
  4. from db.repository.user import get_car_driver, get_user_by_id
  5. from db.repository.vehicle import get_vehicle_by_id
  6. def create_fueling_task(fueling_task: CreateFuelingTask, current_user: int, db: Session):
  7. if not get_vehicle_by_id(fueling_task.VehicleId, db=db):
  8. return "novehicle"
  9. driver = get_car_driver(fueling_task.VehicleId, db=db)
  10. if not driver:
  11. return "nodriver"
  12. fueling_task_object = FuelingTask(
  13. DriverId=driver.Id,
  14. VehicleId=fueling_task.VehicleId,
  15. CreatedById=current_user,
  16. Description=fueling_task.Description,
  17. Date=fueling_task.Date,
  18. Cost=fueling_task.Cost,
  19. FuelRefilled=fueling_task.FuelRefilled,
  20. GasStationName=fueling_task.GasStationName,
  21. ImageBefore=fueling_task.ImageBefore,
  22. ImageAfter=fueling_task.ImageAfter,
  23. )
  24. db.add(fueling_task_object)
  25. db.commit()
  26. db.refresh(fueling_task_object)
  27. return fueling_task_object
  28. def delete_fueling_task(fueling_task_id: int, db: Session):
  29. fueling_task = db.query(FuelingTask).filter(FuelingTask.Id == fueling_task_id).first()
  30. if fueling_task:
  31. db.delete(fueling_task)
  32. db.commit()
  33. return True
  34. return False
  35. def get_fueling_task_by_id(fuel_task_id: int, db: Session):
  36. fuel_task = db.query(FuelingTask).filter(FuelingTask.Id == fuel_task_id).first()
  37. if not fuel_task:
  38. return "notfound"
  39. res = fuel_task.__dict__
  40. driver = get_user_by_id(fuel_task.DriverId, role="Driver", db=db)
  41. driver_obj = driver.__dict__
  42. res["Driver"] = driver_obj
  43. res["Driver"]["AssignedVehicle"] = driver.vehicle
  44. return fuel_task