Commit 4600465a by Tran Hoang Viet

VietTH: Fix reviewcode and feedback

parent d570482e
...@@ -22,6 +22,12 @@ label.error{ ...@@ -22,6 +22,12 @@ label.error{
} }
} }
#message{
.alert{
margin-top: 20px;
}
}
#content{ #content{
.products-list-detail{ .products-list-detail{
.product-item{ .product-item{
......
...@@ -8,38 +8,21 @@ class CartsController < ApplicationController ...@@ -8,38 +8,21 @@ class CartsController < ApplicationController
end end
def create def create
cart = current_user.carts.build(cart_params) if cart_service.create(cart_params)
product_ids = []
quantites = {}
cart_params['cart_items_attributes'].each do |item|
id = item.last['product_id']
product_ids << id
quantites[id] = item.last['quantity']
end
products = Product.where(id: [product_ids]).select(:id, :price)
total = products.inject(0) do |sum, product|
sum + (product.price.to_i * quantites[product.id.to_s].to_i)
end
cart.total = total
if cart.save
clear_cart clear_cart
self.send_checkout_email(cart)
redirect_to root_path redirect_to root_path
else else
render :index render :index
end end
end end
def send_checkout_email(cart)
CartMailer.send_checkout(current_user.email, cart).deliver_later
end
private private
def cart_params def cart_params
params.require(:cart).permit(:total, cart_items_attributes: [:product_id, :quantity]) params.require(:cart).permit(:total, cart_items_attributes: [:product_id, :quantity])
end end
def cart_service
@cart_service ||= CartService.new(current_user, params)
end
end end
\ No newline at end of file
...@@ -8,6 +8,7 @@ class CategoriesController < ApplicationController ...@@ -8,6 +8,7 @@ class CategoriesController < ApplicationController
private private
def set_category def set_category
@category = Category.find_by(id: params[:id]) @category = Category.find_by(id: params[:id])
redirect_to root_path, alert: "Category not found" if @category.blank?
end end
end end
\ No newline at end of file
...@@ -33,6 +33,7 @@ class ProductsController < ApplicationController ...@@ -33,6 +33,7 @@ class ProductsController < ApplicationController
private private
def set_product def set_product
@product = Product.find_by(id: params[:id]) @product = Product.find_by(id: params[:id])
redirect_to root_path, alert: "Product not found" if @product.blank?
end end
def set_categories def set_categories
......
class BaseService
attr_accessor :current_user, :params
def initialize user = nil, params = {}
@current_user = user
@params = params
end
end
\ No newline at end of file
class CartService < BaseService
def create(cart_params)
cart = current_user.carts.build(cart_params)
product_ids = []
quantites = {}
cart_params['cart_items_attributes'].each do |item|
id = item.last['product_id']
product_ids << id
quantites[id] = item.last['quantity']
end
products = Product.where(id: [product_ids]).select(:id, :price)
total = products.inject(0) do |sum, product|
sum + (product.price.to_i * quantites[product.id.to_s].to_i)
end
cart.total = total
if cart.save
send_checkout_email(cart)
{status: true}
else
{status: false}
end
end
private
def send_checkout_email(cart)
CartMailer.send_checkout(current_user.email, cart).deliver_later
end
end
\ No newline at end of file
#message
- if notice
.alert.alert-success
= notice
- if alert
.alert.alert-danger
= alert
\ No newline at end of file
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
= render '/layouts/header' = render '/layouts/header'
#wrapper.container-fluid #wrapper.container-fluid
= render '/layouts/message'
.row .row
.col-md-6.search-wrapper .col-md-6.search-wrapper
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
= render '/layouts/header' = render '/layouts/header'
#wrapper.container-fluid #wrapper.container-fluid
= render '/layouts/message'
.row .row
.col-md-6.search-wrapper .col-md-6.search-wrapper
......
...@@ -24,5 +24,7 @@ module VenShop ...@@ -24,5 +24,7 @@ module VenShop
config.active_record.raise_in_transactional_callbacks = true config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :sidekiq config.active_job.queue_adapter = :sidekiq
config.autoload_paths += Dir["#{config.root}/app/services/**/*.rb"]
end end
end end
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