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.
 
 

87 lines
2.8 KiB

  1. from sqlalchemy.orm import Session
  2. from schemas.maintenancejob import CreateMaintenanceJob
  3. from db.models.maintenancejob import MaintenanceJob
  4. from schemas.carpart import CreateCarPart
  5. from db.models.carpart import CarPart
  6. def create_new_maintenancejob(
  7. maintenancejob: CreateMaintenanceJob, maintenanceworker, db: Session
  8. ):
  9. maintenancejob_object = MaintenanceJob(
  10. MaintenanceWorker=maintenanceworker,
  11. Description=maintenancejob.Description,
  12. VehicleID=maintenancejob.VehicleID,
  13. Date=maintenancejob.Date,
  14. )
  15. print("OBJECT CREATED")
  16. db.add(maintenancejob_object)
  17. db.commit()
  18. db.refresh(maintenancejob_object)
  19. return maintenancejob_object
  20. def create_car_part(car_part: CreateCarPart, db: Session):
  21. car_part_object = CarPart(
  22. ParentId=car_part.ParentId,
  23. Name=car_part.Name,
  24. Number=car_part.Number,
  25. Condition=car_part.Condition,
  26. ImageURL=car_part.image.file.read(),
  27. Cost=car_part.Cost,
  28. )
  29. print("OBJECT CREATED")
  30. db.add(car_part_object)
  31. db.commit()
  32. print("OBJECT SAVED")
  33. db.refresh(car_part_object)
  34. print("OBJECT REFRESHED")
  35. return car_part_object
  36. def calculate_total_cost(car_parts: CarPart):
  37. total_cost = 0
  38. for part in car_parts:
  39. total_cost += part.Cost
  40. return total_cost
  41. def get_all_maintenance_jobs(db: Session):
  42. maintenancejobs = db.query(MaintenanceJob).all()
  43. result = []
  44. for job in maintenancejobs:
  45. job_dict = job.__dict__
  46. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  47. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  48. job_dict["AssignedTo"] = job.CreatedBy.__dict__
  49. job_dict["Vehicle"] = job.Vehicle.__dict__
  50. result.append(job_dict)
  51. return maintenancejobs
  52. def get_maintenance_job(maintenancejob_id: int, db: Session):
  53. maintenancejob = (
  54. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  55. )
  56. maintenancejob.CarPartsList = [part.__dict__ for part in maintenancejob.CarParts]
  57. # print(type(result.CarPartsList))
  58. maintenancejob.TotalCost = calculate_total_cost(maintenancejob.CarParts)
  59. # print(result.TotalCost)
  60. maintenancejob.AssignedTo = maintenancejob.CreatedBy.__dict__
  61. maintenancejob.Vehicle = maintenancejob.Vehicle.__dict__
  62. print(maintenancejob.AssignedTo)
  63. print("DB Access complete")
  64. return maintenancejob
  65. def change_maintenance_status(maintenancejob_id: int, status: str, db: Session):
  66. maintenancejob = (
  67. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  68. )
  69. if maintenancejob is None:
  70. return None
  71. maintenancejob.Status = status
  72. db.commit()
  73. db.refresh(maintenancejob)
  74. return maintenancejob