diff --git a/controller/suggest.js b/controller/suggest.js index 36c3cc98..956363b8 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -1,4 +1,5 @@ +var service = { suggest: require('../service/suggest') }; var geojsonify = require('../helper/geojsonify').suggest; function setup( backend, query ){ @@ -16,18 +17,11 @@ function setup( backend, query ){ }; // query backend - backend().client.suggest( cmd, function( err, data ){ + service.suggest( backend, cmd, function( err, docs ){ - var docs = []; - - // handle backend errors + // error handler if( err ){ return next( err ); } - // map response to a valid FeatureCollection - if( data && Array.isArray( data.pelias ) && data.pelias.length ){ - docs = data['pelias'][0].options || []; - } - // convert docs to geojson var geojson = geojsonify( docs ); diff --git a/service/suggest.js b/service/suggest.js new file mode 100644 index 00000000..b12087aa --- /dev/null +++ b/service/suggest.js @@ -0,0 +1,28 @@ + +/** + + cmd can be any valid ES suggest command + +**/ + +function service( backend, cmd, cb ){ + + // query new backend + backend().client.suggest( cmd, function( err, data ){ + + // handle backend errors + if( err ){ return cb( err ); } + + // map returned documents + var docs = []; + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs = data['pelias'][0].options || []; + } + + // fire callback + return cb( null, docs ); + }); + +} + +module.exports = service; \ No newline at end of file