Commit 81f27000 by ivan Lan

Add Customer Order & Cart Controller

parent 16d637e5
json.partial! "shotengai/share/cart", cart: @resource
json.extract! cart, :id
json.snapshots cart.snapshots, partial: 'shotengai/share/snapshot_simple', as: :snapshot
\ No newline at end of file
......@@ -27,14 +27,19 @@ module Shotengai
class Cart < ::ActiveRecord::Base
self.table_name = 'shotengai_orders'
belongs_to :buyer, polymorphic: true, optional: true
default_scope { where(status: 'cart') }
#
# class Order < Shotengai::Order
# can_by 'Product'
# end
#
# Would let Product belongs to :order & order_cart
# Would let Product belongs to :order & order_cart.
# And get cart_class Order::Cart
#
class << self
def can_buy *good_class_names
good_classes = good_class_names.map { |name| Object.const_get(name) }
......@@ -44,8 +49,8 @@ module Shotengai
}, 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, foreign_key: :shotengai_order_id
# cart has many good_class_snapshot.collection
has_many "#{klass.model_name.singular}_snapshots".to_sym, class_name: "#{klass.snapshot_class}", foreign_key: :shotengai_order_id
# belongs_to 本 Cart class
# optional: true 允许父对象不存在
klass.snapshot_class.belongs_to(
......
module Shotengai
module Controller
module Customer
class OrdersController < Shotengai::Controller::Base
self.resources = Cart
self.template_dir = 'shotengai/customer/orders/'
remove_actions
index_query do |resource, params|
resource.status_is(params[:status])
end
def add_to_cart
buyer_type, buyer_id = add_to_cart_params.values_at(:buyer_type, :buyer_id)
buyer = buyer_type.constantize.find(buyer_id)
@snapshot = buyer.order_cart.snapshots.create!(add_to_cart_params)
head 201
end
def create_by_snapshot
end
def create_directly
end
def pay
@resource.pay!
respond_with @resource, template: "#{@@template_dir}/show"
end
def cancel
@resource.cancel!
respond_with @resource, template: "#{@@template_dir}/show"
end
def get_it
@resource.get_it
respond_with @resource, template: "#{@@template_dir}/show"
end
private
def resource_params
params.require(resource_key).permit(
:address, :customer_remark, snapshot_ids: []
)
end
def add_to_cart_params
params.require(:snapshot).permit(
:series_id, :count, :buyer_type, :buyer_id
)
end
end
end
end
end
module Shotengai
module Controller
module Customer
class OrdersController < Shotengai::Controller::Base
self.resources = Order
self.template_dir = 'shotengai/customer/orders/'
before_action :buyer_auth
before_action :edit_only_unpaid, only: [:update]
# before_action would not keep the super methods' "only" condition
skip_before_action :set_resource, only: [:cart, :add_to_cart, :create_directly]
remove_actions :destroy
index_query do |resource, params|
resource.status_is(params[:status])
end
def cart
@resource = @buyer.order_cart
respond_with @resource, template: "shotengai/customer/cart/show"
end
def add_to_cart
snapshot = @buyer.order_cart.product_snapshots.create!(snapshot_params)
respond_with @resource = snapshot, template: 'shotengai/customer/snapshots/show', status: 201
end
def create_directly # using :series_id & :count
ActiveRecord::Base.transaction do
@resource = @buyer.orders.create!(resource_params)
Shotengai::Series.find(snapshot_params[:shotengai_series_id]).snapshots.create!(
count: snapshot_params[:count],
shotengai_order: @resource
)
end
respond_with @resource, template: "#{@@template_dir}/show", status: 201
end
def pay
@resource.pay!
respond_with @resource, template: "#{@@template_dir}/show"
end
def cancel
@resource.cancel!
respond_with @resource, template: "#{@@template_dir}/show"
end
def get_it
@resource.get_it
respond_with @resource, template: "#{@@template_dir}/show"
end
private
def buyer_auth
@buyer = params[:buyer_type].constantize.find(params[:buyer_id])
end
def resource_params
params.require(resource_key).permit(
:address, :customer_remark,
incr_snapshot_ids: [], gone_snapshot_ids: []
)
end
def snapshot_params
params.require(:snapshot).permit(
:shotengai_series_id, :count
)
end
def edit_only_unpaid
raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.unpaid?
end
end
end
end
end
......@@ -3,7 +3,7 @@ module Shotengai
module Customer
class ProductSnapshotsController < Shotengai::Controller::Base
self.resources = ProductSnapshot
self.template_dir = 'shotengai/merchant/snapshots/'
self.template_dir = 'shotengai/customer/snapshots/'
before_action :edit_only_unpaid, only: [:update, :destroy]
......
......@@ -28,7 +28,7 @@ module Shotengai
end
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
......
......@@ -38,8 +38,9 @@ module Shotengai
include AASM_DLC
aasm column: :status do
state :unpaid, initial: true
state :paid, :delivering, :received, :evaluated
event :pay, after: [:fill_snapshot, :set_pay_time] {
state :paid, :delivering, :received, :canceled, :evaluated
event :pay, before: [:fill_snapshot, :set_pay_time] {
transitions from: :unpaid, to: :paid
}
event :cancel {
......@@ -51,9 +52,9 @@ module Shotengai
event :get_it, after: :set_receipt_time {
transitions from: :delivering, to: :received
}
event :evaluate {
transitions from: :received, to: :evaluated
}
# event :evaluate {
# transitions from: :received, to: :evaluated
# }
# event :soft_delete
end
......@@ -93,6 +94,27 @@ module Shotengai
snapshots.sum(&:total_original_price)
end
# into order
def incr_snapshot_ids= ids
ActiveRecord::Base.transaction do
ids.each { |id|
# using update(shotengai_order_id: id) can not get self.id before save
Shotengai::Snapshot.find(id).update!(shotengai_order: self)
}
end
end
# back to cart
def gone_snapshot_ids= ids
ActiveRecord::Base.transaction do
ids.each { |id|
Shotengai::Snapshot.find(id).update!(
shotengai_order_id: self.class.cart_class.where(buyer: self.buyer).first.id
)
}
end
end
class << self
def inherited subclass
# define Cart class
......
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