Adapt exchange products routes to make api/exchanges/products (without exchange_id) also go to exchange_products#index

This commit is contained in:
luisramos0
2019-11-22 17:24:14 +00:00
parent a589ba38da
commit ff584f9be9
2 changed files with 15 additions and 16 deletions

View File

@@ -1,22 +1,21 @@
# This controller lists products that can be added to an exchange
module Api
class ExchangeProductsController < Api::BaseController
skip_authorization_check only: [:index, :show]
skip_authorization_check only: [:index]
# Lists products for an Enterprise in an Order Cycle
# This is the same as index below but when the Exchange doesn't exist yet
# If exchange_id is present in the URL:
# Lists Products that can be added to that Exchange
#
# Parameters are: enterprise_id, order_cycle_id and incoming
# order_cycle_id is optional if for incoming
def show
load_data_from_params
render_products
end
# Lists products in an Exchange
# If exchange_id is not present in the URL:
# Lists Products of the Enterprise given that can be added to the given Order Cycle
# In this case parameters are: enterprise_id, order_cycle_id and incoming
# (order_cycle_id is not necessary for incoming exchanges)
def index
load_data_from_exchange
if params[:exchange_id].present?
load_data_from_exchange
else
load_data_from_other_params
end
render_products
end
@@ -42,7 +41,7 @@ module Api
@enterprise = exchange.sender
end
def load_data_from_params
def load_data_from_other_params
@enterprise = Enterprise.find_by_id(params[:enterprise_id])
if params[:order_cycle_id]

View File

@@ -43,8 +43,8 @@ Openfoodnetwork::Application.routes.draw do
get :properties, on: :member
end
resources :exchanges, only: [:show], controller: 'exchange_products' do
resources :products, controller: 'exchange_products', only: [:index]
resources :exchanges, only: [:show], to: 'exchange_products#index' do
get :products, to: 'exchange_products#index'
end
resource :status do