0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-03-24 19:28:11 +00:00

Removing invalid brews is working

This commit is contained in:
Scott Tolksdorf
2017-01-10 17:20:33 -05:00
parent 0878439750
commit 728277f861
8 changed files with 159 additions and 28 deletions

View File

@@ -3,10 +3,21 @@ const router = require('express').Router();
const vitreumRender = require('vitreum/steps/render');
const templateFn = require('../client/template.js');
const config = require('nconf');
const Moment = require('moment');
const mw = require('./middleware.js');
const BrewData = require('./brew.data.js');
const getInvalidBrewQuery = ()=>{
return BrewData.model.find({
'$where' : "this.text.length < 140",
createdAt: {
$lt: Moment().subtract(3, 'days').toDate()
}
}).select({ text : false });
}
router.get('/admin', mw.adminLogin, (req, res, next) => {
return vitreumRender('admin', templateFn, {
url : req.originalUrl,
@@ -19,12 +30,17 @@ router.get('/admin', mw.adminLogin, (req, res, next) => {
});
//Removes all empty brews that are older than 3 days and that are shorter than a tweet
router.get('/admin/invalid', mw.adminOnly, (req, res, next)=>{
getInvalidBrewQuery().exec()
.then((brews) => {
return res.json(brews);
})
.catch(next);
});
router.delete('/admin/invalid', mw.adminOnly, (req, res, next)=>{
BrewData.removeInvalid(!!req.query.do_it)
.then((removedCount) => {
return res.join({
count : removedCount
});
getInvalidBrewQuery().remove()
.then(()=>{
return res.status(200).send();
})
.catch(next);
});

View File

@@ -30,15 +30,12 @@ router.post('/api/brew', (req, res, next)=>{
//Update
router.put('/api/brew/:editId', mw.loadBrew, (req, res, next)=>{
console.log(req.account);
const brew = req.body || {};
if(req.account){
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
}
console.log(brew);
BrewData.update(req.params.editId, brew)
.then((brew) => {
console.log(brew.toJSON());
return res.json(brew.toJSON());
})
.catch(next);

View File

@@ -93,21 +93,6 @@ const BrewData = {
return BrewData.get({ editId });
},
//Removes all empty brews that are older than 3 days and that are shorter than a tweet
removeInvalid : (force = false) => {
const invalidBrewQuery = Brew.find({
'$where' : "this.text.length < 140",
createdAt: {
$lt: Moment().subtract(3, 'days').toDate()
}
});
if(force) return invalidBrewQuery.remove().exec();
return invalidBrewQuery.exec()
.then((objs) => {
return objs.length;
});
},
search : (query, req={}) => {
//defaults with page and count
//returns a non-text version of brews