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

44 lines
1.2 KiB
JavaScript

const router = require('express').Router();
const { authenticate } = require('../auth/authenticate');
const Sessions = require('../sessions/sessionsModel');
router.post('/sessions', authenticate, (req, res) => {
Sessions.addSession(req.body)
.then(response => {
res.status(200).json(response);
})
.catch(err => res.send(err));
});
router.get('/:id/sessions', authenticate, (req, res) => {
Sessions.findUserSessions(req.params.id)
.then(users => {
res.status(200).json(users);
})
.catch(err => res.send(err));
});
router.put('/sessions/:id', authenticate, 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", authenticate, 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;