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.

82 lines
2.5 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. Date=maintenancejob.Date,
  13. )
  14. print("OBJECT CREATED")
  15. db.add(maintenancejob_object)
  16. db.commit()
  17. db.refresh(maintenancejob_object)
  18. return maintenancejob_object
  19. def create_car_part(car_part: CreateCarPart, db: Session):
  20. car_part_object = CarPart(
  21. ParentId=car_part.ParentId,
  22. Name=car_part.Name,
  23. Number=car_part.Number,
  24. Condition=car_part.Condition,
  25. ImageURL=car_part.ImageURL,
  26. Cost=car_part.Cost,
  27. )
  28. print("OBJECT CREATED")
  29. db.add(car_part_object)
  30. db.commit()
  31. db.refresh(car_part_object)
  32. return car_part_object
  33. def calculate_total_cost(car_parts: CarPart):
  34. total_cost = 0
  35. for part in car_parts:
  36. total_cost += part.Cost
  37. return total_cost
  38. def get_all_maintenance_jobs(db: Session):
  39. maintenancejobs = db.query(MaintenanceJob).all()
  40. result = []
  41. for job in maintenancejobs:
  42. job_dict = job.__dict__
  43. job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
  44. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  45. job_dict["AssignedTo"] = job.CreatedBy.__dict__
  46. result.append(job_dict)
  47. return maintenancejobs
  48. def get_maintenance_job(maintenancejob_id: int, db: Session):
  49. maintenancejob = (
  50. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  51. )
  52. maintenancejob.CarPartsList = [part.__dict__ for part in maintenancejob.CarParts]
  53. # print(type(result.CarPartsList))
  54. maintenancejob.TotalCost = calculate_total_cost(maintenancejob.CarParts)
  55. # print(result.TotalCost)
  56. maintenancejob.AssignedTo = maintenancejob.CreatedBy.__dict__
  57. print(maintenancejob.AssignedTo)
  58. print("DB Access complete")
  59. return maintenancejob
  60. def change_maintenance_status(maintenancejob_id: int, status: str, db: Session):
  61. maintenancejob = (
  62. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  63. )
  64. if maintenancejob is None:
  65. return None
  66. maintenancejob.Status = status
  67. db.commit()
  68. db.refresh(maintenancejob)
  69. return maintenancejob