Skip to content

Commit

Permalink
Added api to find keywordValue
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsah committed Jan 4, 2018
1 parent c6fc323 commit 108d377
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions apiroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 108d377

Please sign in to comment.