0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-14 12:52:42 +00:00

Small tweaks and linting

This commit is contained in:
Trevor Buckner
2020-04-14 11:08:54 -04:00
parent 80db261c88
commit 70430f84e1

View File

@@ -9,25 +9,25 @@ const zlib = require('zlib');
// }); // });
// }; // };
const getGoodBrewTitle = (text) => { const getGoodBrewTitle = (text)=>{
const titlePos = text.indexOf('# '); const titlePos = text.indexOf('# ');
if (titlePos !== -1) { if(titlePos !== -1) {
const ending = text.indexOf('\n', titlePos); const ending = text.indexOf('\n', titlePos);
return text.substring(titlePos + 2, ending); return text.substring(titlePos + 2, ending);
} else { } else {
return _.find(text.split('\n'), line => line); return _.find(text.split('\n'), (line)=>line);
} }
}; };
const newBrew = (req, res) => { const newBrew = (req, res)=>{
let authors = (req.account) ? [req.account.username] : []; const authors = (req.account) ? [req.account.username] : [];
const newHomebrew = new HomebrewModel(_.merge({}, const newHomebrew = new HomebrewModel(_.merge({},
req.body, req.body,
{ authors: authors } { authors: authors }
)); ));
if (!newHomebrew.title) { if(!newHomebrew.title) {
newHomebrew.title = getGoodBrewTitle(newHomebrew.text); newHomebrew.title = getGoodBrewTitle(newHomebrew.text);
} }
@@ -36,8 +36,8 @@ const newBrew = (req, res) => {
// Delete the non-binary text field since it's not needed anymore // Delete the non-binary text field since it's not needed anymore
newHomebrew.text = undefined; newHomebrew.text = undefined;
newHomebrew.save((err, obj) => { newHomebrew.save((err, obj)=>{
if (err) { if(err) {
console.error(err, err.toString(), err.stack); console.error(err, err.toString(), err.stack);
return res.status(500).send(`Error while creating new brew, ${err.toString()}`); return res.status(500).send(`Error while creating new brew, ${err.toString()}`);
} }
@@ -45,11 +45,9 @@ const newBrew = (req, res) => {
}); });
}; };
router.post('/api', newBrew); const updateBrew = (req, res)=>{
const updateBrew = (req, res) => {
HomebrewModel.get({ editId: req.params.id }) HomebrewModel.get({ editId: req.params.id })
.then((brew) => { .then((brew)=>{
brew = _.merge(brew, req.body); brew = _.merge(brew, req.body);
// Compress brew text to binary before saving // Compress brew text to binary before saving
brew.textBin = zlib.deflateRawSync(req.body.text); brew.textBin = zlib.deflateRawSync(req.body.text);
@@ -57,57 +55,59 @@ const updateBrew = (req, res) => {
brew.text = undefined; brew.text = undefined;
brew.updatedAt = new Date(); brew.updatedAt = new Date();
if (req.account) { if(req.account) {
brew.authors = _.uniq(_.concat(brew.authors, req.account.username)); brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
} }
brew.markModified('authors'); brew.markModified('authors');
brew.markModified('systems'); brew.markModified('systems');
brew.save((err, obj) => { brew.save((err, obj)=>{
if (err) throw err; if(err) throw err;
return res.status(200).send(obj); return res.status(200).send(obj);
}); });
}) })
.catch((err) => { .catch((err)=>{
console.error(err); console.error(err);
return res.status(500).send('Error while saving'); return res.status(500).send('Error while saving');
}); });
}; };
router.put('/api/update/:id', updateBrew); const deleteBrew = (req, res)=>{
router.put('/api/:id', updateBrew); HomebrewModel.find({ editId: req.params.id }, (err, objs)=>{
if(!objs.length || err) {
const deleteBrew = (req, res) => {
HomebrewModel.find({ editId: req.params.id }, (err, objs) => {
if (!objs.length || err)
return res.status(404).send('Can not find homebrew with that id'); return res.status(404).send('Can not find homebrew with that id');
}
const brew = objs[0]; const brew = objs[0];
// Remove current user as author if(req.account) {
if (req.account) { // Remove current user as author
brew.authors = _.pull(brew.authors, req.account.username); brew.authors = _.pull(brew.authors, req.account.username);
brew.markModified('authors'); brew.markModified('authors');
} }
if (brew.authors.length === 0) { if(brew.authors.length === 0) {
// Delete brew if there are no authors left // Delete brew if there are no authors left
brew.remove((err) => { brew.remove((err)=>{
if (err) return res.status(500).send('Error while removing'); if(err) return res.status(500).send('Error while removing');
return res.status(200).send(); return res.status(200).send();
}); });
} else { } else {
// Otherwise, save the brew with updated author list // Otherwise, save the brew with updated author list
brew.save((err, savedBrew) => { brew.save((err, savedBrew)=>{
if (err) throw err; if(err) throw err;
return res.status(200).send(savedBrew); return res.status(200).send(savedBrew);
}); });
} }
}); });
}; };
router.get('/api/remove/:id', deleteBrew); router.post('/api', newBrew);
router.put('/api/:id', updateBrew);
router.put('/api/update/:id', updateBrew);
router.delete('/api/:id', deleteBrew); router.delete('/api/:id', deleteBrew);
router.get('/api/remove/:id', deleteBrew);
module.exports = router; module.exports = router;