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

33 lines
837 B
JavaScript
Raw Normal View History

2019-08-01 07:42:25 +00:00
const router = require('express').Router();
const { authenticate } = require('../auth/authenticate');
const Users = require('../users/usersModel');
router.get('/:id', authenticate, (req, res) => {
Users.findById(req.params.id)
.then(users => {
res.status(200).json(users);
})
.catch(err => res.send(err));
});
router.put('/:id', authenticate, async (req, res) => {
Users.update(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(err => res.status(500).json({ message: 'Error updating the user' }));
});
router.delete("/:id", (req, res) => {
Users.remove(req.params.id)
.then(data => res.status(200).json(data))
.catch(err => res.json(err));
});
module.exports = router;