Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VietTH-VenShop
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
4
Merge Requests
4
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tran Hoang Viet
VietTH-VenShop
Commits
91759019
Commit
91759019
authored
Jul 15, 2015
by
Tran Hoang Viet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
VietTH: Move add_t_cart and update_cart to CartService
parent
5db57ed9
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
89 additions
and
78 deletions
+89
-78
app/controllers/orders_controller.rb
+14
-4
app/controllers/products_controller.rb
+4
-3
app/models/product.rb
+1
-1
app/services/cart_service.rb
+54
-0
app/services/order_service.rb
+12
-68
app/views/categories/show.html.haml
+2
-0
config/initializers/kaminari_config.rb
+1
-1
config/settings.yml
+1
-1
No files found.
app/controllers/orders_controller.rb
View file @
91759019
...
@@ -5,12 +5,17 @@ class OrdersController < ApplicationController
...
@@ -5,12 +5,17 @@ class OrdersController < ApplicationController
end
end
def
create
def
create
result
=
order_service
.
create
(
cart_param
s
)
result
=
cart_service
.
update_cart
(
cart_params
[
'items'
].
value
s
)
if
result
[
:status
].
present?
if
result
[
:status
].
present?
redirect_to
(
root_path
,
notice:
'Checkout is successful.'
)
result
=
order_service
.
create_from_cart
(
current_user
.
cart
)
if
result
[
:status
].
present?
redirect_to
(
root_path
,
notice:
'Checkout is successful.'
)
else
flash
[
:alert
]
=
'Checkout is failed.'
render
:index
end
else
else
flash
[
:alert
]
=
result
[
:messages
]
redirect_to
(
orders_path
,
alert:
"Cart information isn't update. Error(
#{
result
[
:errors
]
}
)"
)
render
:index
end
end
end
end
...
@@ -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
app/controllers/products_controller.rb
View file @
91759019
...
@@ -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
||=
Order
Service
.
new
(
current_user
,
params
)
@
cart_service
||=
Cart
Service
.
new
(
current_user
,
params
)
end
end
end
end
\ No newline at end of file
app/models/product.rb
View file @
91759019
...
@@ -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
app/services/cart_service.rb
0 → 100644
View file @
91759019
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
app/services/order_service.rb
View file @
91759019
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
)
{
product_id:
item
.
id
,
quantity:
item
.
quantity
,
price:
item
.
unit_cost
}
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
end
def
create
(
cart_params
)
order
=
current_user
.
orders
.
build
(
result
=
update_cart_info
(
cart_params
[
'items'
].
values
)
order_items_attributes:
order_items_attributes
,
if
result
[
:status
].
present?
total:
cart
.
total
order_items_attributes
=
current_user
.
cart
.
items
.
map
do
|
item
|
)
{
product_id:
item
.
id
,
quantity:
item
.
quantity
,
price:
item
.
unit_cost
}
end
order
=
current_user
.
orders
.
build
(
if
order
.
save
order_items_attributes:
order_items_attributes
,
cart
.
destroy!
total:
current_user
.
cart
.
total
send_checkout_email
(
order
)
)
{
status:
true
}
if
order
.
save
current_user
.
cart
.
destroy!
send_checkout_email
(
order
)
{
status:
true
}
else
{
status:
false
}
end
else
else
{
status:
false
,
message:
result
[
:messages
].
join
(
','
)
}
{
status:
false
}
end
end
end
end
...
@@ -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
app/views/categories/show.html.haml
View file @
91759019
...
@@ -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
config/initializers/kaminari_config.rb
View file @
91759019
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
...
...
config/settings.yml
View file @
91759019
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment