Add daily averages endpoint / move authentication

This commit is contained in:
ruihildt 2019-08-01 21:12:20 +02:00
parent c36391321c
commit 31bbcb52c7
5 changed files with 19 additions and 24 deletions

View File

@ -6,6 +6,7 @@ const authRouter = require('../services/auth/authRouter');
const usersRouter = require('../services/users/usersRouter'); const usersRouter = require('../services/users/usersRouter');
const sessionsRouter = require('../services/sessions/sessionsRouter'); const sessionsRouter = require('../services/sessions/sessionsRouter');
const dailyAveragesRouter = require('../services/dailyAverages/dailyAveragesRouter'); const dailyAveragesRouter = require('../services/dailyAverages/dailyAveragesRouter');
const { authenticate } = require('../services/auth/authenticate');
const server = express(); const server = express();
@ -14,8 +15,8 @@ server.use(express.json());
server.use(cors()); server.use(cors());
server.use('/api/auth', authRouter); server.use('/api/auth', authRouter);
server.use('/api/users', usersRouter); server.use('/api/users', authenticate, usersRouter);
server.use('/api/users', sessionsRouter); server.use('/api/users', authenticate, sessionsRouter);
server.use('/api/users', dailyAveragesRouter); server.use('/api/users', authenticate, dailyAveragesRouter);
module.exports = server; module.exports = server;

View File

@ -1,10 +1,10 @@
const db = require('../../data/dbConfig'); const db = require('../../data/dbConfig');
module.exports = { module.exports = {
getDailyAverageBy getDailyAverages
} }
function getDailyAverageBy(id) { function getDailyAverages(id) {
return db('dailyAverages') return db('sessions')
.where({}); .where({ id });
} }

View File

@ -4,15 +4,11 @@ const router = require('express').Router();
const Averages = require('./dailyAveragesModel'); const Averages = require('./dailyAveragesModel');
router.get('/:id/averages', (req, res) => { router.get('/:id/averages', (req, res) => {
Averages.getDailyAverages(req.params.id)
Averages.findUserBy(req.params.id) .then(averages => {
.first() res.status(200).json(averages);
.then(response => { })
res.status(200).json(response); .catch(err => res.send(err));
})
.catch(error => {
res.status(500).json(error);
});
}); });
module.exports = router; module.exports = router;

View File

@ -1,9 +1,8 @@
const router = require('express').Router(); const router = require('express').Router();
const { authenticate } = require('../auth/authenticate');
const Sessions = require('../sessions/sessionsModel'); const Sessions = require('../sessions/sessionsModel');
router.post('/sessions', authenticate, (req, res) => { router.post('/sessions', (req, res) => {
Sessions.addSession(req.body) Sessions.addSession(req.body)
.then(response => { .then(response => {
res.status(200).json(response); res.status(200).json(response);
@ -14,7 +13,7 @@ router.post('/sessions', authenticate, (req, res) => {
}); });
}); });
router.get('/:id/sessions', authenticate, (req, res) => { router.get('/:id/sessions', (req, res) => {
Sessions.findUserSessions(req.params.id) Sessions.findUserSessions(req.params.id)
.then(users => { .then(users => {
res.status(200).json(users); res.status(200).json(users);
@ -22,7 +21,7 @@ router.get('/:id/sessions', authenticate, (req, res) => {
.catch(err => res.send(err)); .catch(err => res.send(err));
}); });
router.put('/sessions/:id', authenticate, async (req, res) => { router.put('/sessions/:id', async (req, res) => {
Sessions.updateSession(req.params.id, req.body) Sessions.updateSession(req.params.id, req.body)
.then(session => { .then(session => {
if (session) { if (session) {
@ -36,7 +35,7 @@ router.put('/sessions/:id', authenticate, async (req, res) => {
}); });
}); });
router.delete("/sessions/:id", authenticate, async (req, res) => { router.delete("/sessions/:id", async (req, res) => {
Sessions.removeSession(req.params.id) Sessions.removeSession(req.params.id)
.then(() => res.status(200).json({ message: 'The session has been successfully deleted' })) .then(() => res.status(200).json({ message: 'The session has been successfully deleted' }))
.catch(err => res.json(err)); .catch(err => res.json(err));

View File

@ -1,9 +1,8 @@
const router = require('express').Router(); const router = require('express').Router();
const { authenticate } = require('../auth/authenticate');
const Users = require('../users/usersModel'); const Users = require('../users/usersModel');
router.get('/:id', authenticate, (req, res) => { router.get('/:id', (req, res) => {
Users.findUserById(req.params.id) Users.findUserById(req.params.id)
.then(users => { .then(users => {
res.status(200).json(users); res.status(200).json(users);
@ -11,7 +10,7 @@ router.get('/:id', authenticate, (req, res) => {
.catch(err => res.send(err)); .catch(err => res.send(err));
}); });
router.put('/:id', authenticate, async (req, res) => { router.put('/:id', async (req, res) => {
Users.updateUser(req.params.id, req.body) Users.updateUser(req.params.id, req.body)
.then(user => { .then(user => {
if (user) { if (user) {