0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-13 06:32:39 +00:00

MOved the brews search to its own file, writing out more tests

This commit is contained in:
Scott Tolksdorf
2017-01-27 10:36:07 -05:00
parent 2f82d3875e
commit 8e58e5aca9
6 changed files with 180 additions and 109 deletions

View File

@@ -93,63 +93,8 @@ const BrewData = {
getByEdit : (editId) => {
return BrewData.get({ editId });
},
//TODO: Add a 'core search' which just takes a search object
//TODO: extend the core search with a user search and a term search
//TODO: break these functions off into a 'brew.search.js' file
//TODO: pagniation, sorting and full access should be an 'opts' param
search : (searchTerms, pagination, sorting, fullAccess = true) => {
let query = {};
if(searchTerms){
query = {$text: {
//Wrap terms in quots to perform an AND operator
$search: _.map(searchTerms.split(' '), (term)=>{
return `\"${term}\"`;
}).join(' '),
$caseSensitive : false
}};
}
pagination = _.defaults(pagination, {
limit : 25,
page : 0
});
sorting = _.defaults(sorting, {
'views' : 1
});
let filter = {
//editId : 0,
text : 0
};
if(!fullAccess){
filter.editId = 0;
query.published = false;
}
const searchQuery = Brew
.find(query)
.sort(sorting)
.select(filter)
.limit(pagination.limit)
.skip(pagination.page * pagination.limit)
.exec();
const countQuery = Brew.count(query).exec();
return Promise.all([searchQuery, countQuery])
.then((result) => {
return {
brews : result[0],
total : result[1]
}
});
},
};
module.exports = BrewData;
const BrewSearch = require('./brew.search.js')(Brew);
module.exports = _.merge(BrewData, BrewSearch);

59
server/brew.search.js Normal file
View File

@@ -0,0 +1,59 @@
const _ = require('lodash');
module.exports = (Brew) => {
const cmds = {
termSearch : (terms='', opts, fullAccess) => {
const query = {$text: {
//Wrap terms in quotes to perform an AND operation
$search: _.map(terms.split(' '), (term)=>{
return `\"${term}\"`;
}).join(' '),
$caseSensitive : false
}};
return cmds.search(query, opts, fullAccess);
},
userSearch : (username, opts, fullAccess) => {
const query = {
authors : username
};
return cmds.search(query, opts, fullAccess);
},
search : (queryObj={}, opts={}, fullAccess = true) => {
const pagination = _.defaults(opts.pagination, {
limit : 25,
page : 0
});
const sorting = _.defaults(opts.sorting, {
'views' : 1
});
let filter = {
text : 0
};
if(!fullAccess){
filter.editId = 0;
queryObj.published = false;
}
const searchQuery = Brew
.find(queryObj)
.sort(sorting)
.select(filter)
.limit(pagination.limit)
.skip(pagination.page * pagination.limit)
.exec();
const countQuery = Brew.count(queryObj).exec();
return Promise.all([searchQuery, countQuery])
.then((result) => {
return {
brews : result[0],
total : result[1]
}
});
}
};
return cmds;
};

View File

@@ -24,5 +24,13 @@ module.exports = {
);
});
},
close : ()=>{
return new Promise((resolve, reject) => {
mongoose.connection.close(()=>{
log.info('DB connection closed.');
return resolve();
});
});
},
instance : mongoose
}