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.
 
 

106 lines
3.5 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, maintenanceworker, db: Session
  10. ):
  11. maintenancejob_object = MaintenanceJob(
  12. MaintenanceWorker=maintenanceworker,
  13. Description=maintenancejob.Description,
  14. VehicleID=maintenancejob.VehicleID,
  15. Date=maintenancejob.Date,
  16. VehicleDriverId=get_car_driver(maintenancejob.VehicleID, db).Id,
  17. )
  18. print("OBJECT CREATED")
  19. db.add(maintenancejob_object)
  20. db.commit()
  21. db.refresh(maintenancejob_object)
  22. return maintenancejob_object
  23. def create_car_part(car_part: CreateCarPart, db: Session):
  24. car_part_object = CarPart(
  25. ParentId=car_part.ParentId,
  26. Name=car_part.Name,
  27. Number=car_part.Number,
  28. Condition=car_part.Condition,
  29. ImageURL=car_part.image.file.read(),
  30. Cost=car_part.Cost,
  31. )
  32. print("OBJECT CREATED")
  33. db.add(car_part_object)
  34. db.commit()
  35. print("OBJECT SAVED")
  36. db.refresh(car_part_object)
  37. print("OBJECT REFRESHED")
  38. res_obj = car_part_object.__dict__
  39. res_obj["image"] = base64.b64encode(car_part_object.ImageURL).decode("ascii")
  40. return res_obj
  41. def calculate_total_cost(car_parts: CarPart):
  42. total_cost = 0
  43. for part in car_parts:
  44. total_cost += part.Cost
  45. return total_cost
  46. def get_all_maintenance_jobs(db: Session):
  47. maintenancejobs = db.query(MaintenanceJob).all()
  48. print("DB Access complete")
  49. result = []
  50. for job in maintenancejobs:
  51. job_dict = job.__dict__
  52. print(job_dict)
  53. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  54. print(len(job_dict["CarPartsList"]))
  55. for part in job_dict["CarPartsList"]:
  56. if (part["ImageURL"] is None) or (part["ImageURL"] == ""):
  57. part["image"] = ""
  58. else:
  59. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  60. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  61. job_dict["AssignedTo"] = job.CreatedBy.__dict__
  62. job_dict["Vehicle"] = job.Vehicle.__dict__
  63. result.append(job_dict)
  64. print("Returning...")
  65. return maintenancejobs
  66. def get_maintenance_job(maintenancejob_id: int, db: Session):
  67. maintenancejob = (
  68. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  69. )
  70. if maintenancejob is None:
  71. return None
  72. res = maintenancejob.__dict__
  73. res["CarPartsList"] = [part.__dict__ for part in maintenancejob.CarParts]
  74. for part in maintenancejob.CarPartsList:
  75. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  76. # print(type(result.CarPartsList))
  77. res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts)
  78. # print(result.TotalCost)
  79. res["AssignedTo"] = maintenancejob.CreatedBy.__dict__
  80. res["Vehicle"] = maintenancejob.Vehicle.__dict__
  81. print("DB Access complete")
  82. return maintenancejob
  83. def change_maintenance_status(maintenancejob_id: int, status: str, db: Session):
  84. maintenancejob = (
  85. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  86. )
  87. if maintenancejob is None:
  88. return None
  89. maintenancejob.Status = status
  90. db.commit()
  91. db.refresh(maintenancejob)
  92. return maintenancejob