You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

119 lines
3.8 KiB

  1. import base64
  2. from sqlalchemy.orm import Session
  3. from schemas.maintenancejob import CreateMaintenanceJob
  4. from db.models.maintenancejob import MaintenanceJob
  5. from schemas.carpart import CreateCarPart
  6. from db.models.carpart import CarPart
  7. from db.repository.user import get_car_driver
  8. def create_new_maintenancejob(
  9. maintenancejob: CreateMaintenanceJob, db: Session
  10. ):
  11. vehicledriver = get_car_driver(maintenancejob.VehicleID, db)
  12. if (vehicledriver != False):
  13. vehicledriver = vehicledriver.Id
  14. else:
  15. return "nodriver"
  16. maintenancejob_object = MaintenanceJob(
  17. Description=maintenancejob.Description,
  18. VehicleID=maintenancejob.VehicleID,
  19. Date=maintenancejob.Date,
  20. VehicleDriverId=get_car_driver(maintenancejob.VehicleID, db).Id,
  21. Status="Requested",
  22. )
  23. print("OBJECT CREATED")
  24. db.add(maintenancejob_object)
  25. db.commit()
  26. db.refresh(maintenancejob_object)
  27. return maintenancejob_object
  28. def create_car_part(car_part: CreateCarPart, db: Session):
  29. car_part_object = CarPart(
  30. ParentId=car_part.ParentId,
  31. Name=car_part.Name,
  32. Number=car_part.Number,
  33. Condition=car_part.Condition,
  34. ImageURL=car_part.image.file.read(),
  35. Cost=car_part.Cost,
  36. )
  37. print("OBJECT CREATED")
  38. db.add(car_part_object)
  39. db.commit()
  40. print("OBJECT SAVED")
  41. db.refresh(car_part_object)
  42. print("OBJECT REFRESHED")
  43. res_obj = car_part_object.__dict__
  44. res_obj["image"] = base64.b64encode(car_part_object.ImageURL).decode("ascii")
  45. return res_obj
  46. def calculate_total_cost(car_parts: CarPart):
  47. total_cost = 0
  48. for part in car_parts:
  49. total_cost += part.Cost
  50. return total_cost
  51. def get_all_maintenance_jobs(db: Session):
  52. maintenancejobs = db.query(MaintenanceJob).all()
  53. print("DB Access complete")
  54. result = []
  55. for job in maintenancejobs:
  56. job_dict = job.__dict__
  57. print(job_dict)
  58. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  59. print(len(job_dict["CarPartsList"]))
  60. for part in job_dict["CarPartsList"]:
  61. if (part["ImageURL"] is None) or (part["ImageURL"] == ""):
  62. part["image"] = ""
  63. else:
  64. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  65. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  66. job_dict["AssignedTo"] = job.CreatedBy.__dict__
  67. job_dict["Vehicle"] = job.Vehicle.__dict__
  68. result.append(job_dict)
  69. print("Returning...")
  70. return maintenancejobs
  71. def get_maintenance_job(maintenancejob_id: int, db: Session):
  72. maintenancejob = (
  73. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  74. )
  75. if maintenancejob is None:
  76. return None
  77. res = maintenancejob.__dict__
  78. res["CarPartsList"] = [part.__dict__ for part in maintenancejob.CarParts]
  79. for part in maintenancejob.CarPartsList:
  80. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  81. # print(type(result.CarPartsList))
  82. res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts)
  83. # print(result.TotalCost)
  84. if maintenancejob.Status == "Complete":
  85. res["FinishedBy"] = maintenancejob.FinishedBy.__dict__
  86. res["Vehicle"] = maintenancejob.Vehicle.__dict__
  87. print("DB Access complete")
  88. return maintenancejob
  89. def change_maintenance_status(maintenancejob_id: int, status: str, worker_id: int, db: Session):
  90. maintenancejob = (
  91. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  92. )
  93. if maintenancejob is None:
  94. return None
  95. if (status == "Complete"):
  96. maintenancejob.MaintenanceWorker = worker_id
  97. maintenancejob.Status = status
  98. db.commit()
  99. db.refresh(maintenancejob)
  100. return maintenancejob
  101. else:
  102. return None