0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-02 19:22:47 +00:00

Added in middleware for retriving brews by a user

This commit is contained in:
Scott Tolksdorf
2016-11-23 23:41:39 -05:00
parent 1db24d07bd
commit 750f5c1330
5 changed files with 62 additions and 18 deletions

View File

@@ -16,8 +16,6 @@ refreshCount();
const getTopBrews = (cb)=>{
HomebrewModel.find().sort({views: -1}).limit(5).exec(function(err, brews) {
cb(brews);
@@ -39,7 +37,10 @@ const getGoodBrewTitle = (text) => {
router.post('/api', (req, res)=>{
const newHomebrew = new HomebrewModel(req.body);
const newHomebrew = new HomebrewModel(_.merge({},
req.body,
{authors : [req.account.username]}
));
if(!newHomebrew.title){
newHomebrew.title = getGoodBrewTitle(newHomebrew.text);
}
@@ -57,6 +58,7 @@ router.put('/api/update/:id', (req, res)=>{
.then((brew)=>{
brew = _.merge(brew, req.body);
brew.updatedAt = new Date();
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
brew.save((err, obj)=>{
if(err) throw err;
return res.status(200).send(obj);

View File

@@ -55,6 +55,22 @@ HomebrewSchema.statics.get = function(query){
});
};
HomebrewSchema.statics.getByUser = function(username, allowAccess=false){
return new Promise((resolve, reject) => {
let query = {authors : username, published : true};
if(allowAccess){
delete query.published;
}
Homebrew.find(query, (err, brews)=>{
if(err) return reject('Can not find brew');
return resolve(_.map(brews, (brew)=>{
return brew.sanatize(!allowAccess);
}));
});
});
};
var Homebrew = mongoose.model('Homebrew', HomebrewSchema);