Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shotengai
Project
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
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
shotengai
Commits
d1cea924
Commit
d1cea924
authored
Sep 12, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add order/:id/snapshots & cart/snapshots & Imprve base controller
parent
73f9c6f4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
126 additions
and
131 deletions
+126
-131
order_spec.rb
...generators/templates/spec/requests/customer/order_spec.rb
+41
-62
order_spec.rb
...generators/templates/spec/requests/merchant/order_spec.rb
+2
-2
index.json.jbuilder
...ators/templates/views/merchant/orders/index.json.jbuilder
+2
-1
show.json.jbuilder
...rators/templates/views/merchant/orders/show.json.jbuilder
+1
-1
_merchant_order.json.jbuilder
...ators/templates/views/share/_merchant_order.json.jbuilder
+6
-0
_merchant_order_simple.json.jbuilder
...emplates/views/share/_merchant_order_simple.json.jbuilder
+6
-0
_order.json.jbuilder
lib/generators/templates/views/share/_order.json.jbuilder
+3
-4
_order_simple.json.jbuilder
...erators/templates/views/share/_order_simple.json.jbuilder
+2
-2
base.rb
lib/shotengai/controllers/base.rb
+7
-7
carts_controller.rb
lib/shotengai/controllers/customer/carts_controller.rb
+2
-2
orders_controller.rb
lib/shotengai/controllers/customer/orders_controller.rb
+3
-2
product_series_controller.rb
...otengai/controllers/customer/product_series_controller.rb
+2
-2
product_snapshots_controller.rb
...ngai/controllers/customer/product_snapshots_controller.rb
+39
-36
products_controller.rb
lib/shotengai/controllers/customer/products_controller.rb
+2
-2
orders_controller.rb
lib/shotengai/controllers/merchant/orders_controller.rb
+2
-2
product_series_controller.rb
...otengai/controllers/merchant/product_series_controller.rb
+2
-2
product_snapshots_controller.rb
...ngai/controllers/merchant/product_snapshots_controller.rb
+2
-2
products_controller.rb
lib/shotengai/controllers/merchant/products_controller.rb
+2
-2
v1.json
swagger/v1.json
+0
-0
No files found.
lib/generators/templates/spec/requests/customer/order_spec.rb
View file @
d1cea924
...
...
@@ -91,6 +91,39 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
end
end
path
"/
#{
namespace
}
/orders/{id}/product_snapshots"
do
get
(
summary:
'某订单的 order 所有snapshots'
)
do
produces
'application/json'
consumes
'application/json'
parameter
:buyer_type
,
in: :query
,
type: :string
parameter
:buyer_id
,
in: :query
,
type: :integer
let
(
:buyer_id
)
{
@user
.
id
}
let
(
:buyer_type
)
{
@user
.
class
.
name
}
parameter
:id
,
in: :path
,
type: :integer
parameter
:page
,
in: :query
,
type: :string
parameter
:per_page
,
in: :query
,
type: :string
let
(
:page
)
{
1
}
let
(
:per_page
)
{
100
}
let
(
:id
)
{
@order_1
.
id
}
before
do
@orders
.
last
.
product_snapshots
.
create!
(
product_series:
@series_1
,
count:
100
)
end
response
(
200
,
description:
'successful'
)
do
it
{
body
=
JSON
.
parse
(
response
.
body
)
expect
(
body
[
'product_snapshots'
].
count
).
to
eq
(
@order_1
.
snapshots
.
count
)
}
end
end
end
path
"/
#{
namespace
}
/orders"
do
...
...
@@ -122,62 +155,6 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
end
end
post
(
summary:
'用户 创建订单(using incr_snapshot_ids'
)
do
produces
'application/json'
consumes
'application/json'
parameter
:buyer_type
,
in: :query
,
type: :string
parameter
:buyer_id
,
in: :query
,
type: :integer
let
(
:buyer_id
)
{
@user
.
id
}
let
(
:buyer_type
)
{
@user
.
class
.
name
}
parameter
:order
,
in: :body
,
schema:
{
type: :object
,
properties:
{
order:
{
type: :object
,
properties:
{
address:
{
type: :string
},
user_remark:
{
type: :text
},
incr_snapshot_ids:
{
type: :array
},
# gone_snapshot_ids: { type: :array },
}
}
}
}
response
(
201
,
description:
'successful'
)
do
let
(
:order
)
{{
order:
{
address:
'This is an special address.'
,
user_remark:
'user remark ...'
,
incr_snapshot_ids:
[
@snapshot_2
.
id
],
}
}}
it
{
body
=
JSON
.
parse
(
response
.
body
)
expect
(
body
[
'snapshots'
].
count
).
to
eq
(
1
)
expect
(
body
[
'snapshots'
].
first
[
'id'
]).
to
eq
(
@snapshot_2
.
id
)
expect
(
@snapshot_2
.
reload
.
is_in_cart
).
to
eq
(
false
)
}
end
response
(
400
,
description:
'failed, Cannot edit a snapshot which order was already paid.'
)
do
let
(
:order
)
{{
order:
{
incr_snapshot_ids:
[
@snapshot_1
.
id
],
}
}}
before
{
@order_1
.
pay!
}
it
{
expect
(
response
.
status
).
to
eq
(
400
)
}
end
end
end
path
"/
#{
namespace
}
/orders"
do
post
(
summary:
'create order'
)
do
produces
'application/json'
consumes
'application/json'
...
...
@@ -192,6 +169,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
type: :object
,
properties:
{
address:
{
type: :string
},
user_remark:
{
type: :text
},
incr_snapshot_ids:
{
type: :array
},
},
snapshots:
{
type: :object
,
properties:
{
...
...
@@ -208,7 +186,8 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
{
order:
{
address:
'This is an special address.'
,
user_remark:
'user remark ...'
user_remark:
'user remark ...'
,
incr_snapshot_ids:
[
@snapshot_2
.
id
],
},
snapshots:
{
shotengai_series_id:
@series_1
.
id
,
...
...
@@ -218,15 +197,15 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
}
it
{
body
=
JSON
.
parse
(
response
.
body
)
expect
(
body
[
'snapshots'
].
count
).
to
eq
(
1
)
expect
(
body
[
'snapshots'
].
first
[
'shotengai_series_id'
]).
to
eq
(
@series_1
.
id
)
expect
(
Order
.
last
.
snapshots
.
count
).
to
eq
(
2
)
expect
(
Order
.
last
.
snapshots
.
map
(
&
:shotengai_series_id
).
sort
).
to
eq
([
@series_1
.
id
,
@snapshot_2
.
series
.
id
].
sort
)
expect
(
Order
.
last
.
snapshots
.
map
(
&
:id
).
sort
.
include?
(
@snapshot_2
.
id
)).
to
eq
(
true
)
expect
(
@snapshot_2
.
reload
.
is_in_cart
).
to
eq
(
false
)
}
end
end
end
path
"/
#{
namespace
}
/orders/{id}"
do
parameter
:id
,
in: :path
,
type: :integer
...
...
@@ -242,7 +221,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
response
(
200
,
description:
'successful'
)
do
it
{
expect
(
JSON
.
parse
(
response
.
body
)[
'snapshots'
].
count
).
to
eq
(
@order_1
.
snapshots
.
count
)
#
expect(JSON.parse(response.body)['snapshots'].count).to eq(@order_1.snapshots.count)
}
end
end
...
...
lib/generators/templates/spec/requests/merchant/order_spec.rb
View file @
d1cea924
...
...
@@ -27,7 +27,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
@orders
=
create_list
(
:order
,
3
)
@order_1
=
@orders
.
first
@cart
=
Order
::
Cart
.
create!
@cart
=
create
(
:user
).
order_cart
@snapshot_1
.
update!
(
order:
@order_1
)
@snapshot_other
.
update!
(
order:
@order_1
)
...
...
@@ -70,7 +70,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
consumes
'application/json'
response
(
200
,
description:
'successful'
)
do
it
{
expect
(
JSON
.
parse
(
response
.
body
)[
'snapshots'
].
count
).
to
eq
(
@order_1
.
snapshots
.
count
)
#
expect(JSON.parse(response.body)['snapshots'].count).to eq(@order_1.snapshots.count)
}
end
end
...
...
lib/generators/templates/views/merchant/orders/index.json.jbuilder
View file @
d1cea924
json.current_page @resources.current_page
json.total_pages @resources.total_pages
json.set! @resources.klass.model_name.collection do
json.array! @resources, partial: 'shotengai/share/order_simple', as: :order
json.array! @resources, partial: 'shotengai/share/
merchant_
order_simple', as: :order
end
\ No newline at end of file
lib/generators/templates/views/merchant/orders/show.json.jbuilder
View file @
d1cea924
json.partial! "shotengai/share/order", order: @resource
json.partial! "shotengai/share/
merchant_
order", order: @resource
lib/generators/templates/views/share/_merchant_order.json.jbuilder
0 → 100644
View file @
d1cea924
json.extract! order, :id, :seq, :address,
:pay_time, :delivery_time, :receipt_time,
:delivery_way, :delivery_cost,
:merchant_remark, :mark, :customer_remark,
:status, :status_zh, :meta
# json.snapshots order.snapshots, partial: 'shotengai/share/snapshot_simple', as: :snapshot
lib/generators/templates/views/share/_merchant_order_simple.json.jbuilder
0 → 100644
View file @
d1cea924
json.extract! order, :id, :seq, :address,
:pay_time, :delivery_time, :receipt_time,
:delivery_way, :delivery_cost,
:merchant_remark, :mark, :customer_remark,
:status, :status_zh, :meta
\ No newline at end of file
lib/generators/templates/views/share/_order.json.jbuilder
View file @
d1cea924
json.extract! order, :id, :seq, :status_zh, :address,
:pay_time, :delivery_time, :receipt_time,
:delivery_way, :delivery_cost,
:merchant_remark, :mark, :customer_remark,
:delivery_way, :delivery_cost, :customer_remark,
:status, :status_zh, :meta
json.snapshots order.snapshots, partial: 'shotengai/share/snapshot_simple', as: :snapshot
\ No newline at end of file
# json.snapshots order.snapshots, partial: 'shotengai/share/snapshot_simple', as: :snapshot
\ No newline at end of file
lib/generators/templates/views/share/_order_simple.json.jbuilder
View file @
d1cea924
json.extract! order, :id, :seq, :address,
:pay_time, :delivery_time, :receipt_time,
:delivery_way, :delivery_cost,
:merchant_remark, :mark, :customer_remark,
:delivery_way, :delivery_cost, :customer_remark,
:status, :status_zh, :meta
\ No newline at end of file
lib/shotengai/controllers/base.rb
View file @
d1cea924
...
...
@@ -11,9 +11,9 @@ module Shotengai
# respond_with @products, template: "#{self.class.template_dir}/index"
#
cattr_accessor
:template_dir
@@index_query
||
=
nil
@@default_query
||
=
nil
@@index_query
=
nil
@@default_query
=
nil
class
<<
self
# Add the index query to custom the @@index_resouces on the base of @@resources
...
...
@@ -100,14 +100,14 @@ module Shotengai
@resource
.
destroy!
head
204
end
private
def
index_resources
@@index_query
&
.
call
(
default_resources
,
params
)
||
default_resources
@@index_query
&
.
call
(
default_resources
,
params
,
request
)
||
default_resources
end
def
default_resources
@@default_query
&
.
call
(
self
.
class
.
resources
,
params
)
||
self
.
class
.
resources
@@default_query
&
.
call
(
self
.
class
.
resources
,
params
,
request
)
||
self
.
class
.
resources
end
def
resource_key
...
...
@@ -119,7 +119,7 @@ module Shotengai
end
def
resource_params
params
.
requ
ri
e
(
resource_key
).
permit!
.
merge
other_resource_params
params
.
requ
ir
e
(
resource_key
).
permit!
.
merge
other_resource_params
end
# rewrite this to add more custom column
...
...
lib/shotengai/controllers/customer/carts_controller.rb
View file @
d1cea924
...
...
@@ -11,10 +11,10 @@ module Shotengai
remove_actions
:index
,
:create
,
:destroy
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
end
private
...
...
lib/shotengai/controllers/customer/orders_controller.rb
View file @
d1cea924
...
...
@@ -10,10 +10,11 @@ module Shotengai
remove_actions
:destroy
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
request
.
params
resource
.
status_is
(
params
[
:status
])
end
...
...
lib/shotengai/controllers/customer/product_series_controller.rb
View file @
d1cea924
...
...
@@ -6,13 +6,13 @@ module Shotengai
self
.
template_dir
=
'shotengai/customer/series/'
remove_actions
:create
,
:update
,
:destroy
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
resource
.
where
(
params
[
:product_id
]
&&
{
shotengai_product_id:
params
[
:product_id
]
}
)
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
end
end
end
...
...
lib/shotengai/controllers/customer/product_snapshots_controller.rb
View file @
d1cea924
...
...
@@ -2,48 +2,51 @@ module Shotengai
module
Controller
module
Customer
class
ProductSnapshotsController
<
Shotengai
::
Controller
::
Base
#
self.resources = ProductSnapshot
#
self.template_dir = 'shotengai/customer/snapshots/'
self
.
resources
=
ProductSnapshot
self
.
template_dir
=
'shotengai/customer/snapshots/'
# before_action :buyer_auth
# before_action :edit_only_unpaid, except: [:index, :show, :create]
# default_query do |resource, params|
# # /orders/:order_id/snapshots
# # /series/:series_id/snapshots
# p 'come here'
# resource.where(
# params[:order_id] && { shotengai_order_id: params[:order_id] }
# ).where(
# params[:series_id] && { shotengai_series_id: params[:series_id] }
# )
# end
before_action
:buyer_auth
before_action
:edit_only_unpaid
,
except:
[
:index
,
:show
,
:create
]
self
.
default_query
do
|
resource
,
params
,
request
|
# /orders/:order_id/snapshots
# /series/:series_id/snapshots
buyer
=
params
[
:buyer_type
].
constantize
.
find
(
params
[
:buyer_id
])
order_id
=
request
.
path_info
.
include?
(
'cart'
)
?
buyer
.
order_cart
.
id
:
params
[
:order_id
]
resource
.
where
(
order_id
&&
{
shotengai_order_id:
order_id
}
).
count
resource
.
where
(
order_id
&&
{
shotengai_order_id:
order_id
}
).
where
(
params
[
:series_id
]
&&
{
shotengai_series_id:
params
[
:series_id
]
}
)
end
# index_query do |resource, params|
# params[:in_cart] ? resource.in_cart : resource.in_order
# end
index_query
do
|
resource
,
params
,
request
|
end
#
#
不指定 order 时,默认创建在 cart 中
#
#
TODO: WARNING: snapshots
#
def create
#
order_or_cart = Shotengai::Order.find_by_id(params[:order_id]) || @buyer.order_cart
#
@resource = order_or_cart.product_snapshots.create!(resource_params)
#
respond_with @resource, template: "#{@@template_dir}/show", status: 201
#
end
# 不指定 order 时,默认创建在 cart 中
# TODO: WARNING: snapshots
def
create
order_or_cart
=
Shotengai
::
Order
.
find_by_id
(
params
[
:order_id
])
||
@buyer
.
order_cart
@resource
=
order_or_cart
.
product_snapshots
.
create!
(
resource_params
)
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
,
status:
201
end
#
private
#
def buyer_auth
#
@buyer = params[:buyer_type].constantize.find(params[:buyer_id])
#
end
private
def
buyer_auth
@buyer
=
params
[
:buyer_type
].
constantize
.
find
(
params
[
:buyer_id
])
end
#
def resource_params
#
params.require(resource_key).permit(
#
:count, :shotengai_series_id
#
)
#
end
def
resource_params
params
.
require
(
resource_key
).
permit
(
:count
,
:shotengai_series_id
)
end
#
def edit_only_unpaid
#
raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.order.unpaid?
#
end
def
edit_only_unpaid
raise
Shotengai
::
WebError
.
new
(
'订单已支付,不可修改。'
,
'-1'
,
403
)
unless
@resource
.
order
.
unpaid?
end
end
end
end
...
...
lib/shotengai/controllers/customer/products_controller.rb
View file @
d1cea924
...
...
@@ -6,11 +6,11 @@ module Shotengai
self
.
template_dir
=
'shotengai/customer/products/'
remove_actions
:create
,
:update
,
:destroy
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
params
[
:catalog_list
]
?
resource
.
tagged_with
(
params
[:
catalog_list
],
on: :catalogs
)
:
resource
...
...
lib/shotengai/controllers/merchant/orders_controller.rb
View file @
d1cea924
...
...
@@ -7,11 +7,11 @@ module Shotengai
remove_actions
:create
,
:destroy
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
resource
.
status_is
(
params
[
:status
])
end
...
...
lib/shotengai/controllers/merchant/product_series_controller.rb
View file @
d1cea924
...
...
@@ -5,13 +5,13 @@ module Shotengai
self
.
resources
=
ProductSeries
self
.
template_dir
=
'shotengai/merchant/series/'
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
resource
.
where
(
params
[
:product_id
]
&&
{
shotengai_product_id:
params
[
:product_id
]
}
)
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
end
...
...
lib/shotengai/controllers/merchant/product_snapshots_controller.rb
View file @
d1cea924
...
...
@@ -8,11 +8,11 @@ module Shotengai
remove_actions
:create
,
:destroy
before_action
:edit_only_unpaid
,
only: :update
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
resource
.
in_order
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
resource
.
where
(
params
[
:order_id
]
&&
{
shotengai_order_id:
params
[
:order_id
]
}
).
where
(
...
...
lib/shotengai/controllers/merchant/products_controller.rb
View file @
d1cea924
...
...
@@ -7,11 +7,11 @@ module Shotengai
before_action
:manager_auth
default_query
do
|
resource
,
params
|
default_query
do
|
resource
,
params
,
request
|
resource
.
where
(
manager:
@manager
)
end
index_query
do
|
resource
,
params
|
index_query
do
|
resource
,
params
,
request
|
params
[
:catalog_list
]
?
resource
.
tagged_with
(
params
[:
catalog_list
],
on: :catalogs
)
:
resource
...
...
swagger/v1.json
View file @
d1cea924
This diff is collapsed.
Click to expand it.
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