Commit 389b2433 by Tran Hoang Viet

Merge branch 'vietth_feat_product_management' into vietth_feat_search

parents c4a3614e 4600465a
......@@ -22,6 +22,12 @@ label.error{
}
}
#message{
.alert{
margin-top: 20px;
}
}
#content{
.products-list-detail{
.product-item{
......
......@@ -8,38 +8,21 @@ class CartsController < ApplicationController
end
def create
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
if cart_service.create(cart_params)
clear_cart
self.send_checkout_email(cart)
redirect_to root_path
else
render :index
end
end
def send_checkout_email(cart)
CartMailer.send_checkout(current_user.email, cart).deliver_later
end
private
def cart_params
params.require(:cart).permit(:total, cart_items_attributes: [:product_id, :quantity])
end
def cart_service
@cart_service ||= CartService.new(current_user, params)
end
end
\ No newline at end of file
......@@ -8,6 +8,7 @@ class CategoriesController < ApplicationController
private
def set_category
@category = Category.find_by(id: params[:id])
redirect_to root_path, alert: "Category not found" if @category.blank?
end
end
\ No newline at end of file
......@@ -38,6 +38,7 @@ class ProductsController < ApplicationController
private
def set_product
@product = Product.find_by(id: params[:id])
redirect_to root_path, alert: "Product not found" if @product.blank?
end
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 @@
= render '/layouts/header'
#wrapper.container-fluid
= render '/layouts/message'
.row
.col-md-6.search-wrapper
......
......@@ -11,6 +11,7 @@
= render '/layouts/header'
#wrapper.container-fluid
= render '/layouts/message'
.row
.col-md-6.search-wrapper
......
......@@ -24,5 +24,7 @@ module VenShop
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :sidekiq
config.autoload_paths += Dir["#{config.root}/app/services/**/*.rb"]
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