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() if not fuel_task: return "notfound" res = fuel_task.__dict__ driver = get_user_by_id(fuel_task.DriverId, role="Driver", db=db) driver_obj = driver.__dict__ res["Driver"] = driver_obj res["Driver"]["AssignedVehicle"] = driver.vehicle return fuel_task