Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

maintenancejob.py 2.5 KiB

před 1 rokem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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