0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 14:12:40 +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('# ');
if (titlePos !== -1) {
if(titlePos !== -1) {
const ending = text.indexOf('\n', titlePos);
return text.substring(titlePos + 2, ending);
} else {
return _.find(text.split('\n'), line => line);
return _.find(text.split('\n'), (line)=>line);
}
};
const newBrew = (req, res) => {
let authors = (req.account) ? [req.account.username] : [];
const newBrew = (req, res)=>{
const authors = (req.account) ? [req.account.username] : [];
const newHomebrew = new HomebrewModel(_.merge({},
req.body,
{ authors: authors }
));
if (!newHomebrew.title) {
if(!newHomebrew.title) {
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
newHomebrew.text = undefined;
newHomebrew.save((err, obj) => {
if (err) {
newHomebrew.save((err, obj)=>{
if(err) {
console.error(err, err.toString(), err.stack);
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 })
.then((brew) => {
.then((brew)=>{
brew = _.merge(brew, req.body);
// Compress brew text to binary before saving
brew.textBin = zlib.deflateRawSync(req.body.text);
@@ -57,57 +55,59 @@ const updateBrew = (req, res) => {
brew.text = undefined;
brew.updatedAt = new Date();
if (req.account) {
if(req.account) {
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
}
brew.markModified('authors');
brew.markModified('systems');
brew.save((err, obj) => {
if (err) throw err;
brew.save((err, obj)=>{
if(err) throw err;
return res.status(200).send(obj);
});
})
.catch((err) => {
.catch((err)=>{
console.error(err);
return res.status(500).send('Error while saving');
});
};
router.put('/api/update/:id', updateBrew);
router.put('/api/:id', updateBrew);
const deleteBrew = (req, res) => {
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');
}
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.markModified('authors');
}
if (brew.authors.length === 0) {
if(brew.authors.length === 0) {
// Delete brew if there are no authors left
brew.remove((err) => {
if (err) return res.status(500).send('Error while removing');
brew.remove((err)=>{
if(err) return res.status(500).send('Error while removing');
return res.status(200).send();
});
} else {
// Otherwise, save the brew with updated author list
brew.save((err, savedBrew) => {
if (err) throw err;
brew.save((err, savedBrew)=>{
if(err) throw err;
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.get('/api/remove/:id', deleteBrew);
module.exports = router;