|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- from sqlalchemy.orm import Session
- from schemas.maintenancejob import CreateMaintenanceJob
- from db.models.maintenancejob import MaintenanceJob
- from schemas.carpart import CreateCarPart
- from db.models.carpart import CarPart
-
-
- def create_new_maintenancejob(
- maintenancejob: CreateMaintenanceJob, maintenanceworker, db: Session
- ):
- maintenancejob_object = MaintenanceJob(
- MaintenanceWorker=maintenanceworker,
- Description=maintenancejob.Description,
- VehicleID=maintenancejob.VehicleID,
- Date=maintenancejob.Date,
- )
- print("OBJECT CREATED")
- db.add(maintenancejob_object)
- db.commit()
- db.refresh(maintenancejob_object)
- return maintenancejob_object
-
-
- def create_car_part(car_part: CreateCarPart, db: Session):
- car_part_object = CarPart(
- ParentId=car_part.ParentId,
- Name=car_part.Name,
- Number=car_part.Number,
- Condition=car_part.Condition,
- ImageURL=car_part.image.file.read(),
- Cost=car_part.Cost,
- )
- print("OBJECT CREATED")
- db.add(car_part_object)
- db.commit()
- print("OBJECT SAVED")
- db.refresh(car_part_object)
- print("OBJECT REFRESHED")
- return car_part_object
-
-
- def calculate_total_cost(car_parts: CarPart):
- total_cost = 0
- for part in car_parts:
- total_cost += part.Cost
- return total_cost
-
-
- def get_all_maintenance_jobs(db: Session):
- maintenancejobs = db.query(MaintenanceJob).all()
- result = []
- for job in maintenancejobs:
- job_dict = job.__dict__
- job_dict["CarPartsList"] = [part.__dict__ for part in job.CarParts]
- job_dict["TotalCost"] = calculate_total_cost(job.CarParts)
- job_dict["AssignedTo"] = job.CreatedBy.__dict__
- job_dict["Vehicle"] = job.Vehicle.__dict__
- result.append(job_dict)
- return maintenancejobs
-
-
- def get_maintenance_job(maintenancejob_id: int, db: Session):
- maintenancejob = (
- db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
- )
- maintenancejob.CarPartsList = [part.__dict__ for part in maintenancejob.CarParts]
- # print(type(result.CarPartsList))
- maintenancejob.TotalCost = calculate_total_cost(maintenancejob.CarParts)
- # print(result.TotalCost)
- maintenancejob.AssignedTo = maintenancejob.CreatedBy.__dict__
- maintenancejob.Vehicle = maintenancejob.Vehicle.__dict__
- print(maintenancejob.AssignedTo)
- print("DB Access complete")
- return maintenancejob
-
-
- def change_maintenance_status(maintenancejob_id: int, status: str, db: Session):
- maintenancejob = (
- db.query(MaintenanceJob).filter(MaintenanceJob.Id == maintenancejob_id).first()
- )
- if maintenancejob is None:
- return None
- maintenancejob.Status = status
- db.commit()
- db.refresh(maintenancejob)
- return maintenancejob
|