forked from Team-Wunderpuss-Photogenicus/taskful
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created cookies, controllers, and routers
- Loading branch information
Showing
11 changed files
with
148 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.