1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-19 01:01:28 +00:00

added the search api with pagnination, and added a remove invalid brew endpoint to the admin

This commit is contained in:
Scott Tolksdorf
2016-04-20 01:02:44 -04:00
parent 8688b99bdf
commit f35950c2c4
5 changed files with 162 additions and 98 deletions

View File

@@ -2,7 +2,23 @@ var _ = require('lodash');
var Moment = require('moment');
var HomebrewModel = require('./homebrew.model.js').model;
var changelogText = require('fs').readFileSync('./changelog.md', 'utf8');
var homebrewTotal = 0;
var refreshCount = function(){
HomebrewModel.count({}, function(err, total){
homebrewTotal = total;
});
};
refreshCount()
var mw = {
adminOnly : function(req, res, next){
if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
next();
}else{
return res.status(401).send('Access denied');
}
}
};
var getTopBrews = function(cb){
@@ -12,7 +28,6 @@ var getTopBrews = function(cb){
}
module.exports = function(app){
app.get('/homebrew/top', function(req, res){
@@ -21,7 +36,6 @@ module.exports = function(app){
});
})
//Updating
app.put('/homebrew/update/:id', function(req, res){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
@@ -35,39 +49,65 @@ module.exports = function(app){
});
});
app.get('/homebrew/remove/:id', function(req, res){
//if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
console.log(err);
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
var resEntry = objs[0];
resEntry.remove(function(err){
if(err) return res.status(500).send("Error while removing");
return res.status(200).send();
})
});
//}else{
// return res.status(401).send('Access denied');
//}
app.get('/homebrew/api/remove/:id', function(req, res){
HomebrewModel.find({editId : req.params.id}, function(err, objs){
if(!objs.length || err) return res.status(404).send("Can not find homebrew with that id");
var resEntry = objs[0];
resEntry.remove(function(err){
if(err) return res.status(500).send("Error while removing");
return res.status(200).send();
})
});
});
//Removes all empty brews that are older than 3 days
app.get('/homebrew/clear_old', function(req, res){
if(req.query && req.query.admin_key == process.env.ADMIN_KEY){
HomebrewModel.remove({
text : '',
createdAt: {
$lt: Moment().subtract(3, 'days').toDate()
}
}, function(err, objs){
return res.status(200).send();
});
//Removes all empty brews that are older than 3 days and that are shorter than a tweet
app.get('/homebrew/api/invalid', mw.adminOnly, function(req, res){
var invalidBrewQuery = HomebrewModel.find({
'$where' : "this.text.length < 140",
createdAt: {
$lt: Moment().subtract(3, 'days').toDate()
}
});
if(req.query.do_it){
invalidBrewQuery.remove().exec((err, objs)=>{
refreshCount();
return res.send(200);
})
}else{
return res.status(401).send('Access denied');
invalidBrewQuery.exec((err, objs)=>{
if(err) console.log(err);
return res.json({
count : objs.length
})
})
}
});
app.get('/homebrew/api/search', mw.adminOnly, function(req, res){
var page = req.query.page || 0;
var count = req.query.count || 20;
HomebrewModel.find({}, {
text : 0 //omit the text
}, {
skip: page*count,
limit: count*1
}, function(err, objs){
if(err) console.log(err);
return res.json({
page : page,
count : count,
total : homebrewTotal,
brews : objs
});
});
})
return app;
}