Skip to content

Commit

Permalink
created cookies, controllers, and routers
Browse files Browse the repository at this point in the history
  • Loading branch information
dwejikim committed Sep 19, 2022
1 parent cb41b76 commit b3c6f45
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 123 deletions.
36 changes: 22 additions & 14 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@babel/preset-react": "^7.18.6",
"babel-loader": "^8.2.5",
"concurrently": "^7.4.0",
"cookie-parser": "^1.4.6",
"cross-env": "^7.0.3",
"css-loader": "^6.7.1",
"eslint": "^8.23.1",
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/cookieController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const cookieController = {};

cookieController.setUserCookie = (req, res, next) => {
//save the cookie here
res.cookie('userId', req.body.userId, { httpOnly: true})
res.cookie('userId', res.locals.userId, { httpOnly: true})
next();
}

Expand Down
40 changes: 15 additions & 25 deletions server/controllers/individualController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ individualController.getChores = async(req, res, next) => {

//actual sequelize query to get all chores from personal chores list in db based on user id passed in
try {

dbRes = await models.Chore.findAll({ where: { _id: req.query.id } })

const { userId } = req.cookies;
const dbRes = await models.Chore.findAll({ where: { userid: userId } })
//async grabbing chores response from db
.then((chores) => {
console.log(dbRes[0].dataValues);
//store individuals chores into local to persist
res.locals.chores = chores.dataValues;
res.locals.chores = dbRes[0].dataValues;
//invoke next middleware
return next();
})//end of then chain
}

//catch statement for error response
catch (err) {
((err) => {
Expand All @@ -29,9 +25,10 @@ individualController.getChores = async(req, res, next) => {
});//end of error obj
})//end of catch statement
}
return next();
};//end of getchores

//this controller adds the input chore data to the chores list database
//this should update a chore to userId
individualController.addChore = async (req, res, next) => {
// let currentDate = new Date();
// let time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
Expand All @@ -41,21 +38,21 @@ individualController.addChore = async (req, res, next) => {
// const current = cDay + "/" + cMonth + "/" + cYear + ' ' + time;
//sqlize method to create an entry in personal chores list of db\
try {

const { userId } = req.cookies;
const { choreName, points, priority } = req.body;
const dbRes = await models.Chore.create({

//find the individuals chores table based on req.body.id passed in
// id: 12345,
//add an association for individualid and choreid from req.body.choreid
chorename: 'laundry',
chorename: choreName,

points: 12,
points: points,

priority: 2,
priority: priority,

userid: 123456789,
userid: userId,

familyid: 1234567910,
familyid: 1,

})//end of create sqlize

Expand All @@ -75,26 +72,20 @@ individualController.addChore = async (req, res, next) => {

//This controller deletes a chore from the db
individualController.deleteChore = (req, res, next) => {

const { id } = req.params;
//sqlize query deleting doc by user id and chore id
models.Chore.destroy({

where: {
_id: req.body.id,
chore: req.body.chore,
_id: id,
}//end of conditional query guideliens

})//end of destroy method

//handle destroy response from sequelize
.then((err, chore) => {

//save deleted chore in case needed later
res.locals.chore = chore;
//invoke next middleware
return next();
})//end of then chain

//handle errors from destroy query
.catch((err) => {
return next({
Expand All @@ -104,7 +95,6 @@ individualController.deleteChore = (req, res, next) => {
message: { err: 'An err occurred' },
});//end of error obj
});//end of catch

};//end of deletechore

module.exports = individualController;
88 changes: 32 additions & 56 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,38 @@
const models = require('../sequelize/models/index');
const { User } = require('../sequelize/models/index');

const userController = {};

//this controller adds the input user to the database
userController.addUser = (req, res, next) => {

//create user with passed in id
models.User.create({

//create user with id passed in from body
userid: req.body.id,
//assign a unique chores list
user_chore_list_id: req.body.id,

})//end of user create

//handle successful return
.then((user) => {
res.locals.user = user;
//invoke next middleware
return next();
})//end of then

//handle any errors from query
.catch((err) => {
//return error obj
if (err) return next({
log: 'Middleware error. userController addUser',
userController.login = async (req, res, next) => {
try{
const [user, created] = await User.findOrCreate({
where: { googleid: req.body.userId },
defaults: { googleid: req.body.userId, familyid: 1}
});
res.locals.googleId = req.body.userId;
res.locals.userId = user.dataValues.id;
res.locals.userInfo = user.dataValues;
}catch (err) {
//handle error response from create
return next({
log: 'Middleware error. usercontroller.login',
message: { err: 'An err occurred' },
});//end of return err obj
});//end of catch statement

}//end of adduser

userController.deleteUser = (req, res, next) => {
//get user id from req

//delete user using sql query finding id
models.User.destroy({ where: { userid: req.body.id } })

//handle success
.then((user) => {
//return deleted user for future needs
res.locals.user = user;
//invoke next middleware
return next();
})

//handle error
.catch((err) => {
//return err
return next({
log: 'Middleware error. choresController deletechore',
status: 403,
message: { err: 'An err occurred' },
});//end of err obj
});//end of catch

};//end of deleteuser method
});
}
return next();
}


userController.checkCookie = async (req, res, next) => {
try {
const userId = await User.findOne({ where: { id: req.cookies.userId }})
res.locals.userInfo = userId.dataValues;
}catch (err) {
return next({
log: 'Middleware error. userController.checkCookie',
message: { err: 'An err occurred' },
});
}
return next();
}

module.exports = userController;
2 changes: 0 additions & 2 deletions server/router/family.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ router.delete('/:id', familyController.deleteChore, (req, res) => {
res.status(200).json([{"success": "success"}])
});



module.exports = router;
5 changes: 1 addition & 4 deletions server/router/individual.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ const router = express.Router();

router.get('/', individualController.getChores, (req, res) => {
// console.log('histories', res.locals.histories)
res.status(200).send(res.locals.person)
res.status(200).send(res.locals.chores)
});

router.post('/', individualController.addChore, (req, res) => {

res.status(200).json([{"success": "success"}])
});

router.delete('/:id', individualController.deleteChore, (req, res) => {
res.status(200).json([{"success": "success"}])
});



module.exports = router;
26 changes: 18 additions & 8 deletions server/router/user.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
const express = require('express');
const choresController = require('../controllers/familyController');
const userController = require('../controllers/userController');
const cookieController = require('../controllers/cookieController');

const router = express.Router();

router.get('/:id', choresController.getChores, (req, res) => {
// console.log('histories', res.locals.histories)
res.status(200).json(res.locals.histories)
router.post('/login', userController.login, cookieController.setUserCookie, (req, res) => {
res.status(200).json(res.locals.userInfo)
});

router.post('/:id', choresController.addChore, (req, res) => {
res.status(200).json([{"success": "success"}])
router.get('/home', userController.checkCookie, (req, res) => {
res.status(200).json(res.locals.userInfo)
});

router.delete('/:id', choresController.deleteChore, (req, res) => {
res.status(200).json([{"success": "success"}])
});
// router.get('/:id', choresController.getChores, (req, res) => {
// // console.log('histories', res.locals.histories)
// res.status(200).json(res.locals.histories)
// });

// router.post('/:id', choresController.addChore, (req, res) => {
// res.status(200).json([{"success": "success"}])
// });

// router.delete('/:id', choresController.deleteChore, (req, res) => {
// res.status(200).json([{"success": "success"}])
// });


module.exports = router;
Loading

0 comments on commit b3c6f45

Please sign in to comment.