Commit 4b811198 by ivan Lan

Add merchant/products/batch_event

parent 93c26b73
...@@ -87,6 +87,10 @@ module Shotengai ...@@ -87,6 +87,10 @@ module Shotengai
member do member do
post :put_on_shelf post :put_on_shelf
post :sold_out post :sold_out
post :relive
end
collection do
post :batch_event
end end
resources :#{product}_series resources :#{product}_series
end end
......
...@@ -118,6 +118,27 @@ RSpec.describe "#{namespace}/products", type: :request, capture_examples: true, ...@@ -118,6 +118,27 @@ RSpec.describe "#{namespace}/products", type: :request, capture_examples: true,
end end
end end
path "/#{namespace}/products/batch_event" do
post(summary: '商户 批量处理商品(put_on_shelf, sold_out, soft_delete, relive)') do
parameter :ids, in: :query, type: :string
parameter :event, in: :query, type: :string
let(:ids) { @products.first(2).map(&:id) }
let(:event) { 'put_on_shelf' }
parameter :manager_type, in: :query, type: :string
parameter :manager_id, in: :query, type: :integer
let(:manager_id) { @merchant.id }
let(:manager_type) { @merchant.class.name }
produces 'application/json'
consumes 'application/json'
response(200, description: 'successful') do
it {
expect(Product.where(status: 'on_sale').map(&:id).sort).to eq(@products.first(2).map(&:id).sort)
}
end
end
end
path "/#{namespace}/products/{id}" do path "/#{namespace}/products/{id}" do
parameter 'id', in: :path, type: :string parameter 'id', in: :path, type: :string
let(:id) { @product_1.id } let(:id) { @product_1.id }
......
...@@ -9,7 +9,8 @@ module Shotengai ...@@ -9,7 +9,8 @@ module Shotengai
def base_resources= resources def base_resources= resources
class_eval %Q{ class_eval %Q{
def add_base_resources def add_base_resources
@base_resources = ::#{resources} @base_resources =
ActiveRecord::Relation === #{resources} ? #{resources} : ::#{resources}
end end
} }
end end
...@@ -39,7 +40,7 @@ module Shotengai ...@@ -39,7 +40,7 @@ module Shotengai
respond_to :json respond_to :json
rescue_from ::Shotengai::WebError do |e| rescue_from ::Shotengai::WebError do |e|
render json: { error: e.message }, status: e.status render json: { error: e.message, code: e.code }, status: e.status
end end
rescue_from ActiveRecord::RecordInvalid do |e| rescue_from ActiveRecord::RecordInvalid do |e|
......
...@@ -5,13 +5,13 @@ module Shotengai ...@@ -5,13 +5,13 @@ module Shotengai
self.base_resources = Product self.base_resources = Product
self.template_dir = 'shotengai/merchant/products/' self.template_dir = 'shotengai/merchant/products/'
skip_before_action :set_resource, only: :batch_event
def default_query resources def default_query resources
resources.where(@manager && { manager: @manager }) resources.where(@manager && { manager: @manager })
end end
def index_query resources def index_query resources
p params
p params[:status]
( (
params[:catalog_list] ? params[:catalog_list] ?
resources.tagged_with(params[:catalog_list], on: :catalogs) : resources.tagged_with(params[:catalog_list], on: :catalogs) :
...@@ -31,11 +31,25 @@ module Shotengai ...@@ -31,11 +31,25 @@ module Shotengai
respond_with @resource, template: "#{@template_dir}/show", status: 200 respond_with @resource, template: "#{@template_dir}/show", status: 200
end end
def relive
@resource.relive!
respond_with @resource, template: "#{@template_dir}/show", status: 200
end
def destroy def destroy
@resource.soft_delete! @resource.soft_delete!
head 204 head 204
end end
def batch_event # params[ids] params[:event]
event = (@base_resources.where(nil).klass.aasm.events.map(&:name) & Array[params[:event].to_sym]).first
raise ::Shotengai::WebError.new('Invaild event', '-1', 400) unless event
ActiveRecord::Base.transaction do
default_resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
end
head 200
end
private private
def resource_params def resource_params
# QUESTION: need these ? # QUESTION: need these ?
......
...@@ -49,6 +49,10 @@ module Shotengai ...@@ -49,6 +49,10 @@ module Shotengai
event :soft_delete do event :soft_delete do
transitions from: [:on_sale, :not_on], to: :deleted transitions from: [:on_sale, :not_on], to: :deleted
end end
event :relive do
transitions from: :deleted, to: :not_on
end
end end
def status_zh def status_zh
......
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