UNPKG

paradigm-core

Version:
88 lines (65 loc) 1.69 kB
const {logger, RootController, r} = require('structure-core') /** * SearchController Class * * @public * @class SearchController */ class SearchController extends RootController { /** * SearchController constructor * * @public * @constructor * @param {Object} options - Options */ constructor(options = {}) { super(Object.assign({}, { name: 'search' }, options)) } /** * Search * * @public * @param {Object} req - Express req * @param {Object} res - Express res */ search(req, res) { const applicationId = req.headers.applicationid // Search items const title = req.query.title || '' return new Promise( async (resolve, reject) => { try { const result = await r .table('documents') .between( [applicationId, r.minval], [applicationId, r.maxval], {index: 'link_application_updated'} ) .orderBy({index: r.desc('link_application_updated')}) .filter(function(doc) { return doc('applicationId').eq(applicationId).and( doc('title').match(`(?i)${title}`) ) }) .limit(20) .eqJoin('userId', r.table('users')) .orderBy(r.desc(r.row('left')('updatedAt'))) const results = [] for(let i = 0, l = result.length; i < l; i++) { const doc = result[i].left const user = result[i].right doc.user = user results.push(doc) } resolve(results) } catch(e) { logger.error(e) reject(e) } }) } } module.exports = SearchController