Browse Source

Reuse AddressDataPresenter where appropriate

CLDC-3787-Autocomplete-address-search
Manny Dinssa 4 days ago
parent
commit
475ba0fa9d
  1. 15
      app/controllers/address_search_controller.rb

15
app/controllers/address_search_controller.rb

@ -12,7 +12,8 @@ class AddressSearchController < ApplicationController
if service.error.present? if service.error.present?
render json: { error: service.error }, status: :unprocessable_entity render json: { error: service.error }, status: :unprocessable_entity
else else
render json: [{ address: service.result["ADDRESS"], uprn: service.result["UPRN"] }] presenter = AddressDataPresenter.new(service.result)
render json: [{ address: presenter.address, uprn: presenter.uprn }]
end end
elsif query.match?(/[a-zA-Z]/) elsif query.match?(/[a-zA-Z]/)
# Query contains letters, assume it's an address # Query contains letters, assume it's an address
@ -22,7 +23,11 @@ class AddressSearchController < ApplicationController
if service.error.present? if service.error.present?
render json: { error: service.error }, status: :unprocessable_entity render json: { error: service.error }, status: :unprocessable_entity
else else
render json: service.result.map { |result| { address: result["ADDRESS"], uprn: result["UPRN"] } } results = service.result.map do |result|
presenter = AddressDataPresenter.new(result)
{ address: presenter.address, uprn: presenter.uprn }
end
render json: results
end end
else else
# Query is ambiguous, use both APIs and merge results # Query is ambiguous, use both APIs and merge results
@ -37,7 +42,11 @@ class AddressSearchController < ApplicationController
if address_service.error.present? && uprn_service.error.present? if address_service.error.present? && uprn_service.error.present?
render json: { error: "Address and UPRN are not recognised. Check the input." }, status: :unprocessable_entity render json: { error: "Address and UPRN are not recognised. Check the input." }, status: :unprocessable_entity
else else
render json: results.map { |result| { address: result["ADDRESS"], uprn: result["UPRN"] } } formatted_results = results.map do |result|
presenter = AddressDataPresenter.new(result)
{ address: presenter.address, uprn: presenter.uprn }
end
render json: formatted_results
end end
end end
end end

Loading…
Cancel
Save