Bring from spree_api the api controllers that are overriden in OFN so that we can merge the original and the override afterwards

This commit is contained in:
luisramos0
2019-07-18 17:14:04 +01:00
parent 75c7e0b939
commit d26a0b6b73
3 changed files with 229 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
module Spree
module Api
class ProductsController < Spree::Api::BaseController
respond_to :json
def index
if params[:ids]
@products = product_scope.where(:id => params[:ids])
else
@products = product_scope.ransack(params[:q]).result
end
@products = @products.page(params[:page]).per(params[:per_page])
respond_with(@products)
end
def show
@product = find_product(params[:id])
respond_with(@product)
end
def new
end
def create
authorize! :create, Product
params[:product][:available_on] ||= Time.now
@product = Product.new(params[:product])
begin
if @product.save
respond_with(@product, :status => 201, :default_template => :show)
else
invalid_resource!(@product)
end
rescue ActiveRecord::RecordNotUnique
@product.permalink = nil
retry
end
end
def update
authorize! :update, Product
@product = find_product(params[:id])
if @product.update_attributes(params[:product])
respond_with(@product, :status => 200, :default_template => :show)
else
invalid_resource!(@product)
end
end
def destroy
authorize! :delete, Product
@product = find_product(params[:id])
@product.update_attribute(:deleted_at, Time.now)
@product.variants_including_master.update_all(:deleted_at => Time.now)
respond_with(@product, :status => 204)
end
end
end
end

View File

@@ -0,0 +1,93 @@
module Spree
module Api
class ShipmentsController < Spree::Api::BaseController
respond_to :json
before_filter :find_order
before_filter :find_and_update_shipment, :only => [:ship, :ready, :add, :remove]
def create
variant = Spree::Variant.find(params[:variant_id])
quantity = params[:quantity].to_i
@shipment = @order.shipments.create(:stock_location_id => params[:stock_location_id])
@order.contents.add(variant, quantity, nil, @shipment)
@shipment.refresh_rates
@shipment.save!
respond_with(@shipment.reload, :default_template => :show)
end
def update
authorize! :read, Shipment
@shipment = @order.shipments.find_by_number!(params[:id])
params[:shipment] ||= []
unlock = params[:shipment].delete(:unlock)
if unlock == 'yes'
@shipment.adjustment.open
end
@shipment.update_attributes(params[:shipment])
if unlock == 'yes'
@shipment.adjustment.close
end
@shipment.reload
respond_with(@shipment, :default_template => :show)
end
def ready
authorize! :read, Shipment
unless @shipment.ready?
if @shipment.can_ready?
@shipment.ready!
else
render "spree/api/shipments/cannot_ready_shipment", :status => 422 and return
end
end
respond_with(@shipment, :default_template => :show)
end
def ship
authorize! :read, Shipment
unless @shipment.shipped?
@shipment.ship!
end
respond_with(@shipment, :default_template => :show)
end
def add
variant = Spree::Variant.find(params[:variant_id])
quantity = params[:quantity].to_i
@order.contents.add(variant, quantity, nil, @shipment)
respond_with(@shipment, :default_template => :show)
end
def remove
variant = Spree::Variant.find(params[:variant_id])
quantity = params[:quantity].to_i
@order.contents.remove(variant, quantity, @shipment)
@shipment.reload if @shipment.persisted?
respond_with(@shipment, :default_template => :show)
end
private
def find_order
@order = Spree::Order.find_by_number!(params[:order_id])
authorize! :read, @order
end
def find_and_update_shipment
@shipment = @order.shipments.find_by_number!(params[:id])
@shipment.update_attributes(params[:shipment])
@shipment.reload
end
end
end
end

View File

@@ -0,0 +1,75 @@
module Spree
module Api
class VariantsController < Spree::Api::BaseController
respond_to :json
before_filter :product
def index
@variants = scope.includes(:option_values).ransack(params[:q]).result.
page(params[:page]).per(params[:per_page])
respond_with(@variants)
end
def show
@variant = scope.includes(:option_values).find(params[:id])
respond_with(@variant)
end
def new
end
def create
authorize! :create, Variant
@variant = scope.new(params[:variant])
if @variant.save
respond_with(@variant, :status => 201, :default_template => :show)
else
invalid_resource!(@variant)
end
end
def update
authorize! :update, Variant
@variant = scope.find(params[:id])
if @variant.update_attributes(params[:variant])
respond_with(@variant, :status => 200, :default_template => :show)
else
invalid_resource!(@product)
end
end
def destroy
authorize! :delete, Variant
@variant = scope.find(params[:id])
@variant.destroy
respond_with(@variant, :status => 204)
end
private
def product
@product ||= Spree::Product.find_by_permalink(params[:product_id]) if params[:product_id]
end
def scope
if @product
unless current_api_user.has_spree_role?("admin") || params[:show_deleted]
variants = @product.variants_including_master
else
variants = @product.variants_including_master.with_deleted
end
else
variants = Variant.scoped
if current_api_user.has_spree_role?("admin")
unless params[:show_deleted]
variants = Variant.active
end
else
variants = variants.active
end
end
variants
end
end
end
end