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

46 lines
1.1 KiB
JavaScript

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