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

View File

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

View File

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

View File

@ -1,9 +1,8 @@
const router = require('express').Router();
const { authenticate } = require('../auth/authenticate');
const Sessions = require('../sessions/sessionsModel');
router.post('/sessions', authenticate, (req, res) => {
router.post('/sessions', (req, res) => {
Sessions.addSession(req.body)
.then(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)
.then(users => {
res.status(200).json(users);
@ -22,7 +21,7 @@ router.get('/:id/sessions', authenticate, (req, res) => {
.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)
.then(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)
.then(() => res.status(200).json({ message: 'The session has been successfully deleted' }))
.catch(err => res.json(err));

View File

@ -1,9 +1,8 @@
const router = require('express').Router();
const { authenticate } = require('../auth/authenticate');
const Users = require('../users/usersModel');
router.get('/:id', authenticate, (req, res) => {
router.get('/:id', (req, res) => {
Users.findUserById(req.params.id)
.then(users => {
res.status(200).json(users);
@ -11,7 +10,7 @@ router.get('/:id', authenticate, (req, res) => {
.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)
.then(user => {
if (user) {