2019-08-01 07:42:25 +00:00
|
|
|
const router = require('express').Router();
|
|
|
|
|
|
|
|
const Users = require('../users/usersModel');
|
|
|
|
|
2019-08-01 23:18:56 +00:00
|
|
|
// TODO: add validation
|
2019-08-01 19:12:20 +00:00
|
|
|
router.get('/:id', (req, res) => {
|
2019-08-01 14:25:44 +00:00
|
|
|
Users.findUserById(req.params.id)
|
2019-08-01 07:42:25 +00:00
|
|
|
.then(users => {
|
|
|
|
res.status(200).json(users);
|
|
|
|
})
|
|
|
|
.catch(err => res.send(err));
|
|
|
|
});
|
|
|
|
|
2019-08-01 19:12:20 +00:00
|
|
|
router.put('/:id', async (req, res) => {
|
2019-08-01 14:25:44 +00:00
|
|
|
Users.updateUser(req.params.id, req.body)
|
|
|
|
.then(user => {
|
2019-08-01 07:42:25 +00:00
|
|
|
if (user) {
|
|
|
|
res.status(200).json(user);
|
|
|
|
} else {
|
|
|
|
res.status(404).json({ message: 'The user could not be found' });
|
|
|
|
}
|
|
|
|
})
|
2019-08-01 20:31:44 +00:00
|
|
|
.catch(() => res.status(500).json({ message: 'Error updating the user' }));
|
2019-08-01 07:42:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
router.delete("/:id", (req, res) => {
|
2019-08-01 14:25:44 +00:00
|
|
|
Users.removeUser(req.params.id)
|
2019-08-01 20:31:44 +00:00
|
|
|
.then(() => res.status(200).json({ message: 'The user was successfully deleted.'}))
|
2019-08-01 07:42:25 +00:00
|
|
|
.catch(err => res.json(err));
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|