From bf6da8e1039b97e925700ed9c12f84b67c591cee Mon Sep 17 00:00:00 2001 From: Tobias Date: Sun, 3 Mar 2019 00:51:16 +0000 Subject: [PATCH] Allow optional min & max price, add rep calculation, move type as first item in pipeline if available --- src/controllers/search.controller.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/controllers/search.controller.js b/src/controllers/search.controller.js index 7ae3233..7757068 100644 --- a/src/controllers/search.controller.js +++ b/src/controllers/search.controller.js @@ -1,5 +1,5 @@ const Post = require('../model/post') -const {handleErr} = require('../utils') +const {calcRep} = require('../utils') const steem = require('steem') exports.search = (req, res) => { @@ -42,14 +42,16 @@ exports.search = (req, res) => { ) } - let matchOptions = {$match: { - price: { $lte: maxPrice, $gte: minPrice } - }} + let matchOptions = {$match: {}} if (type) { matchOptions.$match['type'] = type } + if (minPrice && maxPrice) { + matchOptions.$match['price'] = { $lte: maxPrice, $gte: minPrice } + } + if (currency && currency.toLowerCase() !== 'any') { matchOptions.$match['currency'] = currency } @@ -78,6 +80,8 @@ exports.search = (req, res) => { }} ) + console.log(JSON.stringify(pipeline)) + try { Post.aggregate(pipeline).exec((err, result) => { if (!err) { @@ -90,6 +94,7 @@ exports.search = (req, res) => { steem.api.getContent(singleResult.author, singleResult.permlink, function (err, post) { if (!err) { post.json_metadata = JSON.parse(post.json_metadata) + post.rep = calcRep(post.author_reputation) const mergedResults = {...dbData, ...post} combinedResults.push(mergedResults) if (combinedResults.length === searchData.length) {