Skip to content

Commit

Permalink
add to routine from brands
Browse files Browse the repository at this point in the history
  • Loading branch information
PollyCR committed Nov 11, 2019
1 parent 638a553 commit ff6141a
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 15 deletions.
Binary file modified app/controllers/api/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions app/controllers/api/v1/active_ingredients_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Api::V1::ActiveIngredientsController < ApplicationController

def index
render json: ActiveIngredient.all
end
end
12 changes: 5 additions & 7 deletions app/controllers/api/v1/list_products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
class Api::V1::ListProductsController < ApplicationController
def create
# byebug
brand = Brand.find_or_create_by(name: params[:brand])
product = Product.find_or_create_by(name: params[:product_name], brand_id: brand.id, product_type: params[:product_type])
list = List.find_or_create_by(user_id: params[:id])
list_product = ListProduct.find_or_create_by(product_id: product.id, list_id: list.id)
# byebug
list = List.find_or_create_by(user_id: params[:user_id])
list_product = ListProduct.find_or_create_by(list_id: list.id, product_id: params[:product_id], purchased: false)
# # byebug
render json: list_product
end

def index
list_products = ListProduct.find_by(list_id: params[:list_id])
list_products = Product.find_by(id: params[:product_id])
render json: list_products
end

def destroy
# byebug
list_product = ListProduct.find_by(product_id: params[:id])
list_product = ListProduct.find(params[:id])
# byebug
list_product.destroy
# listProduct.destroy(params[:id])
Expand Down
1 change: 0 additions & 1 deletion app/controllers/api/v1/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class Api::V1::ProductsController < ApplicationController

def create

brand = Brand.find_or_create_by(params[:brand_name])
product = Product.create(params)
render json: product
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/routine_products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ def create
# byebug
brand = Brand.find_or_create_by(name: params[:brand])
product = Product.find_or_create_by(name: params[:product_name], brand_id: brand.id, product_type: params[:product_type])
active_ingredients = params[:active_ingredients].split(",").map{|ingredient|ActiveIngredient.find_or_create_by(product_id: product.id, name: ingredient).id}
active_ingredients = params[:active_ingredients] ? params[:active_ingredients].split(",").map{|ingredient|ActiveIngredient.find_or_create_by(product_id: product.id, name: ingredient).id} : nil
routine = Routine.find_or_create_by(routine_type: params[:routine], user_id: params[:id])
# byebug
routine_product = RoutineProduct.find_or_create_by(product_id: product.id, routine_id: routine.id)
# byebug
render json: routine_product
Expand Down
2 changes: 2 additions & 0 deletions app/models/active_ingredient.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class ActiveIngredient < ApplicationRecord
belongs_to :product


end
1 change: 1 addition & 0 deletions app/models/routine_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class RoutineProduct < ApplicationRecord
belongs_to :product
belongs_to :routine
has_many :comments
has_many :active_ingredients, through: :product
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class User < ApplicationRecord
has_many :lists
has_many :list_products, through: :lists


def morning_products
routine_products ? routine_products.select{|rp| rp.routine.routine_type == "am"}.map{|rp| rp.product} : null
end
Expand Down
5 changes: 5 additions & 0 deletions app/serializers/active_ingredients_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ActiveIngredientSerializer < ActiveModel::Serializer
attributes :id, :name
belongs_to :product
belongs_to :routine_product, through: :product
end
2 changes: 1 addition & 1 deletion app/serializers/list_product_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class ListProductSerializer < ActiveModel::Serializer
attributes :id, :list_id, :product_id
attributes :id, :list_id, :product_id, :purchased
end
3 changes: 2 additions & 1 deletion app/serializers/product_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :brand_id, :product_type
attributes :id, :name, :brand_id, :product_type, :active_ingredients
has_many :routine_products
has_many :active_ingredients
end
3 changes: 2 additions & 1 deletion app/serializers/routine_product_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class RoutineProductSerializer < ActiveModel::Serializer
attributes :id, :product_id, :routine_id
attributes :id, :product_id, :routine_id, :active_ingredients
belongs_to :product
has_many :comments
has_many :active_ingredients, through: :products
end
1 change: 1 addition & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class UserSerializer < ActiveModel::Serializer
has_many :entries, through: :diaries
has_many :lists
has_many :list_products, through: :lists

# has_many :comments, through: :routines


Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
resources :comments
resources :products
resources :entries
resources :active_ingredients

post "/signup", to: "users#create"
post '/login', to: 'authentication#create'
Expand Down
7 changes: 4 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
Diary.destroy_all
RoutineProduct.destroy_all
ListProduct.destroy_all
ActiveIngredient.destroy_all
Entry.destroy_all
Diary.destroy_all
Comment.destroy_all
Routine.destroy_all
Product.destroy_all
RoutineProduct.destroy_all
List.destroy_all
User.destroy_all
ActiveIngredient.destroy_all
Brand.destroy_all


Expand Down
7 changes: 7 additions & 0 deletions test/controllers/active_ingredients_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ActiveIngredientsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/controllers/api/v1/active_ingredients_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Api::V1::ActiveIngredientsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit ff6141a

Please sign in to comment.