Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for updating vendor stock location #5

Open
wants to merge 1 commit into
base: features/fetch-vendors-associated-to-user
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/controllers/spree/api/v2/vendor/stock_locations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Spree
module Api
module V2
module Vendor
class StockLocationsController < Spree::Api::V2::ResourceController
before_action :require_spree_current_user
before_action :load_vendor

# GET /api/v2/vendor/vendors/:vendor_id/stock_locations
def index
if check_vendor_access
render_serialized_payload { serialize_collection(paginated_collection) }
else
render_error_payload(paginated_collection.errors)
end
end

# GET /api/v2/vendor/vendors/:vendor_id/stock_locations/:id
def show
stock_location = Spree::StockLocation.find(params[:id])
if stock_location && check_vendor_access
render_serialized_payload { serialize_resource(stock_location) }
else
render_error_payload(stock_location.errors)
end
end

# PUT/PATCH /api/v2/vendor/vendors/:vendor_id/stock_locations/:id
def update
stock_location = Spree::StockLocation.find(params[:id])
if check_vendor_access && stock_location.update(stock_location_params)
render_serialized_payload { serialize_resource(stock_location) }
else
render_error_payload(stock_location.errors)
end
end

private

def load_vendor
@vendor = Spree::Vendor.find(params[:vendor_id])
end

def check_vendor_access
@vendor.users.include?(spree_current_user)
end

def paginated_collection
collection_paginator.new(@vendor.stock_locations, params).call
end

def collection_serializer
Spree::V2::Vendor::StockLocationSerializer
end

def resource_serializer
Spree::V2::Vendor::StockLocationSerializer
end

def stock_location_params
params.require(:stock_location).permit(Spree::PermittedAttributes.stock_location_attributes)
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions app/serializers/spree/v2/vendor/stock_location_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Spree
module V2
module Vendor
class StockLocationSerializer < Spree::Api::V2::BaseSerializer
set_type :stock_location

attributes :id, :name, :default, :address1, :address2, :city, :state_id, :state_name, :country_id,
:zipcode, :phone, :active, :backorderable_default, :propagate_all_variants, :admin_name,
:phone, :created_at

belongs_to :vendor, serializer: Spree::V2::Storefront::VendorSerializer
belongs_to :country, serializer: Spree::V2::Storefront::CountrySerializer
end
end
end
end
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
resources :vendors, controller: :user_vendors, only: %i[index]
end
end

namespace :vendor, only: [] do
resources :vendors do
resources :stock_locations, only: %i[index show update]
end
end
end
end
end