mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-22 09:37:53 +00:00
API Code Cleanup + Alt Endpoints
Added 2 new API Endpoints as alternatives to existing ones: - PUT /api/:id (same as PUT /api/update/:id) - DELETE /api/:id (same as GET /api/remove/:id)
This commit is contained in:
committed by
Trevor Buckner
parent
2745a4d6c1
commit
80db261c88
@@ -15,18 +15,12 @@ const getGoodBrewTitle = (text)=>{
|
||||
const ending = text.indexOf('\n', titlePos);
|
||||
return text.substring(titlePos + 2, ending);
|
||||
} else {
|
||||
return _.find(text.split('\n'), (line)=>{
|
||||
return line;
|
||||
});
|
||||
return _.find(text.split('\n'), line => line);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
router.post('/api', (req, res)=>{
|
||||
|
||||
let authors = [];
|
||||
if(req.account) authors = [req.account.username];
|
||||
const newBrew = (req, res) => {
|
||||
let authors = (req.account) ? [req.account.username] : [];
|
||||
|
||||
const newHomebrew = new HomebrewModel(_.merge({},
|
||||
req.body,
|
||||
@@ -37,8 +31,10 @@ router.post('/api', (req, res)=>{
|
||||
newHomebrew.title = getGoodBrewTitle(newHomebrew.text);
|
||||
}
|
||||
|
||||
newHomebrew.textBin = zlib.deflateRawSync(newHomebrew.text); // Compress brew text to binary before saving
|
||||
newHomebrew.text = undefined; // Delete the non-binary text field since it's not needed anymore
|
||||
// Compress brew text to binary before saving
|
||||
newHomebrew.textBin = zlib.deflateRawSync(newHomebrew.text);
|
||||
// Delete the non-binary text field since it's not needed anymore
|
||||
newHomebrew.text = undefined;
|
||||
|
||||
newHomebrew.save((err, obj) => {
|
||||
if (err) {
|
||||
@@ -47,17 +43,23 @@ router.post('/api', (req, res)=>{
|
||||
}
|
||||
return res.json(obj);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
router.put('/api/update/:id', (req, res)=>{
|
||||
router.post('/api', newBrew);
|
||||
|
||||
const updateBrew = (req, res) => {
|
||||
HomebrewModel.get({ editId: req.params.id })
|
||||
.then((brew) => {
|
||||
brew = _.merge(brew, req.body);
|
||||
brew.textBin = zlib.deflateRawSync(req.body.text); // Compress brew text to binary before saving
|
||||
brew.text = undefined; // Delete the non-binary text field since it's not needed anymore
|
||||
// Compress brew text to binary before saving
|
||||
brew.textBin = zlib.deflateRawSync(req.body.text);
|
||||
// Delete the non-binary text field since it's not needed anymore
|
||||
brew.text = undefined;
|
||||
brew.updatedAt = new Date();
|
||||
|
||||
if(req.account) brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
|
||||
if (req.account) {
|
||||
brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
|
||||
}
|
||||
|
||||
brew.markModified('authors');
|
||||
brew.markModified('systems');
|
||||
@@ -68,14 +70,18 @@ router.put('/api/update/:id', (req, res)=>{
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
console.error(err);
|
||||
return res.status(500).send('Error while saving');
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
router.get('/api/remove/:id', (req, res)=>{
|
||||
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) return res.status(404).send('Can not find homebrew with that id');
|
||||
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
|
||||
@@ -84,37 +90,32 @@ router.get('/api/remove/:id', (req, res)=>{
|
||||
brew.markModified('authors');
|
||||
}
|
||||
|
||||
if (brew.authors.length === 0) {
|
||||
// Delete brew if there are no authors left
|
||||
if(!brew.authors.length)
|
||||
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
|
||||
else
|
||||
brew.save((err, savedBrew) => {
|
||||
if (err) throw err;
|
||||
return res.status(200).send(savedBrew);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
router.get('/api/remove/:id', deleteBrew);
|
||||
router.delete('/api/:id', deleteBrew);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
/*
|
||||
|
||||
|
||||
|
||||
module.exports = function(app) {
|
||||
|
||||
app;
|
||||
|
||||
|
||||
|
||||
|
||||
app.get('/api/search', mw.adminOnly, function(req, res) {
|
||||
|
||||
var page = req.query.page || 0;
|
||||
var count = req.query.count || 20;
|
||||
|
||||
@@ -135,20 +136,16 @@ module.exports = function(app){
|
||||
skip: page*count,
|
||||
limit: count*1
|
||||
}, function(err, objs) {
|
||||
if(err) console.log(err);
|
||||
if (err) console.error(err);
|
||||
return res.json({
|
||||
page : page,
|
||||
count : count,
|
||||
total : homebrewTotal,
|
||||
brews : objs
|
||||
});
|
||||
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
return app;
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user