from sqlalchemy.orm import sessionmaker from backend.app.db.connection import db_engine from backend.app.db.models.recommendations import Plan, Recommendation, RecommendationMaterials def create_plan(plan): """ This function will create a record for the plan in the database if it does not exist. :param plan: dictionary of data representing a plan to be created """ Session = sessionmaker(bind=db_engine) with Session() as session: new_plan = Plan(**plan) session.add(new_plan) session.commit() return new_plan.id def create_recommendation(recommendation): """ This function will create a record for the recommendation in the database if it does not exist. :param recommendation: dictionary of data representing a recommendation to be created """ Session = sessionmaker(bind=db_engine) with Session() as session: new_recommendation = Recommendation(**recommendation) session.add(new_recommendation) session.commit() return new_recommendation.id def create_recommendation_material(recommendation_id, material_id, depth): """ This function will create a record for the recommendation_material in the database if it does not exist. :param recommendation_id: ID of the recommendation :param material_id: ID of the material :param depth: depth of the material, may be null if a material where depth is not applicable """ Session = sessionmaker(bind=db_engine) with Session() as session: new_recommendation_material = RecommendationMaterials( recommendation_id=recommendation_id, material_id=material_id, depth=depth ) session.add(new_recommendation_material) session.commit() return new_recommendation_material.id def create_plan_recommendations(plan_id, recommendation_ids): """ This function will create a record for the plan_recommendation in the database if it does not exist. :param plan_id: ID of the plan :param recommendation_ids: list of recommendation IDs """ Session = sessionmaker(bind=db_engine) with Session() as session: for recommendation_id in recommendation_ids: session.execute( 'INSERT INTO plan_recommendations (plan_id, recommendation_id) VALUES (:plan_id, :recommendation_id)', {'plan_id': plan_id, 'recommendation_id': recommendation_id} ) session.commit()