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

Initial Commit. All seems to be working...?

EditPage.jsx and GoogleActions.js need to be cleaned up and shortened...
This commit is contained in:
Trevor Buckner
2020-10-05 23:33:15 -04:00
parent 2065ff80ff
commit 35e1ce0df2
19 changed files with 1148 additions and 108 deletions

View File

@@ -2,6 +2,7 @@ const _ = require('lodash');
const HomebrewModel = require('./homebrew.model.js').model;
const router = require('express').Router();
const zlib = require('zlib');
const GoogleActions = require('./googleActions.js');
// const getTopBrews = (cb) => {
// HomebrewModel.find().sort({ views: -1 }).limit(5).exec(function(err, brews) {
@@ -20,17 +21,22 @@ const getGoodBrewTitle = (text)=>{
};
const newBrew = (req, res)=>{
const authors = (req.account) ? [req.account.username] : [];
const brew = req.body;
brew.authors = (req.account) ? [req.account.username] : [];
const newHomebrew = new HomebrewModel(_.merge({},
req.body,
{ authors: authors }
));
if(!newHomebrew.title) {
newHomebrew.title = getGoodBrewTitle(newHomebrew.text);
if(!brew.title) {
brew.title = getGoodBrewTitle(brew.text);
}
delete brew.editId;
delete brew.shareId;
delete brew.googleId;
console.log('creating new local file using this data:');
console.log(brew);
const newHomebrew = new HomebrewModel(brew);
console.log('this is the new local homebrew');
console.log(newHomebrew);
// 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
@@ -41,11 +47,17 @@ const newBrew = (req, res)=>{
console.error(err, err.toString(), err.stack);
return res.status(500).send(`Error while creating new brew, ${err.toString()}`);
}
return res.json(obj);
console.log('NEW BREW. gOING TO RETURN THIS:');
obj = obj.toObject();
obj.gDrive = false;
console.log(obj);
return res.status(200).send(obj);
});
};
const updateBrew = (req, res)=>{
console.log('UPDATE LOCAL');
HomebrewModel.get({ editId: req.params.id })
.then((brew)=>{
brew = _.merge(brew, req.body);
@@ -62,8 +74,13 @@ const updateBrew = (req, res)=>{
brew.markModified('authors');
brew.markModified('systems');
console.log('saving this brew');
console.log(brew);
brew.save((err, obj)=>{
if(err) throw err;
console.log('sending this updated brew:');
console.log(obj);
return res.status(200).send(obj);
});
})
@@ -103,11 +120,41 @@ const deleteBrew = (req, res)=>{
});
};
newGoogleBrew = async (req, res, next)=>{
let oAuth2Client;
console.log('newGoogleBrew (API)');
try { oAuth2Client = GoogleActions.authCheck(req.account, res); } catch (err) { return res.status(err.status).send(err.message); }
const brew = req.body;
brew.authors = (req.account) ? [req.account.username] : [];
if(!brew.title) {
brew.title = getGoodBrewTitle(brew.text);
}
delete brew.editId;
delete brew.shareId;
delete brew.googleId;
req.body = brew;
console.log(oAuth2Client);
const newHomebrew = await GoogleActions.newGoogleBrew(oAuth2Client, brew);
return res.status(200).send(newHomebrew);
};
router.post('/api', newBrew);
router.post('/api/newGoogle/', newGoogleBrew);
router.put('/api/:id', updateBrew);
router.put('/api/update/:id', updateBrew);
router.put('/api/updateGoogle/:id', (req, res)=>{GoogleActions.updateGoogleBrew(req, res);});
router.delete('/api/:id', deleteBrew);
router.get('/api/remove/:id', deleteBrew);
router.get('/api/removeGoogle/:id', (req, res)=>{GoogleActions.deleteGoogleBrew(req, res, req.params.id);});
module.exports = router;