Skip to content

Commit

Permalink
Merge pull request #5 from tobias-g1/develop
Browse files Browse the repository at this point in the history
Enhanced search API changes
  • Loading branch information
surpassinggoogle committed Feb 26, 2019
2 parents 39b9dfc + ca4c38f commit a99368f
Showing 1 changed file with 114 additions and 30 deletions.
144 changes: 114 additions & 30 deletions src/controllers/search.controller.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,119 @@
const Post = require('../model/post')
const {stringify, handleErr} = require('../utils')
const {handleErr} = require('../utils')
const steem = require('steem')

exports.search = (req, res) => {
let {query} = req.body
let limit = req.params.limit || 50
if (query) {
console.log('query:', stringify(query))
let {searchText} = query
console.log(searchText, Boolean(searchText))
if (searchText.trim().length === 0 || searchText.trim() === '.' || searchText.trim().toLowerCase() === 'steemgigs') {
Post.find({type: 'steemgigs_post'}).select('').limit(limit).exec((err, result) => {
if (!err) {
console.log(JSON.stringify(result, null, 2))
res.status(200).send(result)
} else {
handleErr(err, res, 'empty result')
}
})
} else if (searchText.trim().length > 0) {
Post.find({type: 'steemgigs_post', $text: {$search: searchText.trim()}}, { score: {$meta: 'textScore'} }).select('').limit(limit).exec((err, result) => {
if (!err) {
console.log(JSON.stringify(result, null, 2))
res.status(200).send(result)
} else {
handleErr(err, res, 'empty result')
}
})
} else {
res.status(400).send('please supply a proper query')
}
} else {
res.status(400).send('please supply a proper query')
const {searchText, type, category, subcategory, currency, minPrice, maxPrice, pageNumber, order, limit} = req.body.query
const skipCount = limit * (pageNumber - 1)
let searchResult = {}
let sortMethod = {}
// Set sort method based on request
switch (order.toLowerCase()) {
case 'oldest':
sortMethod = {
_id: 1
}
break
case 'newest':
sortMethod = {
_id: -1
}
break
case 'price_low':
sortMethod = {
price: 1
}
break
case 'price_high':
sortMethod = {
price: -1
}
break
}

// Define pipeline based on request provided parameters

let pipeline = []

if (searchText) {
pipeline = pipeline.concat(
{$match: {$text: {$search: searchText.trim()}}},
{$addFields: {score: {$meta: 'textScore'}}}
)
}

let matchOptions = {$match: {
price: { $lte: maxPrice, $gte: minPrice }
}}

if (type) {
matchOptions.$match['type'] = type
}

if (currency && currency.toLowerCase() !== 'any') {
matchOptions.$match['currency'] = currency
}

if (category && category.toLowerCase() !== 'any') {
matchOptions.$match['category'] = category
}

if (subcategory && subcategory.toLowerCase() !== 'any') {
matchOptions.$match['subcategory'] = subcategory
}

pipeline.push(matchOptions)

// Add facet for search result data, pagination & post count
pipeline.push(
{ '$facet': {
'search_data': [
{'$sort': sortMethod},
{'$skip': skipCount},
{'$limit': limit}
],
'post_count': [
{ $count: 'count' }
]
}}
)

try {
Post.aggregate(pipeline).exec((err, result) => {
if (!err) {
const getBlockchainData = new Promise(function (resolve, reject) {
let combinedResults = []
const searchData = result[0].search_data
if (searchData.length !== 0) {
searchData.forEach(singleResult => {
const dbData = singleResult
steem.api.getContent(singleResult.author, singleResult.permlink, function (err, post) {
if (!err) {
post.json_metadata = JSON.parse(post.json_metadata)
const mergedResults = {...dbData, ...post}
combinedResults.push(mergedResults)
if (combinedResults.length === searchData.length) {
resolve(combinedResults)
}
}
})
})
} else {
resolve([])
}
})
getBlockchainData
.then(function (results) {
if (result[0].post_count[0]) {
searchResult.pages = Math.ceil(result[0].post_count[0].count / limit)
}
searchResult.results = results
res.status(200).send(searchResult)
})
}
})
} catch (err) {
res.status(500)
console.log(err)
}
}

0 comments on commit a99368f

Please sign in to comment.