sleep-tracker-backend/services/sessions/sessionsRouter.js
ruihildt 77a4c28757 Fix dailyAverages query on session update & test
- Add dailyAverages seeds
- Start implementing testing
2019-08-02 01:18:56 +02:00

47 lines
1.1 KiB
JavaScript

const router = require('express').Router();
const Sessions = require('../sessions/sessionsModel');
// TODO: add validation
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;