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, 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.ImageURL, Cost=car_part.Cost, ) print("OBJECT CREATED") db.add(car_part_object) db.commit() db.refresh(car_part_object) 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["CarParts"] = [part.__dict__ for part in job.CarParts] job_dict["TotalCost"] = calculate_total_cost(job.CarParts) 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() ) result = maintenancejob.__dict__ result["CarParts"] = [part.__dict__ for part in maintenancejob.CarParts] return result 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