From 88d96c0938ebd454267d9fd72c5f32be95d57837 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Fri, 14 Jun 2019 10:41:51 +0200 Subject: [PATCH] Use service in controller --- .../external_protocols_controller.rb | 17 +++++------------ .../search_protocols_service.rb | 2 +- .../external_protocols_controller_spec.rb | 8 ++++++++ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/controllers/external_protocols_controller.rb b/app/controllers/external_protocols_controller.rb index a4cae5862..3f1936f87 100644 --- a/app/controllers/external_protocols_controller.rb +++ b/app/controllers/external_protocols_controller.rb @@ -6,25 +6,18 @@ class ExternalProtocolsController < ApplicationController # GET list_external_protocol def index - # list_protocols = SearchService.call(index_params) - succeed = true - protocols = [ - { name: 'Protocol1' }, - { name: 'Protocol2' }, - { name: 'Protocol3' } - ] + service_call = ProtocolImporters::SearchProtocolsService + .call(protocol_source: 'protocolsio/v3', query_params: index_params) - if succeed + if service_call.succeed? render json: { html: render_to_string( partial: 'protocol_importers/list_of_protocol_cards.html.erb', - locals: { protocols: protocols } + locals: { protocols: service_call.protocols_list } ) } else - render json: { - errors: { protocol: 'error_placeholder' } - }, status: 400 + render json: { errors: service_call.errors }, status: 400 end end diff --git a/app/services/protocol_importers/search_protocols_service.rb b/app/services/protocol_importers/search_protocols_service.rb index 2a6a11ae3..ab418326b 100644 --- a/app/services/protocol_importers/search_protocols_service.rb +++ b/app/services/protocol_importers/search_protocols_service.rb @@ -35,7 +35,7 @@ module ProtocolImporters @errors[:invalid_params][:key] = 'Key cannot be empty' if @query_params[:key].blank? # try if page id is ok - if @query_params[:page_id] && !@query_params[:page_id].positive? + if @query_params[:page_id] && !@query_params[:page_id].to_i.positive? @errors[:invalid_params][:page_id] = 'Page needs to be positive' end diff --git a/spec/controllers/external_protocols_controller_spec.rb b/spec/controllers/external_protocols_controller_spec.rb index cb750c186..94e0cc1c6 100644 --- a/spec/controllers/external_protocols_controller_spec.rb +++ b/spec/controllers/external_protocols_controller_spec.rb @@ -24,6 +24,14 @@ describe ExternalProtocolsController, type: :controller do let(:action) { get :index, params: params } + before do + service = double('success_service') + allow(service).to(receive(:succeed?)).and_return(true) + allow(service).to(receive(:protocols_list)).and_return({}) + + allow_any_instance_of(ProtocolImporters::SearchProtocolsService).to(receive(:call)).and_return(service) + end + it 'returns JSON, 200 response when protocol parsing was valid' do action expect(response).to have_http_status(:success)