|
- import base64
- from sqlalchemy.orm import Session
-
-
- from schemas.fuelingtask import CreateFuelingTask, OutputFuelingTask
- 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.file.read(),
- ImageAfter=fueling_task.ImageAfter.file.read(),
- )
- db.add(fueling_task_object)
- db.commit()
- db.refresh(fueling_task_object)
- print(driver.__dict__)
- driverobj = driver.__dict__
- driverobj["AssignedVehicle"] = driver.vehicle.__dict__
- resobj = fueling_task_object.__dict__
- resobj["Driver"] = driverobj
- ia = fueling_task_object.ImageAfter
- ib = fueling_task_object.ImageBefore
- if ia is not None:
- ia = base64.b64encode(ia).decode("ascii")
- if ib is not None:
- ib = base64.b64encode(ib).decode("ascii")
- resobj["ImageBefore"] = ib
- resobj["ImageAfter"] = ia
- return resobj
-
-
- 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__
- imagebefore = fuel_task.ImageBefore
- imageafter = fuel_task.ImageAfter
- imagebeforeBase64 = base64.b64encode(imagebefore).decode("ascii")
- imageafterBase64 = base64.b64encode(imageafter).decode("ascii")
- res["Creator"] = fuel_task.CreatedById
- res["ImageBefore"] = imagebeforeBase64
- res["ImageAfter"] = imageafterBase64
- res["Driver"] = driver_obj
- res["Driver"]["AssignedVehicle"] = driver.vehicle
- return res
-
-
- def get_all_fueling_tasks(db: Session):
- fuel_task = db.query(FuelingTask).all()
- result = []
- for task in fuel_task:
- res = task.__dict__
- res["ImageBefore"] = ""
- res["ImageAfter"] = ""
- res["Creator"] = task.CreatedById
- driver = get_user_by_id(task.DriverId, role="Driver", db=db)
- driver_obj = driver.__dict__
- res["Driver"] = driver_obj
- res["Driver"]["AssignedVehicle"] = driver.vehicle
- result.append(res)
- x = {"FuelingTasks": result}
- return x
|