/api/app/controllers/spree/api/v1/products_controller.rb

https://gitlab.com/shinvdu/spree · Ruby · 129 lines · 80 code · 16 blank · 33 comment · 6 complexity · 77c01cb0f2cafbc98d4b594783fcea81 MD5 · raw file

  1. module Spree
  2. module Api
  3. module V1
  4. class ProductsController < Spree::Api::BaseController
  5. def index
  6. if params[:ids]
  7. @products = product_scope.where(id: params[:ids].split(",").flatten)
  8. else
  9. @products = product_scope.ransack(params[:q]).result
  10. end
  11. @products = @products.distinct.page(params[:page]).per(params[:per_page])
  12. expires_in 15.minutes, public: true
  13. headers['Surrogate-Control'] = "max-age=#{15.minutes}"
  14. respond_with(@products)
  15. end
  16. def show
  17. @product = find_product(params[:id])
  18. expires_in 15.minutes, public: true
  19. headers['Surrogate-Control'] = "max-age=#{15.minutes}"
  20. headers['Surrogate-Key'] = "product_id=1"
  21. respond_with(@product)
  22. end
  23. # Takes besides the products attributes either an array of variants or
  24. # an array of option types.
  25. #
  26. # By submitting an array of variants the option types will be created
  27. # using the *name* key in options hash. e.g
  28. #
  29. # product: {
  30. # ...
  31. # variants: {
  32. # price: 19.99,
  33. # sku: "hey_you",
  34. # options: [
  35. # { name: "size", value: "small" },
  36. # { name: "color", value: "black" }
  37. # ]
  38. # }
  39. # }
  40. #
  41. # Or just pass in the option types hash:
  42. #
  43. # product: {
  44. # ...
  45. # option_types: ['size', 'color']
  46. # }
  47. #
  48. # By passing the shipping category name you can fetch or create that
  49. # shipping category on the fly. e.g.
  50. #
  51. # product: {
  52. # ...
  53. # shipping_category: "Free Shipping Items"
  54. # }
  55. #
  56. def new
  57. end
  58. def create
  59. authorize! :create, Product
  60. params[:product][:available_on] ||= Time.current
  61. set_up_shipping_category
  62. options = { variants_attrs: variants_params, options_attrs: option_types_params }
  63. @product = Core::Importer::Product.new(nil, product_params, options).create
  64. if @product.persisted?
  65. respond_with(@product, status: 201, default_template: :show)
  66. else
  67. invalid_resource!(@product)
  68. end
  69. end
  70. def update
  71. @product = find_product(params[:id])
  72. authorize! :update, @product
  73. options = { variants_attrs: variants_params, options_attrs: option_types_params }
  74. @product = Core::Importer::Product.new(@product, product_params, options).update
  75. if @product.errors.empty?
  76. respond_with(@product.reload, status: 200, default_template: :show)
  77. else
  78. invalid_resource!(@product)
  79. end
  80. end
  81. def destroy
  82. @product = find_product(params[:id])
  83. authorize! :destroy, @product
  84. @product.destroy
  85. respond_with(@product, status: 204)
  86. end
  87. private
  88. def product_params
  89. params.require(:product).permit(permitted_product_attributes)
  90. end
  91. def variants_params
  92. variants_key = if params[:product].has_key? :variants
  93. :variants
  94. else
  95. :variants_attributes
  96. end
  97. params.require(:product).permit(
  98. variants_key => [permitted_variant_attributes, :id],
  99. ).delete(variants_key) || []
  100. end
  101. def option_types_params
  102. params[:product].fetch(:option_types, [])
  103. end
  104. def set_up_shipping_category
  105. if shipping_category = params[:product].delete(:shipping_category)
  106. id = ShippingCategory.find_or_create_by(name: shipping_category).id
  107. params[:product][:shipping_category_id] = id
  108. end
  109. end
  110. end
  111. end
  112. end
  113. end