|
- 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__
- fueling_task_object.Driver = driverobj
- 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__
- imagebefore = fuel_task.ImageBefore
- imageafter = fuel_task.ImageAfter
- imagebeforeBase64 = base64.b64encode(imagebefore).decode('ascii')
- imageafterBase64 = base64.b64encode(imageafter).decode('ascii')
- res["ImageBefore"] = imagebeforeBase64
- res["ImageAfter"] = imageafterBase64
- res["Driver"] = driver_obj
- res["Driver"]["AssignedVehicle"] = driver.vehicle
- return res
|