Commit d1cea924 by ivan Lan

Add order/:id/snapshots & cart/snapshots & Imprve base controller

parent 73f9c6f4
...@@ -91,6 +91,39 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -91,6 +91,39 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
end end
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 path "/#{namespace}/orders" do
...@@ -122,62 +155,6 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -122,62 +155,6 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
end end
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 post(summary: 'create order') do
produces 'application/json' produces 'application/json'
consumes 'application/json' consumes 'application/json'
...@@ -192,6 +169,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -192,6 +169,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
type: :object, properties: { type: :object, properties: {
address: { type: :string }, address: { type: :string },
user_remark: { type: :text }, user_remark: { type: :text },
incr_snapshot_ids: { type: :array },
}, },
snapshots: { snapshots: {
type: :object, properties: { type: :object, properties: {
...@@ -208,7 +186,8 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -208,7 +186,8 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
{ {
order: { order: {
address: 'This is an special address.', address: 'This is an special address.',
user_remark: 'user remark ...' user_remark: 'user remark ...',
incr_snapshot_ids: [ @snapshot_2.id ],
}, },
snapshots: { snapshots: {
shotengai_series_id: @series_1.id, shotengai_series_id: @series_1.id,
...@@ -218,15 +197,15 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -218,15 +197,15 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
} }
it { it {
body = JSON.parse(response.body) expect(Order.last.snapshots.count).to eq(2)
expect(body['snapshots'].count).to eq(1) expect(Order.last.snapshots.map(&:shotengai_series_id).sort).to eq([@series_1.id, @snapshot_2.series.id].sort)
expect(body['snapshots'].first['shotengai_series_id']).to eq(@series_1.id) 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 end
end end
path "/#{namespace}/orders/{id}" do path "/#{namespace}/orders/{id}" do
parameter :id, in: :path, type: :integer parameter :id, in: :path, type: :integer
...@@ -242,7 +221,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -242,7 +221,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
response(200, description: 'successful') do response(200, description: 'successful') do
it { 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
end end
......
...@@ -27,7 +27,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -27,7 +27,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
@orders = create_list(:order, 3) @orders = create_list(:order, 3)
@order_1 = @orders.first @order_1 = @orders.first
@cart = Order::Cart.create! @cart = create(:user).order_cart
@snapshot_1.update!(order: @order_1) @snapshot_1.update!(order: @order_1)
@snapshot_other.update!(order: @order_1) @snapshot_other.update!(order: @order_1)
...@@ -70,7 +70,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta ...@@ -70,7 +70,7 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
consumes 'application/json' consumes 'application/json'
response(200, description: 'successful') do response(200, description: 'successful') do
it { 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
end end
......
json.current_page @resources.current_page json.current_page @resources.current_page
json.total_pages @resources.total_pages json.total_pages @resources.total_pages
json.set! @resources.klass.model_name.collection do 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 end
\ No newline at end of file
json.partial! "shotengai/share/order", order: @resource json.partial! "shotengai/share/merchant_order", order: @resource
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
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
json.extract! order, :id, :seq, :status_zh, :address, json.extract! order, :id, :seq, :status_zh, :address,
:pay_time, :delivery_time, :receipt_time, :pay_time, :delivery_time, :receipt_time,
:delivery_way, :delivery_cost, :delivery_way, :delivery_cost, :customer_remark,
:merchant_remark, :mark, :customer_remark,
:status, :status_zh, :meta :status, :status_zh, :meta
json.snapshots order.snapshots, partial: 'shotengai/share/snapshot_simple', as: :snapshot # json.snapshots order.snapshots, partial: 'shotengai/share/snapshot_simple', as: :snapshot
\ No newline at end of file \ No newline at end of file
json.extract! order, :id, :seq, :address, json.extract! order, :id, :seq, :address,
:pay_time, :delivery_time, :receipt_time, :pay_time, :delivery_time, :receipt_time,
:delivery_way, :delivery_cost, :delivery_way, :delivery_cost, :customer_remark,
:merchant_remark, :mark, :customer_remark,
:status, :status_zh, :meta :status, :status_zh, :meta
\ No newline at end of file
...@@ -11,9 +11,9 @@ module Shotengai ...@@ -11,9 +11,9 @@ module Shotengai
# respond_with @products, template: "#{self.class.template_dir}/index" # respond_with @products, template: "#{self.class.template_dir}/index"
# #
cattr_accessor :template_dir cattr_accessor :template_dir
@@index_query ||= nil @@index_query = nil
@@default_query ||= nil @@default_query = nil
class << self class << self
# Add the index query to custom the @@index_resouces on the base of @@resources # Add the index query to custom the @@index_resouces on the base of @@resources
...@@ -100,14 +100,14 @@ module Shotengai ...@@ -100,14 +100,14 @@ module Shotengai
@resource.destroy! @resource.destroy!
head 204 head 204
end end
private private
def index_resources def index_resources
@@index_query&.call(default_resources, params) || default_resources @@index_query&.call(default_resources, params, request) || default_resources
end end
def default_resources 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 end
def resource_key def resource_key
...@@ -119,7 +119,7 @@ module Shotengai ...@@ -119,7 +119,7 @@ module Shotengai
end end
def resource_params def resource_params
params.requrie(resource_key).permit!.merge other_resource_params params.require(resource_key).permit!.merge other_resource_params
end end
# rewrite this to add more custom column # rewrite this to add more custom column
......
...@@ -11,10 +11,10 @@ module Shotengai ...@@ -11,10 +11,10 @@ module Shotengai
remove_actions :index, :create, :destroy remove_actions :index, :create, :destroy
default_query do |resource, params| default_query do |resource, params, request|
end end
index_query do |resource, params| index_query do |resource, params, request|
end end
private private
......
...@@ -10,10 +10,11 @@ module Shotengai ...@@ -10,10 +10,11 @@ module Shotengai
remove_actions :destroy remove_actions :destroy
default_query do |resource, params| default_query do |resource, params, request|
end end
index_query do |resource, params| index_query do |resource, params, request|
request.params
resource.status_is(params[:status]) resource.status_is(params[:status])
end end
......
...@@ -6,13 +6,13 @@ module Shotengai ...@@ -6,13 +6,13 @@ module Shotengai
self.template_dir = 'shotengai/customer/series/' self.template_dir = 'shotengai/customer/series/'
remove_actions :create, :update, :destroy remove_actions :create, :update, :destroy
default_query do |resource, params| default_query do |resource, params, request|
resource.where( resource.where(
params[:product_id] && { shotengai_product_id: params[:product_id] } params[:product_id] && { shotengai_product_id: params[:product_id] }
) )
end end
index_query do |resource, params| index_query do |resource, params, request|
end end
end end
end end
......
...@@ -2,48 +2,51 @@ module Shotengai ...@@ -2,48 +2,51 @@ module Shotengai
module Controller module Controller
module Customer module Customer
class ProductSnapshotsController < Shotengai::Controller::Base class ProductSnapshotsController < Shotengai::Controller::Base
# self.resources = ProductSnapshot self.resources = ProductSnapshot
# self.template_dir = 'shotengai/customer/snapshots/' self.template_dir = 'shotengai/customer/snapshots/'
# before_action :buyer_auth before_action :buyer_auth
# before_action :edit_only_unpaid, except: [:index, :show, :create] before_action :edit_only_unpaid, except: [:index, :show, :create]
# default_query do |resource, params| self.default_query do |resource, params, request|
# # /orders/:order_id/snapshots # /orders/:order_id/snapshots
# # /series/:series_id/snapshots # /series/:series_id/snapshots
# p 'come here' buyer = params[:buyer_type].constantize.find(params[:buyer_id])
# resource.where( order_id = request.path_info.include?('cart') ? buyer.order_cart.id : params[:order_id]
# params[:order_id] && { shotengai_order_id: params[:order_id] } resource.where(
# ).where( order_id && { shotengai_order_id: order_id }
# params[:series_id] && { shotengai_series_id: params[:series_id] } ).count
# ) resource.where(
# end order_id && { shotengai_order_id: order_id }
).where(
params[:series_id] && { shotengai_series_id: params[:series_id] }
)
end
# index_query do |resource, params| index_query do |resource, params, request|
# params[:in_cart] ? resource.in_cart : resource.in_order end
# end
# # 不指定 order 时,默认创建在 cart 中 # 不指定 order 时,默认创建在 cart 中
# # TODO: WARNING: snapshots # TODO: WARNING: snapshots
# def create def create
# order_or_cart = Shotengai::Order.find_by_id(params[:order_id]) || @buyer.order_cart order_or_cart = Shotengai::Order.find_by_id(params[:order_id]) || @buyer.order_cart
# @resource = order_or_cart.product_snapshots.create!(resource_params) @resource = order_or_cart.product_snapshots.create!(resource_params)
# respond_with @resource, template: "#{@@template_dir}/show", status: 201 respond_with @resource, template: "#{@@template_dir}/show", status: 201
# end end
# private private
# def buyer_auth def buyer_auth
# @buyer = params[:buyer_type].constantize.find(params[:buyer_id]) @buyer = params[:buyer_type].constantize.find(params[:buyer_id])
# end end
# def resource_params def resource_params
# params.require(resource_key).permit( params.require(resource_key).permit(
# :count, :shotengai_series_id :count, :shotengai_series_id
# ) )
# end end
# def edit_only_unpaid def edit_only_unpaid
# raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.order.unpaid? raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.order.unpaid?
# end end
end end
end end
end end
......
...@@ -6,11 +6,11 @@ module Shotengai ...@@ -6,11 +6,11 @@ module Shotengai
self.template_dir = 'shotengai/customer/products/' self.template_dir = 'shotengai/customer/products/'
remove_actions :create, :update, :destroy remove_actions :create, :update, :destroy
default_query do |resource, params| default_query do |resource, params, request|
end end
index_query do |resource, params| index_query do |resource, params, request|
params[:catalog_list] ? params[:catalog_list] ?
resource.tagged_with(params[:catalog_list], on: :catalogs) : resource.tagged_with(params[:catalog_list], on: :catalogs) :
resource resource
......
...@@ -7,11 +7,11 @@ module Shotengai ...@@ -7,11 +7,11 @@ module Shotengai
remove_actions :create, :destroy remove_actions :create, :destroy
default_query do |resource, params| default_query do |resource, params, request|
end end
index_query do |resource, params| index_query do |resource, params, request|
resource.status_is(params[:status]) resource.status_is(params[:status])
end end
......
...@@ -5,13 +5,13 @@ module Shotengai ...@@ -5,13 +5,13 @@ module Shotengai
self.resources = ProductSeries self.resources = ProductSeries
self.template_dir = 'shotengai/merchant/series/' self.template_dir = 'shotengai/merchant/series/'
default_query do |resource, params| default_query do |resource, params, request|
resource.where( resource.where(
params[:product_id] && { shotengai_product_id: params[:product_id] } params[:product_id] && { shotengai_product_id: params[:product_id] }
) )
end end
index_query do |resource, params| index_query do |resource, params, request|
end end
......
...@@ -8,11 +8,11 @@ module Shotengai ...@@ -8,11 +8,11 @@ module Shotengai
remove_actions :create, :destroy remove_actions :create, :destroy
before_action :edit_only_unpaid, only: :update before_action :edit_only_unpaid, only: :update
default_query do |resource, params| default_query do |resource, params, request|
resource.in_order resource.in_order
end end
index_query do |resource, params| index_query do |resource, params, request|
resource.where( resource.where(
params[:order_id] && { shotengai_order_id: params[:order_id] } params[:order_id] && { shotengai_order_id: params[:order_id] }
).where( ).where(
......
...@@ -7,11 +7,11 @@ module Shotengai ...@@ -7,11 +7,11 @@ module Shotengai
before_action :manager_auth before_action :manager_auth
default_query do |resource, params| default_query do |resource, params, request|
resource.where(manager: @manager) resource.where(manager: @manager)
end end
index_query do |resource, params| index_query do |resource, params, request|
params[:catalog_list] ? params[:catalog_list] ?
resource.tagged_with(params[:catalog_list], on: :catalogs) : resource.tagged_with(params[:catalog_list], on: :catalogs) :
resource resource
......
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