diff --git a/app/controllers/recipes_controller.rb b/app/controllers/recipes_controller.rb index 4c67f8c..24dcff8 100644 --- a/app/controllers/recipes_controller.rb +++ b/app/controllers/recipes_controller.rb @@ -20,6 +20,41 @@ def create @recipe.creator = params[:recipe][:creator] @recipe.submitter = current_user @recipe.save + + params[:IngredientsRecipes].each do | ingredient | + if (Ingredient.find_by(name: ingredient[:name])) + @component = Ingredient.find_by(name: ingredient[:name])) + else + @component = Ingredient.new + @component.name = ingredient[:name] + @component.save + end + + ing = IngredientsRecipes.new + ing.amount = IngredientsRecipes[:amount] + ing.measurement = IngredientsRecipes[:measurement] + ing.recipe = @recipe + ing.name = @component + ing.save + end + end + def update + recipe = Recipe.find_by(name: params[:name]) + recipe.update(recipe_params) + recipe.save + render json: recipe + end + + def delete + recipe = Recipe.find_by(id: params[:recipe_id]) + recipe.destroy + end + + private + def recipe_params + params.require(:recipe).permit(:name) + end + end