-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyle Zarazan
committed
Nov 27, 2024
1 parent
bd06def
commit bf460f3
Showing
14 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class MealPlansController < ApplicationController | ||
|
||
def create | ||
@meal_plan = MealPlan.new(meal_plan_params) | ||
|
||
if @meal_plan.save | ||
render json: @meal_plan, status: :created | ||
else | ||
render json: @meal_plan.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def show | ||
@meal_plan = MealPlan.find(params[:id]) | ||
end | ||
|
||
def add_recipe | ||
@meal_plan = MealPlan.find(params[:id]) | ||
@recipe = Recipe.find(params[:recipe_id]) | ||
|
||
if @meal_plan.recipes << @recipe | ||
render json: @meal_plan, status: :ok | ||
else | ||
render json: { error: "Could not add recipe to meal plan" }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def remove_recipe | ||
@meal_plan = MealPlan.find(params[:id]) | ||
@recipe = Recipe.find(params[:recipe_id]) | ||
|
||
if @meal_plan.recipes.delete(@recipe) | ||
render json: @meal_plan, status: :ok | ||
else | ||
render json: { error: "Could not remove recipe from meal plan" }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def meal_plan_params | ||
params.require(:meal_plan).permit(:name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class MealPlan < ApplicationRecord | ||
has_and_belongs_to_many :recipes | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateMealPlan < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :meal_plans do |t| | ||
t.name :string | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateMealPlansRecipes < ActiveRecord::Migration[8.0] | ||
def change | ||
create_join_table :meal_plans, :recipes do |t| | ||
t.integer :quantity | ||
t.index :recipe_id | ||
t.index :meal_plan_id | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.