From fbf1134e3e6b386c9ec5a3946c61f3faf84397fb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 13 May 2016 18:10:46 +0200 Subject: Introduce paginations in videos listing --- server/middlewares/index.js | 2 ++ server/middlewares/pagination.js | 18 ++++++++++++++++++ server/middlewares/reqValidators/index.js | 2 ++ server/middlewares/reqValidators/pagination.js | 21 +++++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 server/middlewares/pagination.js create mode 100644 server/middlewares/reqValidators/pagination.js (limited to 'server/middlewares') diff --git a/server/middlewares/index.js b/server/middlewares/index.js index ffd19337c..f0fad3418 100644 --- a/server/middlewares/index.js +++ b/server/middlewares/index.js @@ -1,11 +1,13 @@ 'use strict' const oauth2 = require('./oauth2') +const pagination = require('./pagination') const reqValidatorsMiddleware = require('./reqValidators') const secureMiddleware = require('./secure') const middlewares = { oauth2: oauth2, + pagination: pagination, reqValidators: reqValidatorsMiddleware, secure: secureMiddleware } diff --git a/server/middlewares/pagination.js b/server/middlewares/pagination.js new file mode 100644 index 000000000..2a426ec28 --- /dev/null +++ b/server/middlewares/pagination.js @@ -0,0 +1,18 @@ +'use strict' + +const paginationMiddleware = { + setPagination: setPagination +} + +function setPagination (req, res, next) { + if (!req.query.start) req.query.start = 0 + else req.query.start = parseInt(req.query.start) + if (!req.query.count) req.query.count = 15 + else req.query.count = parseInt(req.query.count) + + return next() +} + +// --------------------------------------------------------------------------- + +module.exports = paginationMiddleware diff --git a/server/middlewares/reqValidators/index.js b/server/middlewares/reqValidators/index.js index c6c5e1309..b732a27b6 100644 --- a/server/middlewares/reqValidators/index.js +++ b/server/middlewares/reqValidators/index.js @@ -1,10 +1,12 @@ 'use strict' +const paginationReqValidators = require('./pagination') const podsReqValidators = require('./pods') const remoteReqValidators = require('./remote') const videosReqValidators = require('./videos') const reqValidators = { + pagination: paginationReqValidators, pods: podsReqValidators, remote: remoteReqValidators, videos: videosReqValidators diff --git a/server/middlewares/reqValidators/pagination.js b/server/middlewares/reqValidators/pagination.js new file mode 100644 index 000000000..ca8375396 --- /dev/null +++ b/server/middlewares/reqValidators/pagination.js @@ -0,0 +1,21 @@ +'use strict' + +const checkErrors = require('./utils').checkErrors +const logger = require('../../helpers/logger') + +const reqValidatorsPagination = { + pagination: pagination +} + +function pagination (req, res, next) { + req.checkParams('start', 'Should have a number start').optional().isInt() + req.checkParams('count', 'Should have a number count').optional().isInt() + + logger.debug('Checking pagination parameters', { parameters: req.params }) + + checkErrors(req, res, next) +} + +// --------------------------------------------------------------------------- + +module.exports = reqValidatorsPagination -- cgit v1.2.3