Class: Spree::Api::V2::Storefront::WishlistsController
Instance Method Summary
collapse
#index
Methods included from Caching
#collection_cache_key, #collection_cache_opts, #serializer_params_cache_key
#collection_links, #collection_meta, #collection_permitted_params
#content_type
Instance Method Details
#add_item ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 60
def add_item
spree_authorize! :create, Spree::WishedItem
if resource.wished_items.present? && resource.wished_items.detect { |wv| wv.variant_id.to_s == params[:variant_id].to_s }.present?
@wished_item = resource.wished_items.detect { |wi| wi.variant_id.to_s == params[:variant_id].to_s }
@wished_item.quantity = params[:quantity]
else
@wished_item = Spree::WishedItem.new(params.permit(:quantity, :variant_id))
@wished_item.wishlist = resource
@wished_item.save
end
resource.reload
if @wished_item.persisted?
render_serialized_payload { serialize_wished_item(@wished_item) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
end
|
#add_items ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 103
def add_items
spree_authorize! :create, Spree::WishedItem
@wished_items = resource.wished_items
ApplicationRecord.transaction do
wished_items_attributes.each do |wished_item_attributes|
wished_item = @wished_items.find_by(variant_id: wished_item_attributes[:variant_id])
if wished_item
wished_item.update!(quantity: wished_item_attributes[:quantity] || 1)
else
Spree::WishedItem.create!(wished_item_attributes.merge(wishlist: resource))
end
end
end
resource.reload
render_serialized_payload { serialize_resource(resource) }
rescue ActiveRecord::RecordInvalid => e
render json: { error: e.record.errors.full_messages.to_sentence }, status: 422
end
|
#create ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 14
def create
spree_authorize! :create, Spree::Wishlist
@wishlist = spree_current_user.wishlists.new(wishlist_attributes)
ensure_current_store(@wishlist)
@wishlist.save
if @wishlist.persisted?
render_serialized_payload(201) { serialize_resource(@wishlist) }
else
render_error_payload(@wishlist.errors.full_messages.to_sentence)
end
end
|
#default ⇒ Object
52
53
54
55
56
57
58
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 52
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
|
#destroy ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 42
def destroy
authorize! :destroy, resource
if resource.destroy
head 204
else
render_error_payload(I18n.t('spree.api.v2.wishlist.errors.the_wishlist_could_not_be_destroyed'))
end
end
|
#remove_item ⇒ Object
93
94
95
96
97
98
99
100
101
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 93
def remove_item
spree_authorize! :destroy, wished_item
if wished_item.destroy
render_serialized_payload { serialize_wished_item(wished_item) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
end
|
#remove_items ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 125
def remove_items
spree_authorize! :destroy, Spree::WishedItem
ApplicationRecord.transaction do
resource.wished_items.where(id: wished_items_ids).each(&:destroy!)
end
resource.reload
render_serialized_payload { serialize_resource(resource) }
rescue ActiveRecord::RecordNotDestroyed => e
render json: { error: e.record.errors.full_messages.to_sentence }, status: 422
end
|
#set_item_quantity ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 81
def set_item_quantity
spree_authorize! :update, wished_item
wished_item.update(params.permit(:quantity))
if wished_item.errors.empty?
render_serialized_payload { serialize_wished_item(wished_item) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
end
|
#show ⇒ Object
9
10
11
12
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 9
def show
spree_authorize! :show, resource
super
end
|
#update ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
|
# File 'app/controllers/spree/api/v2/storefront/wishlists_controller.rb', line 30
def update
authorize! :update, resource
resource.update wishlist_attributes
if resource.errors.empty?
render_serialized_payload { serialize_resource(resource) }
else
render_error_payload(resource.errors.full_messages.to_sentence)
end
end
|