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

32 lines
803 B
JavaScript

const router = require('express').Router();
const Users = require('../users/usersModel');
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;