33 lines
827 B
JavaScript
33 lines
827 B
JavaScript
const router = require('express').Router();
|
|
|
|
const Users = require('../users/usersModel');
|
|
|
|
// TODO: add validation
|
|
router.get('/:id', (req, res) => {
|
|
Users.findUserById(req.params.id)
|
|
.then(users => {
|
|
res.status(200).json(users);
|
|
})
|
|
.catch(err => res.send(err));
|
|
});
|
|
|
|
router.put('/:id', async (req, res) => {
|
|
Users.updateUser(req.params.id, req.body)
|
|
.then(user => {
|
|
if (user) {
|
|
res.status(200).json(user);
|
|
} else {
|
|
res.status(404).json({ message: 'The user could not be found' });
|
|
}
|
|
})
|
|
.catch(() => res.status(500).json({ message: 'Error updating the user' }));
|
|
});
|
|
|
|
router.delete("/:id", (req, res) => {
|
|
Users.removeUser(req.params.id)
|
|
.then(() => res.status(200).json({ message: 'The user was successfully deleted.'}))
|
|
.catch(err => res.json(err));
|
|
});
|
|
|
|
module.exports = router;
|