From 108d377e96cb6f507c526ede1340233eb92a45cb Mon Sep 17 00:00:00 2001 From: kohsah Date: Thu, 4 Jan 2018 14:29:31 +0530 Subject: [PATCH] Added api to find keywordValue --- apiroutes.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/apiroutes.js b/apiroutes.js index 695d14d..e78632f 100644 --- a/apiroutes.js +++ b/apiroutes.js @@ -130,4 +130,60 @@ function findKeyword(req, res, next) { ); } +/** + * Accepts a Keyword Value parameter and returns full keyword objects for it from the full filter cache + * /keyword?kwv=x&kwv=y + */ +router.get( + '/keywordValue', + findKeywordValue +); + +/** + * Finds keyword objects using the full filter cache based on a filter value + * NOTE: this makes use of the Promise form of the fs module. + * where the APIs are generated by promisyfing fs using bluebird. + * @param {object} req + * @param {object} res + * @param {object} next + */ +function findKeywordValue(req, res, next) { + const keywordValue = coerceIntoArray(req.query.kwv) ; + + fs.openAsync(filtercache.getCacheFile(), 'r').then( + function(fd) { + fs.readFileAsync(filtercache.getCacheFile(), 'utf8') + .then( + function(contents) { + let filterObj = JSON.parse(contents); + apputils.fsClose(fs, fd); + let kwObjs = filterObj.filter.find( + filter => filter.name === 'keywords' + ); + let found = []; + keywordValue.map( + (value) => { + let filterVal = kwObjs.keyword.find( + (filter) => filter.value === value + ); + if (filterVal !== undefined) { + found.push(filterVal); + } + } + ); + res.json({found: found}); + } + ).catch( + function (error) { + winston.log(" error in findKeywordValue " + error.message); + } + ); + } + ); + +} + +const coerceIntoArray = (obj) => + Array.isArray(obj) ? obj : [obj] || []; + module.exports = router;