2019-08-01 14:25:44 +00:00
|
|
|
const router = require('express').Router();
|
|
|
|
|
|
|
|
const Sessions = require('../sessions/sessionsModel');
|
|
|
|
|
2019-08-01 23:18:56 +00:00
|
|
|
// TODO: add validation
|
2019-08-01 19:12:20 +00:00
|
|
|
router.post('/sessions', (req, res) => {
|
2019-08-01 14:25:44 +00:00
|
|
|
Sessions.addSession(req.body)
|
|
|
|
.then(response => {
|
|
|
|
res.status(200).json(response);
|
|
|
|
})
|
2019-08-01 18:43:43 +00:00
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
res.send(err)
|
|
|
|
});
|
2019-08-01 14:25:44 +00:00
|
|
|
});
|
|
|
|
|
2019-08-01 19:12:20 +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));
|
|
|
|
});
|
|
|
|
|
2019-08-01 19:12:20 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-01 19:12:20 +00:00
|
|
|
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;
|