Commit d1cea924 by ivan Lan

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

parent 73f9c6f4
......@@ -91,10 +91,8 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
end
end
path "/#{namespace}/orders" do
get(summary: '用户 订单列表') do
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
......@@ -102,27 +100,34 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
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
parameter :status, in: :query, type: :string
let(:page) { 1 }
let(:per_page) { 100 }
let(:id) { @order_1.id }
produces 'application/json'
consumes 'application/json'
response(200, description: 'successful') do
it { expect(JSON.parse(response.body)['orders'].count).to eq(3) }
before do
@orders.last.product_snapshots.create!(
product_series: @series_1,
count: 100
)
end
response(200, description: 'filter by status') do
before { @orders.last.pay! }
let(:status) { 'paid' }
it { expect(JSON.parse(response.body)['orders'].count).to eq(1) }
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
post(summary: '用户 创建订单(using incr_snapshot_ids') do
path "/#{namespace}/orders" do
get(summary: '用户 订单列表') do
produces 'application/json'
consumes 'application/json'
parameter :buyer_type, in: :query, type: :string
......@@ -130,54 +135,26 @@ RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, ta
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
parameter :page, in: :query, type: :string
parameter :per_page, in: :query, type: :string
parameter :status, in: :query, type: :string
response(400, description: 'failed, Cannot edit a snapshot which order was already paid.') do
let(:page) { 1 }
let(:per_page) { 100 }
let(:order) {{
order: {
incr_snapshot_ids: [ @snapshot_1.id ],
}
}}
before {
@order_1.pay!
}
it {
expect(response.status).to eq(400)
}
produces 'application/json'
consumes 'application/json'
response(200, description: 'successful') do
it { expect(JSON.parse(response.body)['orders'].count).to eq(3) }
end
response(200, description: 'filter by status') do
before { @orders.last.pay! }
let(:status) { 'paid' }
it { expect(JSON.parse(response.body)['orders'].count).to eq(1) }
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
......
......@@ -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
......
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
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,
: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
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
......@@ -12,8 +12,8 @@ module Shotengai
#
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
......@@ -103,11 +103,11 @@ module Shotengai
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.requrie(resource_key).permit!.merge other_resource_params
params.require(resource_key).permit!.merge other_resource_params
end
# rewrite this to add more custom column
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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(
......
......@@ -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
......
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