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.
 
 

97 lines
3.2 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. result = []
  49. for job in maintenancejobs:
  50. job_dict = job.__dict__
  51. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  52. for part in job_dict["CarPartsList"]:
  53. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  54. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  55. job_dict["AssignedTo"] = job.CreatedBy.__dict__
  56. job_dict["Vehicle"] = job.Vehicle.__dict__
  57. result.append(job_dict)
  58. return maintenancejobs
  59. def get_maintenance_job(maintenancejob_id: int, db: Session):
  60. maintenancejob = (
  61. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  62. )
  63. res = maintenancejob.__dict__
  64. res["CarPartsList"] = [part.__dict__ for part in maintenancejob.CarParts]
  65. for part in maintenancejob.CarPartsList:
  66. part["image"] = base64.b64encode(part["ImageURL"]).decode("ascii")
  67. # print(type(result.CarPartsList))
  68. res["TotalCost"] = calculate_total_cost(maintenancejob.CarParts)
  69. # print(result.TotalCost)
  70. res["AssignedTo"] = maintenancejob.CreatedBy.__dict__
  71. res["Vehicle"] = maintenancejob.Vehicle.__dict__
  72. print("DB Access complete")
  73. return maintenancejob
  74. def change_maintenance_status(maintenancejob_id: int, status: str, db: Session):
  75. maintenancejob = (
  76. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  77. )
  78. if maintenancejob is None:
  79. return None
  80. maintenancejob.Status = status
  81. db.commit()
  82. db.refresh(maintenancejob)
  83. return maintenancejob