Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Move wished variant add update and remove to actions on wishlist controller.

Matthew Kennedyef12b267dc54
diff --git a/api/app/controllers/spree/api/v2/storefront/wished_variants_controller.rb b/api/app/controllers/spree/api/v2/storefront/wished_variants_controller.rb
deleted file mode 100644
index d04e1f516a..0000000000
--- a/api/app/controllers/spree/api/v2/storefront/wished_variants_controller.rb
+++ /dev/null
@@ -1,68 +0,0 @@
-module Spree
- module Api
- module V2
- module Storefront
- class WishedVariantsController < ::Spree::Api::V2::ResourceController
- def create
- spree_authorize! :create, Spree::WishedVariant
- spree_authorize! :update, wishlist
-
- wished_variant = Spree::WishedVariant.new(wished_variant_attributes)
-
- if wishlist.include? params[:wished_variant][:variant_id]
- wished_variant = wishlist.wished_variants.detect { |item| item.variant_id.to_s == params[:wished_product][:variant_id] }
- else
- wished_variant.wishlist = wishlist
- wished_variant.save
- end
-
- wishlist.reload
- if wished_variant.persisted?
- render_serialized_payload { serialize_resource(wished_variant) }
- else
- render_error_payload(wished_variant.errors.full_messages.to_sentence)
- end
- end
-
- def update
- spree_authorize! :update, resource
- resource.update(wished_variant_attributes)
-
- if resource.errors.empty?
- render_serialized_payload { serialize_resource(resource) }
- else
- render_error_payload(resource.errors.full_messages.to_sentence)
- end
- end
-
- def destroy
- spree_authorize! :destroy, resource
- if resource.destroy
- render_serialized_payload { serialize_resource(resource) }
- else
- render_error_payload('Something went wrong')
- end
- end
-
- private
-
- def resource
- @resource ||= wishlist.wished_variants.find(params[:id])
- end
-
- def wishlist
- @wishlist ||= current_store.wishlists.find_by!(token: params[:wishlist_id])
- end
-
- def wished_variant_attributes
- params.require(:wished_variant).permit(permitted_wished_variant_attributes)
- end
-
- def resource_serializer
- ::Spree::V2::Storefront::WishedVariantSerializer
- end
- end
- end
- end
- end
-end
diff --git a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
index 7c758423bf..4a16a720ba 100644
--- a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
@@ -5,7 +5,7 @@ module Spree
class WishlistsController < ::Spree::Api::V2::ResourceController
include Spree::Api::V2::CollectionOptionsHelpers
- before_action :require_spree_current_user
+ before_action :require_spree_current_user, except: [:show]
def index
spree_authorize! :index, Spree::Wishlist
@@ -56,23 +56,72 @@ module Spree
def default
spree_authorize! :create, Spree::Wishlist
+
default_wishlist = spree_current_user.default_wishlist_for_store(current_store)
render_serialized_payload { serialize_resource(default_wishlist) }
end
+ def add_item
+ spree_authorize! :create, Spree::WishedVariant
+ spree_authorize! :update, resource
+
+ wished_variant = Spree::WishedVariant.new(params.permit(:quantity, :remark, :variant_id))
+
+ if resource.include? params[:variant_id]
+ wished_variant = resource.wished_variants.detect { |wv| wv.variant_id == params[:variant_id] }
+ else
+ wished_variant.wishlist = resource
+ wished_variant.save
+ end
+
+ resource.reload
+
+ if wished_variant.persisted?
+ render_serialized_payload { serialize_resource(resource) }
+ else
+ render_error_payload(resource.errors.full_messages.to_sentence)
+ end
+ end
+
+ def update_item
+ spree_authorize! :update, wished_variant
+
+ wished_variant.update(params.permit(:quantity, :remark))
+
+ if wished_variant.errors.empty?
+ render_serialized_payload { serialize_resource(resource) }
+ else
+ render_error_payload(resource.errors.full_messages.to_sentence)
+ end
+ end
+
+ def remove_item
+ spree_authorize! :update, wished_variant
+
+ if wished_variant.destroy
+ render_serialized_payload { serialize_resource(resource) }
+ else
+ render_error_payload('Something went wrong')
+ end
+ end
+
private
def wishlist_attributes
params.require(:wishlist).permit(permitted_wishlist_attributes)
end
+ def wished_variant_attributes
+ params.permit(permitted_wished_variant_attributes)
+ end
+
def resource
- @resource ||= scope.find_by!(token: params[:id])
+ @resource ||= current_store.wishlists.find_by!(token: params[:id])
end
- def scope
- current_store.wishlists
+ def wished_variant
+ @wished_variant ||= resource.wished_variants.find(params[:wished_variant_id])
end
def resource_serializer
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 85e8d48507..7eaf4657fe 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -168,8 +168,13 @@ Spree::Core::Engine.add_routes do
resources :cms_pages, only: %i[index show]
resources :wishlists do
- get 'default', on: :collection
- resources :wished_variants, only: [:create, :update, :destroy]
+ get :default, on: :collection
+
+ member do
+ post :add_item
+ patch 'update_item/:wished_variant_id', to: 'wishlists#update_item', as: :update_item
+ delete 'remove_item/:wished_variant_id', to: 'wishlists#remove_item', as: :remove_item
+ end
end
end