sleep-tracker-backend/services/users/usersRouter.js

33 lines
827 B
JavaScript
Raw Normal View History

2019-08-01 07:42:25 +00:00
const router = require('express').Router();
const Users = require('../users/usersModel');
// TODO: add validation
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));
});
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;