From cb4aeebf7a9ff4744d13bf9e165366ff1a08dab7 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 26 Jan 2021 10:27:01 -0500 Subject: [PATCH 01/15] initial commit --- bin/run.rb | 3 ++- config/environment.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/run.rb b/bin/run.rb index cf08c338..4a9501bc 100644 --- a/bin/run.rb +++ b/bin/run.rb @@ -1,5 +1,6 @@ require_relative '../config/environment' - + puts "HELLO WORLD" + diff --git a/config/environment.rb b/config/environment.rb index 4dbe13e5..667889d0 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,5 @@ require 'bundler' Bundler.require - + ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') require_all 'lib' From 0f3f8514540d63419b4ca6e1fe345eff8a3257ea Mon Sep 17 00:00:00 2001 From: Jade McDaniels Date: Tue, 26 Jan 2021 10:34:18 -0500 Subject: [PATCH 02/15] testing --- bin/run.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/run.rb b/bin/run.rb index cf08c338..09f05988 100644 --- a/bin/run.rb +++ b/bin/run.rb @@ -3,3 +3,6 @@ puts "HELLO WORLD" + + +puts "Hello World" From 3c3beb956c757c4002316e98b6f6522401155374 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 26 Jan 2021 17:52:24 -0500 Subject: [PATCH 03/15] schema built --- Gemfile | 2 +- Gemfile.lock | 69 ++++++++++----------- app/models/ingredient.rb | 5 ++ app/models/ingredient_recipe.rb | 4 ++ app/models/recipe.rb | 5 ++ app/models/user.rb | 5 ++ app/models/user_recipe.rb | 4 ++ config/environment.rb | 4 +- db/migrate/001_create_recipes.rb | 8 +++ db/migrate/002_create_ingredients.rb | 8 +++ db/migrate/003_create_users.rb | 7 +++ db/migrate/004_create_ingredient_recipes.rb | 8 +++ db/migrate/005_create_user_recipes.rb | 9 +++ db/schema.rb | 50 +++++++++++++++ 14 files changed, 150 insertions(+), 38 deletions(-) create mode 100644 app/models/ingredient.rb create mode 100644 app/models/ingredient_recipe.rb create mode 100644 app/models/recipe.rb create mode 100644 app/models/user.rb create mode 100644 app/models/user_recipe.rb create mode 100644 db/migrate/001_create_recipes.rb create mode 100644 db/migrate/002_create_ingredients.rb create mode 100644 db/migrate/003_create_users.rb create mode 100644 db/migrate/004_create_ingredient_recipes.rb create mode 100644 db/migrate/005_create_user_recipes.rb create mode 100644 db/schema.rb diff --git a/Gemfile b/Gemfile index c004f4ca..4dd3567d 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,6 @@ source "https://rubygems.org" gem "sinatra-activerecord" -gem "sqlite3" +gem "sqlite3", "~> 1.4.0" gem "pry" gem "require_all" diff --git a/Gemfile.lock b/Gemfile.lock index 9589226d..b3e23b8e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,47 +1,46 @@ GEM remote: https://rubygems.org/ specs: - activemodel (6.0.3.1) - activesupport (= 6.0.3.1) - activerecord (6.0.3.1) - activemodel (= 6.0.3.1) - activesupport (= 6.0.3.1) - activesupport (6.0.3.1) + activemodel (6.1.1) + activesupport (= 6.1.1) + activerecord (6.1.1) + activemodel (= 6.1.1) + activesupport (= 6.1.1) + activesupport (6.1.1) concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 0.7, < 2) - minitest (~> 5.1) - tzinfo (~> 1.1) - zeitwerk (~> 2.2, >= 2.2.2) - coderay (1.1.1) - concurrent-ruby (1.1.6) - i18n (1.8.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + zeitwerk (~> 2.3) + coderay (1.1.3) + concurrent-ruby (1.1.8) + i18n (1.8.7) concurrent-ruby (~> 1.0) - method_source (0.8.2) - minitest (5.14.1) - mustermann (1.0.3) - pry (0.10.4) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) + method_source (1.0.0) + minitest (5.14.3) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) rack (2.2.3) - rack-protection (2.0.7) + rack-protection (2.1.0) rack - require_all (1.3.3) - sinatra (2.0.7) + require_all (3.0.0) + ruby2_keywords (0.0.4) + sinatra (2.1.0) mustermann (~> 1.0) - rack (~> 2.0) - rack-protection (= 2.0.7) + rack (~> 2.2) + rack-protection (= 2.1.0) tilt (~> 2.0) - sinatra-activerecord (2.0.12) - activerecord (>= 3.2) + sinatra-activerecord (2.0.22) + activerecord (>= 4.1) sinatra (>= 1.0) - slop (3.6.0) - sqlite3 (1.3.13) - thread_safe (0.3.6) + sqlite3 (1.4.2) tilt (2.0.10) - tzinfo (1.2.7) - thread_safe (~> 0.1) - zeitwerk (2.3.0) + tzinfo (2.0.4) + concurrent-ruby (~> 1.0) + zeitwerk (2.4.2) PLATFORMS ruby @@ -50,7 +49,7 @@ DEPENDENCIES pry require_all sinatra-activerecord - sqlite3 + sqlite3 (~> 1.4.0) BUNDLED WITH - 1.14.6 + 2.2.3 diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb new file mode 100644 index 00000000..be80c0ab --- /dev/null +++ b/app/models/ingredient.rb @@ -0,0 +1,5 @@ +class Ingredient < ActiveRecord::Base + has_many :recipes + + +end diff --git a/app/models/ingredient_recipe.rb b/app/models/ingredient_recipe.rb new file mode 100644 index 00000000..386b985e --- /dev/null +++ b/app/models/ingredient_recipe.rb @@ -0,0 +1,4 @@ +class IngredientRecipe < ActiveRecord::Base + belongs_to :ingredient + belongs_to :recipe +end \ No newline at end of file diff --git a/app/models/recipe.rb b/app/models/recipe.rb new file mode 100644 index 00000000..de7807f4 --- /dev/null +++ b/app/models/recipe.rb @@ -0,0 +1,5 @@ +class Recipe < ActiveRecord::Base + has_many :ingredients, through: :ingredient_recipes + has_many :users, through: :user_recipes +end + diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 00000000..f0f2f0ae --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,5 @@ +class User < ActiveRecord::Base + has_many :recipes + has_many :ingredients, through: :recipes + +end \ No newline at end of file diff --git a/app/models/user_recipe.rb b/app/models/user_recipe.rb new file mode 100644 index 00000000..2c7fb574 --- /dev/null +++ b/app/models/user_recipe.rb @@ -0,0 +1,4 @@ +class UserRecipe < ActiveRecord::Base + belongs_to :user + belongs_to :recipe +end \ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index 667889d0..2a7073f6 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,5 @@ require 'bundler' Bundler.require - + ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') -require_all 'lib' +require_all 'app' diff --git a/db/migrate/001_create_recipes.rb b/db/migrate/001_create_recipes.rb new file mode 100644 index 00000000..7ac876a0 --- /dev/null +++ b/db/migrate/001_create_recipes.rb @@ -0,0 +1,8 @@ +class CreateRecipes < ActiveRecord::Migration[5.1] + def change + create_table :recipes do |t| + t.string :name + t.text :instructions + end + end +end \ No newline at end of file diff --git a/db/migrate/002_create_ingredients.rb b/db/migrate/002_create_ingredients.rb new file mode 100644 index 00000000..8b71cef1 --- /dev/null +++ b/db/migrate/002_create_ingredients.rb @@ -0,0 +1,8 @@ +class CreateIngredients < ActiveRecord::Migration[5.1] + def change + create_table :ingredients do |t| + t.string :name + t.string :type + end + end +end \ No newline at end of file diff --git a/db/migrate/003_create_users.rb b/db/migrate/003_create_users.rb new file mode 100644 index 00000000..027c8b61 --- /dev/null +++ b/db/migrate/003_create_users.rb @@ -0,0 +1,7 @@ +class CreateUsers < ActiveRecord::Migration[5.1] + def change + create_table :users do |t| + t.string :name + end + end +end \ No newline at end of file diff --git a/db/migrate/004_create_ingredient_recipes.rb b/db/migrate/004_create_ingredient_recipes.rb new file mode 100644 index 00000000..c00ea099 --- /dev/null +++ b/db/migrate/004_create_ingredient_recipes.rb @@ -0,0 +1,8 @@ +class CreateIngredientRecipes < ActiveRecord::Migration[5.1] + def change + create_table :ingredient_recipes do |t| + t.integer :ingredient_id + t.integer :recipe_id + end + end +end \ No newline at end of file diff --git a/db/migrate/005_create_user_recipes.rb b/db/migrate/005_create_user_recipes.rb new file mode 100644 index 00000000..7254b9c5 --- /dev/null +++ b/db/migrate/005_create_user_recipes.rb @@ -0,0 +1,9 @@ +class CreateUserRecipes < ActiveRecord::Migration[5.1] + def change + create_table :user_recipes do |t| + t.integer :user_id + t.integer :recipe_id + t.integer :rating + end + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..7692c765 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,50 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 5) do + + create_table "ingredient_recipes", force: :cascade do |t| + t.integer "ingredient_id" + t.integer "recipe_id" + end + + create_table "ingredients", force: :cascade do |t| + t.string "name" + t.string "type" + end + + create_table "recipes", force: :cascade do |t| + t.string "name" + t.string "ingredient_1" + t.string "measurement_1" + t.string "ingredient_2" + t.string "measurement_2" + t.string "ingredient_3" + t.string "measurement_3" + t.string "ingredient_4" + t.string "measurement_4" + t.string "ingredient_5" + t.string "measurement_5" + t.text "instructions" + end + + create_table "user_recipes", force: :cascade do |t| + t.integer "user_id" + t.integer "recipe_id" + t.integer "rating" + end + + create_table "users", force: :cascade do |t| + t.string "name" + end + +end From 599e9fe6ccb967fb4908b0796208358dd2366d8b Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 26 Jan 2021 18:51:29 -0500 Subject: [PATCH 04/15] outline methods in run.rb --- bin/run.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/bin/run.rb b/bin/run.rb index 4a9501bc..139adfe2 100644 --- a/bin/run.rb +++ b/bin/run.rb @@ -2,5 +2,26 @@ -puts "HELLO WORLD" +puts "Welcome to the Cocktail Recipe Interface" +puts "Please enter your username" +user_name = gets.chomp #call find_or_create_by_name method in user.rb +puts "What would you like to do?" +puts "1. Find a random recipe by ingredient" + #call suggest_random_recipe using find_recipe_by_ingredient method and then + #picking a random array element in recipe.rb + #be sure to prompt_user_for_rating +puts "2. Show my favorite recipes" + #traverse through user_recipe table to find highest ratings and return as an array +puts "3. Suggest a new recipe" + #traverse through recipes and spit out random one + #be sure to prompt_user_for_rating +puts "4. Look up a recipe by recipe name" + #call find_recipe_by_name +puts "5. List all recipes with a certain ingredient" + #call find_recipe_by_ingredient method and return an array of choices +choice = gets.chomp + #if choice == 1 do blah blah blah etc. +#make method prompt_user_for_rating which will ask for a rating +#then store it with the recipe in the user_recipe table +#make method update_rating ? From a18455cae45807b6a663c09c3c537d88336f93da Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 27 Jan 2021 09:11:40 -0500 Subject: [PATCH 05/15] initial_api_stuff --- Gemfile | 1 + Gemfile.lock | 18 ++++++++++++++++++ app/models/recipe.rb | 5 +++++ bin/api_parsing.rb | 31 +++++++++++++++++++++++++++++++ bin/run.rb | 10 ++++++++++ 5 files changed, 65 insertions(+) create mode 100644 bin/api_parsing.rb mode change 100644 => 100755 bin/run.rb diff --git a/Gemfile b/Gemfile index 4dd3567d..840a55ab 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,4 @@ gem "sinatra-activerecord" gem "sqlite3", "~> 1.4.0" gem "pry" gem "require_all" +gem "rest-client" diff --git a/Gemfile.lock b/Gemfile.lock index b3e23b8e..0d650116 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,12 +14,21 @@ GEM zeitwerk (~> 2.3) coderay (1.1.3) concurrent-ruby (1.1.8) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) + http-accept (1.7.0) + http-cookie (1.0.3) + domain_name (~> 0.5) i18n (1.8.7) concurrent-ruby (~> 1.0) method_source (1.0.0) + mime-types (3.3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2020.1104) minitest (5.14.3) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) + netrc (0.11.0) pry (0.13.1) coderay (~> 1.1) method_source (~> 1.0) @@ -27,6 +36,11 @@ GEM rack-protection (2.1.0) rack require_all (3.0.0) + rest-client (2.1.0) + http-accept (>= 1.7.0, < 2.0) + http-cookie (>= 1.0.2, < 2.0) + mime-types (>= 1.16, < 4.0) + netrc (~> 0.8) ruby2_keywords (0.0.4) sinatra (2.1.0) mustermann (~> 1.0) @@ -40,6 +54,9 @@ GEM tilt (2.0.10) tzinfo (2.0.4) concurrent-ruby (~> 1.0) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.7) zeitwerk (2.4.2) PLATFORMS @@ -48,6 +65,7 @@ PLATFORMS DEPENDENCIES pry require_all + rest-client sinatra-activerecord sqlite3 (~> 1.4.0) diff --git a/app/models/recipe.rb b/app/models/recipe.rb index de7807f4..4aba7059 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -1,5 +1,10 @@ class Recipe < ActiveRecord::Base has_many :ingredients, through: :ingredient_recipes has_many :users, through: :user_recipes + + def self.import_recipe(:name, :instructions) + Recipe.create(:name, :instructions) + IngredientRecipe.create(recipe_id: self.id) + end end diff --git a/bin/api_parsing.rb b/bin/api_parsing.rb new file mode 100644 index 00000000..f340226c --- /dev/null +++ b/bin/api_parsing.rb @@ -0,0 +1,31 @@ +require_relative '../config/environment' +require 'rest-client' +require 'json' +require 'pry' + +def enter_name + puts "Enter drink name" + name = gets.chomp +end + +def get_data(name) + # binding.pry + + url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=#{name}" + response = RestClient.get(url) + result = JSON.parse(response) + + while result["drinks"] == nil + puts "Drink not found. Please input another recipe name." + name = gets.chomp + url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=#{name}" + response = RestClient.get(url) + result = JSON.parse(response) + end + binding.pry + drink_name = result["drinks"][0]["strDrink"] + drink_instructions = result["drinks"][0]["strInstructions"] + Recipe.import_recipe(name: drink_name, instructions: drink_instructions) + +end + diff --git a/bin/run.rb b/bin/run.rb old mode 100644 new mode 100755 index 139adfe2..757989c8 --- a/bin/run.rb +++ b/bin/run.rb @@ -1,4 +1,5 @@ require_relative '../config/environment' +require_relative 'api_parsing' @@ -16,10 +17,19 @@ #traverse through recipes and spit out random one #be sure to prompt_user_for_rating puts "4. Look up a recipe by recipe name" + #call find_recipe_by_name puts "5. List all recipes with a certain ingredient" #call find_recipe_by_ingredient method and return an array of choices choice = gets.chomp +puts "Thank you" +# binding.pry +choice = choice.to_i +if choice == 4 + puts "Enter the name of your drink" + name = gets.chomp + get_data(name) +end #if choice == 1 do blah blah blah etc. #make method prompt_user_for_rating which will ask for a rating From 6014d26f2a2b20b700ee0ffb98e42f7bfe756367 Mon Sep 17 00:00:00 2001 From: Jade McDaniels Date: Wed, 27 Jan 2021 10:17:07 -0500 Subject: [PATCH 06/15] updating --- bin/run.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/run.rb b/bin/run.rb index cf08c338..a6e4ecce 100644 --- a/bin/run.rb +++ b/bin/run.rb @@ -2,4 +2,5 @@ -puts "HELLO WORLD" +puts "Test" + From 6d9da4e8fdd91093380eda850d47f889b608fd40 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 27 Jan 2021 12:31:10 -0500 Subject: [PATCH 07/15] api import working --- app/models/ingredient.rb | 7 +++++++ app/models/ingredient_recipe.rb | 8 ++++++++ app/models/recipe.rb | 9 ++++++--- bin/api_parsing.rb | 21 ++++++++++++++------- bin/run.rb | 7 +++---- db/schema.rb | 10 ---------- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index be80c0ab..cf3ebb29 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -1,5 +1,12 @@ class Ingredient < ActiveRecord::Base has_many :recipes + def self.add__or_create_ingredient(ingredient) + if Ingredient.all.find{|i| i.name == ingredient} + else + Ingredient.create(name: ingredient) + end + Ingredient.all.find{|i| i.name == ingredient} + end end diff --git a/app/models/ingredient_recipe.rb b/app/models/ingredient_recipe.rb index 386b985e..a9699e5a 100644 --- a/app/models/ingredient_recipe.rb +++ b/app/models/ingredient_recipe.rb @@ -1,4 +1,12 @@ class IngredientRecipe < ActiveRecord::Base belongs_to :ingredient belongs_to :recipe + + def self.find_or_create_ingredient_recipe(recipe_id, ingredient_id) + if IngredientRecipe.all.find {|ir| ir.recipe_id == recipe_id && ir.ingredient_id == ingredient_id} + else + IngredientRecipe.create(recipe_id: recipe_id, ingredient_id: ingredient_id) + end + IngredientRecipe.all.find {|ir| ir.recipe_id == recipe_id && ir.ingredient_id == ingredient_id} + end end \ No newline at end of file diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 4aba7059..fd781410 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -2,9 +2,12 @@ class Recipe < ActiveRecord::Base has_many :ingredients, through: :ingredient_recipes has_many :users, through: :user_recipes - def self.import_recipe(:name, :instructions) - Recipe.create(:name, :instructions) - IngredientRecipe.create(recipe_id: self.id) + def self.find_or_create_recipe(name, instructions) + if Recipe.all.find{|r| r.name == name} + else + Recipe.create(name: name, instructions: instructions) + end + Recipe.all.find{|r| r.name == name} end end diff --git a/bin/api_parsing.rb b/bin/api_parsing.rb index f340226c..a5bbe92c 100644 --- a/bin/api_parsing.rb +++ b/bin/api_parsing.rb @@ -3,13 +3,9 @@ require 'json' require 'pry' -def enter_name - puts "Enter drink name" - name = gets.chomp -end def get_data(name) - # binding.pry + url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=#{name}" response = RestClient.get(url) @@ -22,10 +18,21 @@ def get_data(name) response = RestClient.get(url) result = JSON.parse(response) end - binding.pry + drink_name = result["drinks"][0]["strDrink"] drink_instructions = result["drinks"][0]["strInstructions"] - Recipe.import_recipe(name: drink_name, instructions: drink_instructions) + current_ingr_num = 1 + current_recipe = Recipe.find_or_create_recipe(drink_name, drink_instructions) + + while result["drinks"][0]["strIngredient#{current_ingr_num}"] + current_ingredient = Ingredient.add__or_create_ingredient(result["drinks"][0]["strIngredient#{current_ingr_num}"]) + IngredientRecipe.find_or_create_ingredient_recipe(current_recipe.id, current_ingredient.id) + current_ingr_num += 1 + end + + + + end diff --git a/bin/run.rb b/bin/run.rb index 757989c8..7d40ded8 100755 --- a/bin/run.rb +++ b/bin/run.rb @@ -21,12 +21,11 @@ #call find_recipe_by_name puts "5. List all recipes with a certain ingredient" #call find_recipe_by_ingredient method and return an array of choices +puts "6. Import a recipe from the database" choice = gets.chomp -puts "Thank you" -# binding.pry choice = choice.to_i -if choice == 4 - puts "Enter the name of your drink" +if choice == 6 + puts "Enter the recipe name you are looking for" name = gets.chomp get_data(name) end diff --git a/db/schema.rb b/db/schema.rb index 7692c765..edd8452b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -24,16 +24,6 @@ create_table "recipes", force: :cascade do |t| t.string "name" - t.string "ingredient_1" - t.string "measurement_1" - t.string "ingredient_2" - t.string "measurement_2" - t.string "ingredient_3" - t.string "measurement_3" - t.string "ingredient_4" - t.string "measurement_4" - t.string "ingredient_5" - t.string "measurement_5" t.text "instructions" end From 50e50404f301484b69030e64660ae5807eac982c Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 27 Jan 2021 14:53:08 -0500 Subject: [PATCH 08/15] newest --- bin/api_parsing.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/api_parsing.rb b/bin/api_parsing.rb index a5bbe92c..c46dd6d7 100644 --- a/bin/api_parsing.rb +++ b/bin/api_parsing.rb @@ -10,7 +10,6 @@ def get_data(name) url = "https://www.thecocktaildb.com/api/json/v1/1/search.php?s=#{name}" response = RestClient.get(url) result = JSON.parse(response) - while result["drinks"] == nil puts "Drink not found. Please input another recipe name." name = gets.chomp @@ -23,7 +22,7 @@ def get_data(name) drink_instructions = result["drinks"][0]["strInstructions"] current_ingr_num = 1 current_recipe = Recipe.find_or_create_recipe(drink_name, drink_instructions) - + while result["drinks"][0]["strIngredient#{current_ingr_num}"] current_ingredient = Ingredient.add__or_create_ingredient(result["drinks"][0]["strIngredient#{current_ingr_num}"]) IngredientRecipe.find_or_create_ingredient_recipe(current_recipe.id, current_ingredient.id) From 26c0515baf9e2e8dfc65f4e09a8a79431ee19691 Mon Sep 17 00:00:00 2001 From: Jade McDaniels Date: Wed, 27 Jan 2021 14:56:11 -0500 Subject: [PATCH 09/15] changes --- app/models/ingredient_recipe.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/ingredient_recipe.rb b/app/models/ingredient_recipe.rb index 386b985e..20e28ed0 100644 --- a/app/models/ingredient_recipe.rb +++ b/app/models/ingredient_recipe.rb @@ -1,4 +1,9 @@ class IngredientRecipe < ActiveRecord::Base belongs_to :ingredient belongs_to :recipe + + def find_recipe_by_ingredient + IngredientRecipe + end + end \ No newline at end of file From 8421e3b3502572547702c4cf6a061faeb9ac946a Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 27 Jan 2021 17:47:26 -0500 Subject: [PATCH 10/15] choice 2/3 mostly done --- app/models/ingredient.rb | 3 ++- app/models/recipe.rb | 9 +++++++++ app/models/user.rb | 35 +++++++++++++++++++++++++++++++++-- bin/run.rb | 22 +++++++++++++++++++--- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index cf3ebb29..0e969809 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -1,5 +1,6 @@ class Ingredient < ActiveRecord::Base - has_many :recipes + has_many :ingredient_recipes + has_many :recipes, through: :ingredient_recipes def self.add__or_create_ingredient(ingredient) if Ingredient.all.find{|i| i.name == ingredient} diff --git a/app/models/recipe.rb b/app/models/recipe.rb index fd781410..1c49039c 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -1,4 +1,5 @@ class Recipe < ActiveRecord::Base + has_many :ingredient_recipes has_many :ingredients, through: :ingredient_recipes has_many :users, through: :user_recipes @@ -9,5 +10,13 @@ def self.find_or_create_recipe(name, instructions) end Recipe.all.find{|r| r.name == name} end + + def self.suggest_random_recipe + # binding.pry + Recipe.all[rand(Recipe.all.size)] + end + def self.list_my_ingredients(recipe) + recipe.ingredients.each {|i| puts i.name} + end end diff --git a/app/models/user.rb b/app/models/user.rb index 62047e25..fd8b8776 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,11 +1,11 @@ require 'pry' class User < ActiveRecord::Base - has_many :recipes + has_many :user_recipes has_many :ingredients, through: :recipes + has_many :recipes, through: :user_recipes def self.find_or_create_by_name(name) - if User.all.find {|user| user.name == name} else User.create name: name @@ -13,5 +13,36 @@ def self.find_or_create_by_name(name) User.all.find {|user| user.name == name} end + def rate_recipe(recipe) + puts "How would you rate this recipe? (1-5)" + rating = gets.chomp + rating = rating.to_i + while rating < 1 || rating > 5 + puts "Rating needs to be from 1-5, please try again." + puts "How would you rate this recipe? (1-5)" + rating = gets.chomp + rating = rating.to_i + end + if UserRecipe.all.find{|i| i.user_id == self.id && i.recipe_id == recipe.id} + puts "You have already rated this recipe!" + else + UserRecipe.create(user_id: self.id, recipe_id: recipe.id, rating: rating) + end + UserRecipe.all.select{|i| i.user_id == self.id && i.recipe_id == recipe.id} + end + + def show_highest_ratings + current_users_ratings = UserRecipe.all.select {|i| i.user_id == self.id} + ratings_over_4 = current_users_ratings.select {|i| i.rating >= 4} + + if ratings_over_4 + ratings_over_4.each do |i| + drink = Recipe.all.find {|recipe| recipe.id == i.recipe_id} + puts "#{drink.name}: #{i.rating} stars" + end + else + puts "You don't have any favorite recipes!" + end + end end \ No newline at end of file diff --git a/bin/run.rb b/bin/run.rb index 715dfa72..820bc3a8 100755 --- a/bin/run.rb +++ b/bin/run.rb @@ -7,7 +7,8 @@ puts "Please enter your username" user_name = gets.chomp #call find_or_create_by_name method in user.rb current_user = User.find_or_create_by_name(user_name) - binding.pry + +puts "Hello, #{user_name}," puts "What would you like to do?" puts "1. Find a random recipe by ingredient" #call suggest_random_recipe using find_recipe_by_ingredient method and then @@ -26,12 +27,27 @@ puts "6. Import a recipe from the database" choice = gets.chomp choice = choice.to_i -if choice == 6 +if choice == 2 + current_user.show_highest_ratings + +elsif choice == 3 + #Right now it just picks a random recipe even if the user has made it before + #Maybe change to only reccomend new recipes? + current_recipe = Recipe.suggest_random_recipe + puts "Time to make a(n) #{current_recipe.name}!" + puts "Here are the ingredients:" + Recipe.list_my_ingredients(current_recipe) + puts current_recipe.instructions + current_user.rate_recipe(current_recipe) + binding.pry + +elsif choice == 6 puts "Enter the recipe name you are looking for" name = gets.chomp get_data(name) + puts "The recipe for #{name} has been sucessfully imported." end - #if choice == 1 do blah blah blah etc. + #make method prompt_user_for_rating which will ask for a rating #then store it with the recipe in the user_recipe table From 231fdd309ddb077b0898e37c999b40d643f0c9e9 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 28 Jan 2021 12:07:13 -0500 Subject: [PATCH 11/15] method 2, 3, 6 done --- app/models/recipe.rb | 13 ++++++++++--- app/models/user.rb | 5 +++++ bin/run.rb | 9 +++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/models/recipe.rb b/app/models/recipe.rb index 1c49039c..6ac52ad2 100644 --- a/app/models/recipe.rb +++ b/app/models/recipe.rb @@ -11,10 +11,17 @@ def self.find_or_create_recipe(name, instructions) Recipe.all.find{|r| r.name == name} end - def self.suggest_random_recipe - # binding.pry - Recipe.all[rand(Recipe.all.size)] + def self.suggest_random_recipe(recipe_ids_already_made) + all_rec = Recipe.all + new_recipes = [] + all_rec.each do |r| + if !recipe_ids_already_made.any? {|e| e == r.id} + new_recipes.push(r) + end + end + new_recipes[rand(new_recipes.size - 1)] end + def self.list_my_ingredients(recipe) recipe.ingredients.each {|i| puts i.name} end diff --git a/app/models/user.rb b/app/models/user.rb index fd8b8776..1d1b8fc0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,4 +45,9 @@ def show_highest_ratings end end + def list_my_recipe_ids + user_rec_array = UserRecipe.all.select {|i| i.user_id == self.id} + user_rec_array.map{|r| r.recipe_id} + end + end \ No newline at end of file diff --git a/bin/run.rb b/bin/run.rb index 820bc3a8..aec37129 100755 --- a/bin/run.rb +++ b/bin/run.rb @@ -9,7 +9,9 @@ current_user = User.find_or_create_by_name(user_name) puts "Hello, #{user_name}," +sleep(1) puts "What would you like to do?" +sleep(1) puts "1. Find a random recipe by ingredient" #call suggest_random_recipe using find_recipe_by_ingredient method and then #picking a random array element in recipe.rb @@ -33,13 +35,16 @@ elsif choice == 3 #Right now it just picks a random recipe even if the user has made it before #Maybe change to only reccomend new recipes? - current_recipe = Recipe.suggest_random_recipe + # binding.pry + current_users_recipe_ids = current_user.list_my_recipe_ids + # binding.pry + current_recipe = Recipe.suggest_random_recipe(current_users_recipe_ids) puts "Time to make a(n) #{current_recipe.name}!" puts "Here are the ingredients:" Recipe.list_my_ingredients(current_recipe) puts current_recipe.instructions current_user.rate_recipe(current_recipe) - binding.pry + elsif choice == 6 puts "Enter the recipe name you are looking for" From 6b14b65e0dadb77d56614207eaa21ead455a6e4b Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 28 Jan 2021 12:54:00 -0500 Subject: [PATCH 12/15] method 5 works --- app/models/ingredient_recipe.rb | 22 ++++++++++++++++ bin/run.rb | 45 +++++++++++++++++---------------- 2 files changed, 45 insertions(+), 22 deletions(-) diff --git a/app/models/ingredient_recipe.rb b/app/models/ingredient_recipe.rb index a9699e5a..35234b39 100644 --- a/app/models/ingredient_recipe.rb +++ b/app/models/ingredient_recipe.rb @@ -9,4 +9,26 @@ def self.find_or_create_ingredient_recipe(recipe_id, ingredient_id) end IngredientRecipe.all.find {|ir| ir.recipe_id == recipe_id && ir.ingredient_id == ingredient_id} end + + def self.find_recipe_by_ingredient(ingredient) + + while !Ingredient.all.find {|i| i.name == ingredient} + puts "Ingredient not found. Please enter a valid ingredient" + ingredient = gets.chomp + end + + ingredient_id = Ingredient.all.find {|i| i.name == ingredient}.id + + ing_rec_instance = IngredientRecipe.all.select {|ir| ir.ingredient_id == ingredient_id} + rec_ids = ing_rec_instance.map{|i| i.recipe_id} + recs = Recipe.all.select {|r| rec_ids.any?(r.id)} + puts "The recipes containing #{ingredient} are:" + recs.each do |r| + puts r.name + end + end + + def self.random_recipe_from_ingredient(ingredient) + recipe_array = UserRecipe.find_recipe_by_ingredient(ingredient) + end \ No newline at end of file diff --git a/bin/run.rb b/bin/run.rb index aec37129..9938f4fc 100755 --- a/bin/run.rb +++ b/bin/run.rb @@ -29,28 +29,29 @@ puts "6. Import a recipe from the database" choice = gets.chomp choice = choice.to_i -if choice == 2 - current_user.show_highest_ratings - -elsif choice == 3 - #Right now it just picks a random recipe even if the user has made it before - #Maybe change to only reccomend new recipes? - # binding.pry - current_users_recipe_ids = current_user.list_my_recipe_ids - # binding.pry - current_recipe = Recipe.suggest_random_recipe(current_users_recipe_ids) - puts "Time to make a(n) #{current_recipe.name}!" - puts "Here are the ingredients:" - Recipe.list_my_ingredients(current_recipe) - puts current_recipe.instructions - current_user.rate_recipe(current_recipe) - - -elsif choice == 6 - puts "Enter the recipe name you are looking for" - name = gets.chomp - get_data(name) - puts "The recipe for #{name} has been sucessfully imported." +case choice + when 2 + current_user.show_highest_ratings + when 3 + current_users_recipe_ids = current_user.list_my_recipe_ids + # binding.pry + current_recipe = Recipe.suggest_random_recipe(current_users_recipe_ids) + puts "Time to make a(n) #{current_recipe.name}!" + puts "Here are the ingredients:" + Recipe.list_my_ingredients(current_recipe) + puts current_recipe.instructions + current_user.rate_recipe(current_recipe) + when 5 + puts "Enter the ingredient" + ingredient = gets.chomp + IngredientRecipe.find_recipe_by_ingredient(ingredient) + when 6 + puts "Enter the recipe name you are looking for" + name = gets.chomp + get_data(name) + puts "The recipe for #{name} has been sucessfully imported." + else + puts "That is not a valid choice." end From 6bd6187f8f378e0c775ef92c4ef4555ddf3162f6 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 28 Jan 2021 13:13:24 -0500 Subject: [PATCH 13/15] method 1 working --- app/models/ingredient_recipe.rb | 17 +++++++++++++---- bin/run.rb | 9 ++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/models/ingredient_recipe.rb b/app/models/ingredient_recipe.rb index 35234b39..023b6059 100644 --- a/app/models/ingredient_recipe.rb +++ b/app/models/ingredient_recipe.rb @@ -22,13 +22,22 @@ def self.find_recipe_by_ingredient(ingredient) ing_rec_instance = IngredientRecipe.all.select {|ir| ir.ingredient_id == ingredient_id} rec_ids = ing_rec_instance.map{|i| i.recipe_id} recs = Recipe.all.select {|r| rec_ids.any?(r.id)} - puts "The recipes containing #{ingredient} are:" - recs.each do |r| + end + + def self.print_recipes(recipe) + puts "The recipes containing that ingredient are:" + recipe.each do |r| puts r.name end end def self.random_recipe_from_ingredient(ingredient) - recipe_array = UserRecipe.find_recipe_by_ingredient(ingredient) - + recipe_array = IngredientRecipe.find_recipe_by_ingredient(ingredient) + suggested_recipe = recipe_array[rand(recipe_array.size - 1)] + puts "Time to make a(n) #{suggested_recipe.name}!" + puts "Here are the ingredients:" + Recipe.list_my_ingredients(suggested_recipe) + puts suggested_recipe.instructions + suggested_recipe + end end \ No newline at end of file diff --git a/bin/run.rb b/bin/run.rb index 9938f4fc..54d6dcbe 100755 --- a/bin/run.rb +++ b/bin/run.rb @@ -30,6 +30,12 @@ choice = gets.chomp choice = choice.to_i case choice + when 1 + puts "Enter the ingredient" + ingredient = gets.chomp + current_recipe = IngredientRecipe.random_recipe_from_ingredient(ingredient) + current_user.rate_recipe(current_recipe) + when 2 current_user.show_highest_ratings when 3 @@ -44,7 +50,8 @@ when 5 puts "Enter the ingredient" ingredient = gets.chomp - IngredientRecipe.find_recipe_by_ingredient(ingredient) + recipe = IngredientRecipe.find_recipe_by_ingredient(ingredient) + IngredientRecipe.print_recipes(recipe) when 6 puts "Enter the recipe name you are looking for" name = gets.chomp From 4e6e695d8ca643ab78b3263bf34215c2f8842f13 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 28 Jan 2021 14:23:37 -0500 Subject: [PATCH 14/15] just missing method 4 --- bin/run.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/run.rb b/bin/run.rb index 54d6dcbe..952f018b 100755 --- a/bin/run.rb +++ b/bin/run.rb @@ -1,5 +1,6 @@ require_relative '../config/environment' require_relative 'api_parsing' +ActiveRecord::Base.logger = nil From 93412f85da619b42b2f7e32d101a7631acf77e25 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 29 Jan 2021 10:59:56 -0500 Subject: [PATCH 15/15] final final also final --- app/models/user.rb | 1 + bin/run.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 08ad36f6..3c2f4701 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -73,6 +73,7 @@ def find_recipe_by_name(name) # end # end puts "Here is your #{name} recipe." + Recipe.list_my_ingredients(rec_name) puts rec_name.instructions self.rate_recipe(rec_name) end diff --git a/bin/run.rb b/bin/run.rb index 26dd4e3a..21ec6184 100755 --- a/bin/run.rb +++ b/bin/run.rb @@ -50,6 +50,7 @@ puts "Enter recipe name." name = gets.chomp current_user.find_recipe_by_name(name) + binding.pry when 5 puts "Enter the ingredient"