Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

fuelingtask.py 1.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. res = fuel_task.__dict__
  38. res["Driver"] = get_user_by_id(fuel_task.DriverId, db=db)
  39. return fuel_task