Commit 91759019 by Tran Hoang Viet

VietTH: Move add_t_cart and update_cart to CartService

parent 5db57ed9
...@@ -5,13 +5,18 @@ class OrdersController < ApplicationController ...@@ -5,13 +5,18 @@ class OrdersController < ApplicationController
end end
def create def create
result = order_service.create(cart_params) result = cart_service.update_cart(cart_params['items'].values)
if result[:status].present?
result = order_service.create_from_cart(current_user.cart)
if result[:status].present? if result[:status].present?
redirect_to(root_path, notice: 'Checkout is successful.') redirect_to(root_path, notice: 'Checkout is successful.')
else else
flash[:alert] = result[:messages] flash[:alert] = 'Checkout is failed.'
render :index render :index
end end
else
redirect_to(orders_path, alert: "Cart information isn't update. Error(#{result[:errors]})")
end
end end
private private
...@@ -23,4 +28,8 @@ class OrdersController < ApplicationController ...@@ -23,4 +28,8 @@ class OrdersController < ApplicationController
@order_service ||= OrderService.new(current_user, params) @order_service ||= OrderService.new(current_user, params)
end end
def cart_service
@cart_service ||= CartService.new(current_user, params)
end
end end
\ No newline at end of file
...@@ -24,7 +24,7 @@ class ProductsController < ApplicationController ...@@ -24,7 +24,7 @@ class ProductsController < ApplicationController
end end
def add_cart def add_cart
result = order_service.add_to_cart(@product) result = cart_service.add_to_cart(@product)
if result[:status].present? if result[:status].present?
redirect_to :back redirect_to :back
else else
...@@ -51,8 +51,8 @@ class ProductsController < ApplicationController ...@@ -51,8 +51,8 @@ class ProductsController < ApplicationController
params.require(:product).permit(:title, :price, :category_id, :image) params.require(:product).permit(:title, :price, :category_id, :image)
end end
def order_service def cart_service
@order_service ||= OrderService.new(current_user, params) @cart_service ||= CartService.new(current_user, params)
end end
end end
\ No newline at end of file
...@@ -17,5 +17,5 @@ class Product < ActiveRecord::Base ...@@ -17,5 +17,5 @@ class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader mount_uploader :image, ImageUploader
solr_options per_page: 5, fields: [:id, :title] solr_options per_page: Settings.limit_category, fields: [:id, :title]
end end
class CartService < BaseService
def add_to_cart(product)
quantity = if current_user.cart.contains?(product)
cart_item = current_user.cart.find(product)
cart_item.destroy
params[:quantity].to_i + cart_item.quantity.to_i
else
params[:quantity].to_i
end
if quantity <= product.stock
current_user.cart.add_item(
id: product.id,
name: product.title,
unit_cost: product.price,
cost: (product.price || 0) * quantity,
quantity: quantity,
type: Product,
stock: product.stock
)
{status: true}
else
{status: false}
end
end
def update_cart(items)
products = Product.where(items.map { |item| item['id'] })
products = products.inject({}) do |list, product|
list[product.id.to_s] = product
list
end
errors = []
items.each do |item|
product = products[item['id']]
if item['quantity'].to_i > product.stock
errors.push(product.id)
else
cart_item = current_user.cart.find(product)
cart_item.quantity = item['quantity']
end
end
if errors.present?
{status: false, errors: errors}
else
{status: true}
end
end
end
\ No newline at end of file
class OrderService < BaseService class OrderService < BaseService
def add_to_cart product def create_from_cart(cart)
quantity = if current_user.cart.contains?(product) order_items_attributes = cart.items.map do |item|
cart_item = current_user.cart.find(product)
cart_item.destroy
params[:quantity].to_i + cart_item.quantity.to_i
else
params[:quantity].to_i
end
if quantity <= product.stock
current_user.cart.add_item(
id: product.id,
name: product.title,
unit_cost: product.price,
cost: (product.price || 0) * quantity,
quantity: quantity,
type: Product,
stock: product.stock
)
{status: true}
else
{status: false, message: "Quanity must have less than #{product.stock}"}
end
end
def create(cart_params)
result = update_cart_info(cart_params['items'].values)
if result[:status].present?
order_items_attributes = current_user.cart.items.map do |item|
{product_id: item.id, quantity: item.quantity, price: item.unit_cost} {product_id: item.id, quantity: item.quantity, price: item.unit_cost}
end end
order = current_user.orders.build( order = current_user.orders.build(
order_items_attributes: order_items_attributes, order_items_attributes: order_items_attributes,
total: current_user.cart.total total: cart.total
) )
if order.save if order.save
current_user.cart.destroy! cart.destroy!
send_checkout_email(order) send_checkout_email(order)
{status: true} {status: true}
else else
{status: false} {status: false}
end end
else
{status: false, message: result[:messages].join(',')}
end
end end
private private
...@@ -54,29 +24,4 @@ class OrderService < BaseService ...@@ -54,29 +24,4 @@ class OrderService < BaseService
OrderMailer.send_checkout(current_user.email, order).deliver_later OrderMailer.send_checkout(current_user.email, order).deliver_later
end end
def update_cart_info(items)
products = Product.where(items.map { |item| item['id'] })
products = products.inject({}) do |list, product|
list[product.id.to_s] = product
list
end
errors = []
items.each do |item|
product = products[item['id']]
if item['quantity'].to_i > product.stock
errors.push(product.id)
else
cart_item = current_user.cart.find(product)
cart_item.quantity = item['quantity']
end
end
if errors.present?
{status: false, messages: errors}
else
{status: true}
end
end
end end
\ No newline at end of file
...@@ -3,5 +3,6 @@ ...@@ -3,5 +3,6 @@
.module .module
#category-items.products-list-detail #category-items.products-list-detail
= render @products = render @products
.clearfix
.pagination .pagination
= paginate @products = paginate @products
\ No newline at end of file
Kaminari.configure do |config| Kaminari.configure do |config|
config.default_per_page = 5 config.default_per_page = 6
# config.max_per_page = nil # config.max_per_page = nil
# config.window = 4 # config.window = 4
# config.outer_window = 0 # config.outer_window = 0
......
defaults: &defaults defaults: &defaults
limit_category: 5 limit_category: 6
limit_product_recommended: 6 limit_product_recommended: 6
limit_product_newest: 9 limit_product_newest: 9
limit_length_category_title: 50 limit_length_category_title: 50
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment