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.

64 lines
1.9 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["CarParts"] = [part.__dict__ for part in job.CarParts]
  44. job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
  45. result.append(job_dict)
  46. return maintenancejobs
  47. def get_maintenance_job(maintenancejob_id: int, db: Session):
  48. maintenancejob = (
  49. db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
  50. )
  51. result = maintenancejob.__dict__
  52. result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts]
  53. return result