|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- from sqlalchemy.orm import Session
-
-
- from schemas.fuelingtask import CreateFuelingTask
- from db.models.fuelingtask import FuelingTask
- from db.repository.user import get_car_driver, get_user_by_id
- from db.repository.vehicle import get_vehicle_by_id
-
- def create_fueling_task(fueling_task: CreateFuelingTask, current_user: int, db: Session):
-
- if not get_vehicle_by_id(fueling_task.VehicleId, db=db):
- return "novehicle"
-
- driver = get_car_driver(fueling_task.VehicleId, db=db)
-
- if not driver:
- return "nodriver"
-
- fueling_task_object = FuelingTask(
- DriverId=driver.Id,
- VehicleId=fueling_task.VehicleId,
- CreatedById=current_user,
- Description=fueling_task.Description,
- Date=fueling_task.Date,
- Cost=fueling_task.Cost,
- FuelRefilled=fueling_task.FuelRefilled,
- GasStationName=fueling_task.GasStationName,
- ImageBefore=fueling_task.ImageBefore,
- ImageAfter=fueling_task.ImageAfter,
- )
- db.add(fueling_task_object)
- db.commit()
- db.refresh(fueling_task_object)
- return fueling_task_object
-
-
- def delete_fueling_task(fueling_task_id: int, db: Session):
- fueling_task = db.query(FuelingTask).filter(FuelingTask.Id == fueling_task_id).first()
- if fueling_task:
- db.delete(fueling_task)
- db.commit()
- return True
- return False
-
- def get_fueling_task_by_id(fuel_task_id: int, db: Session):
- fuel_task = db.query(FuelingTask).filter(FuelingTask.Id == fuel_task_id).first()
- res = fuel_task.__dict__
- res["Driver"] = get_user_by_id(fuel_task.DriverId, db=db)
- return fuel_task
|