0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-12 11:02:42 +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:
Timothy Cyrus
2020-03-11 16:26:43 -04:00
committed by Trevor Buckner
parent 2745a4d6c1
commit 80db261c88

View File

@@ -3,127 +3,128 @@ const HomebrewModel = require('./homebrew.model.js').model;
const router = require('express').Router(); const router = require('express').Router();
const zlib = require('zlib'); const zlib = require('zlib');
// const getTopBrews = (cb)=>{ // const getTopBrews = (cb) => {
// HomebrewModel.find().sort({ views: -1 }).limit(5).exec(function(err, brews) { // HomebrewModel.find().sort({ views: -1 }).limit(5).exec(function(err, brews) {
// cb(brews); // cb(brews);
// }); // });
// }; // };
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)=>{ return _.find(text.split('\n'), line => line);
return line;
});
} }
}; };
const newBrew = (req, res) => {
let authors = (req.account) ? [req.account.username] : [];
router.post('/api', (req, res)=>{
let authors = [];
if(req.account) authors = [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);
} }
newHomebrew.textBin = zlib.deflateRawSync(newHomebrew.text); // Compress brew text to binary before saving // Compress brew text to binary before saving
newHomebrew.text = undefined; // Delete the non-binary text field since it's not needed anymore 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)=>{ 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()}`);
} }
return res.json(obj); 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 }) HomebrewModel.get({ editId: req.params.id })
.then((brew)=>{ .then((brew) => {
brew = _.merge(brew, req.body); brew = _.merge(brew, req.body);
brew.textBin = zlib.deflateRawSync(req.body.text); // Compress brew text to binary before saving // Compress brew text to binary before saving
brew.text = undefined; // Delete the non-binary text field since it's not needed anymore 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(); 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('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.log(err); console.error(err);
return res.status(500).send('Error while saving'); return res.status(500).send('Error while saving');
}); });
}); };
router.get('/api/remove/:id', (req, res)=>{ router.put('/api/update/:id', updateBrew);
HomebrewModel.find({ editId: req.params.id }, (err, objs)=>{ router.put('/api/:id', updateBrew);
if(!objs.length || err) return res.status(404).send('Can not find homebrew with that id');
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]; const brew = objs[0];
// Remove current user as author // Remove current user as author
if(req.account){ if (req.account) {
brew.authors = _.pull(brew.authors, req.account.username); brew.authors = _.pull(brew.authors, req.account.username);
brew.markModified('authors'); brew.markModified('authors');
} }
// Delete brew if there are no authors left if (brew.authors.length === 0) {
if(!brew.authors.length) // 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();
}); });
// Otherwise, save the brew with updated author list } else {
else // 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.delete('/api/:id', deleteBrew);
module.exports = router; module.exports = router;
/* /*
module.exports = function(app) {
module.exports = function(app){
app; app;
app.get('/api/search', mw.adminOnly, function(req, res) {
app.get('/api/search', mw.adminOnly, function(req, res){
var page = req.query.page || 0; var page = req.query.page || 0;
var count = req.query.count || 20; var count = req.query.count || 20;
var query = {}; var query = {};
if(req.query && req.query.id){ if (req.query && req.query.id) {
query = { query = {
"$or" : [{ "$or": [{
editId : req.query.id editId : req.query.id
},{ }, {
shareId : req.query.id shareId : req.query.id
}] }]
}; };
@@ -134,21 +135,17 @@ module.exports = function(app){
}, { }, {
skip: page*count, skip: page*count,
limit: count*1 limit: count*1
}, function(err, objs){ }, function(err, objs) {
if(err) console.log(err); if (err) console.error(err);
return res.json({ return res.json({
page : page, page : page,
count : count, count : count,
total : homebrewTotal, total : homebrewTotal,
brews : objs brews : objs
}); });
}); });
}) })
return app; return app;
} }
*/ */