Commit 30aac4aa by ivan Lan

Fix Cart & Finish Customer Snapshot Controller

parent 88461037
json.extract! snapshot, :id, :original_price, :price, :count,
json.extract! snapshot, :id, :shotengai_series_id, :original_price, :price, :count,
:product_status_zh, :order_status_zh, :product_status, :order_status,
:total_price, :total_original_price, :revised_amount,
:spec, :cover_image, :banners, :detail, :meta
\ No newline at end of file
:spec, :cover_image, :banners, :detail, :meta, :is_in_cart
\ No newline at end of file
json.extract! snapshot, :id, :original_price, :price, :count,
json.extract! snapshot, :id, :shotengai_series_id, :original_price, :price, :count,
:product_status_zh, :order_status_zh, :product_status, :order_status,
:total_price, :total_original_price, :revised_amount,
:spec, :cover_image, :meta
\ No newline at end of file
:spec, :cover_image, :meta, :is_in_cart
\ No newline at end of file
......@@ -41,11 +41,11 @@ module Shotengai
# 所有snapshot
has_many :snapshots, -> {
where(type: good_classes.map { |good_class| "#{good_class.name}Snapshot" })
}, class_name: 'Shotengai::Snapshot'
}, class_name: 'Shotengai::Snapshot', foreign_key: :shotengai_order_id
good_classes.each do |klass|
# cart has many good_class.collection
has_many klass.model_name.collection.to_sym, class_name: klass.name
has_many klass.model_name.collection.to_sym, class_name: klass.name, foreign_key: :shotengai_order_id
# belongs_to 本 Cart class
# optional: true 允许父对象不存在
klass.snapshot_class.belongs_to(
......
module Shotengai
module Controller
module Customer
class ProductSnapshotsController < Shotengai::Controller::Base
self.resources = ProductSnapshot
self.template_dir = 'shotengai/merchant/snapshots/'
before_action :edit_only_unpaid, only: [:update, :destroy]
default_query do |resource, params|
# /orders/:order_id/snapshots
# /series/:series_id/snapshots
resource.where(
params[:order_id] && { shotengai_order_id: params[: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
def create
buyer_type, buyer_id = resource_params.values_at(:buy_type, :buyer_id)
@buyer = buyer_type.constantize.find(buyer_id) if buyer_type && buyer_id
@resource = default_resources.create!(
resource_params.merge(buyer: buyer)
)
respond_with @resource, template: "#{@@template_dir}/show", status: 201
end
private
def resource_params
params.require(resource_key).permit(
:count, :shotengai_series_id, :buyer_id, :buy_type
)
end
def edit_only_unpaid
raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.order.unpaid?
end
end
end
end
end
......@@ -6,7 +6,8 @@ module Shotengai
self.template_dir = 'shotengai/merchant/snapshots/'
remove_actions :create, :destroy
before_action :edit_only_unpaid, only: :update
default_query do |resource, params|
resource.in_order
end
......@@ -19,17 +20,16 @@ module Shotengai
)
end
def update
raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.order.unpaid?
super
end
private
def resource_params
params.require(resource_key).permit(
:revised_amount
)
end
def edit_only_unpaid
raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.order.unpaid?
end
end
end
end
......
......@@ -33,6 +33,8 @@ module Shotengai
belongs_to :shotengai_order, foreign_key: :shotengai_order_id,
class_name: 'Shotengai::Order', optional: true
belongs_to :shotengai_cart, foreign_key: :shotengai_order_id,
class_name: 'Shotengai::Cart', optional: true
scope :in_order, ->{
joins("
......@@ -92,6 +94,10 @@ module Shotengai
count * original_price
end
def is_in_cart
shotengai_cart&.status == 'cart'
end
def product_status; series.status end
def product_status_zh; series.status_zh end
......
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