Skip to content

Commit

Permalink
Refactor code to separate index page and results page rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAlejandro18 committed Jun 8, 2024
1 parent ffc1deb commit 3cf81af
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
19 changes: 19 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const VLSM = require('../models/vlsm');

const indexPage = (req, res) => {
res.render('index', {
title: 'Calculadora VLSM',
page: 'index'
});
};

const resultsPage = (req, res) => {
let data = req.session.result;

res.render('results', {
title: 'Calculadora VLSM | Resultados',
page: 'results',
data
});
};

const calculate = (req, res) => {
try {
let { main_network, lans, prefix } = req.body;
Expand Down Expand Up @@ -27,5 +44,7 @@ const calculate = (req, res) => {
}

module.exports = {
indexPage,
resultsPage,
calculate
};
10 changes: 10 additions & 0 deletions middlewares/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const validateSessionResult = (req, res, next) => {
if (!req.session.result) {
return res.redirect('/');
}
next();
}

module.exports = {
validateSessionResult
};
20 changes: 4 additions & 16 deletions routes/pages.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
const { Router } = require('express');
const router = Router();
const { calculate } = require('../controllers/index');
const { indexPage, resultsPage, calculate } = require('../controllers/index');
const { validateSessionResult } = require('../middlewares/index');

router.get('/', (req, res) => {
res.render('index', {
title: 'Calculadora VLSM',
page: 'index'
});
});
router.get('/', indexPage);

router.post('/calculate', calculate);

router.get('/results', (req, res) => {
let data = req.session.result;

res.render('results', {
title: 'Calculadora VLSM | Resultados',
page: 'results',
data
});
});
router.get('/results', validateSessionResult, resultsPage);

module.exports = router;

0 comments on commit 3cf81af

Please sign in to comment.