sleep-tracker-backend/services/sessions/sessionsRouter.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-08-01 14:25:44 +00:00
const router = require('express').Router();
const Sessions = require('../sessions/sessionsModel');
// TODO: add validation
router.post('/sessions', (req, res) => {
2019-08-01 14:25:44 +00:00
Sessions.addSession(req.body)
.then(response => {
res.status(200).json(response);
})
.catch(err => {
console.log(err);
res.send(err)
});
2019-08-01 14:25:44 +00:00
});
router.get('/:id/sessions', (req, res) => {
2019-08-01 14:25:44 +00:00
Sessions.findUserSessions(req.params.id)
.then(users => {
res.status(200).json(users);
})
.catch(err => res.send(err));
});
router.put('/sessions/:id', async (req, res) => {
2019-08-01 14:25:44 +00:00
Sessions.updateSession(req.params.id, req.body)
.then(session => {
if (session) {
res.status(200).json(session);
} else {
2019-08-01 20:31:44 +00:00
res.status(404).json({ message: 'The session could not be found.' });
2019-08-01 14:25:44 +00:00
}
})
.catch(err => {
res.status(500).json(err);
});
});
router.delete("/sessions/:id", async (req, res) => {
2019-08-01 14:25:44 +00:00
Sessions.removeSession(req.params.id)
2019-08-01 20:31:44 +00:00
.then(() => res.status(200).json({ message: 'The session has been successfully deleted.' }))
2019-08-01 14:25:44 +00:00
.catch(err => res.json(err));
});
module.exports = router;