diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-05-13 18:10:46 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-05-13 18:10:46 +0200 |
commit | fbf1134e3e6b386c9ec5a3946c61f3faf84397fb (patch) | |
tree | 9071ea72c86e83bb07d8ec681a54daff2e0eb7b9 /server/middlewares | |
parent | f67e976fdccbfe6432c1176148d194c980a5585a (diff) | |
download | PeerTube-fbf1134e3e6b386c9ec5a3946c61f3faf84397fb.tar.gz PeerTube-fbf1134e3e6b386c9ec5a3946c61f3faf84397fb.tar.zst PeerTube-fbf1134e3e6b386c9ec5a3946c61f3faf84397fb.zip |
Introduce paginations in videos listing
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/index.js | 2 | ||||
-rw-r--r-- | server/middlewares/pagination.js | 18 | ||||
-rw-r--r-- | server/middlewares/reqValidators/index.js | 2 | ||||
-rw-r--r-- | server/middlewares/reqValidators/pagination.js | 21 |
4 files changed, 43 insertions, 0 deletions
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 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const oauth2 = require('./oauth2') | 3 | const oauth2 = require('./oauth2') |
4 | const pagination = require('./pagination') | ||
4 | const reqValidatorsMiddleware = require('./reqValidators') | 5 | const reqValidatorsMiddleware = require('./reqValidators') |
5 | const secureMiddleware = require('./secure') | 6 | const secureMiddleware = require('./secure') |
6 | 7 | ||
7 | const middlewares = { | 8 | const middlewares = { |
8 | oauth2: oauth2, | 9 | oauth2: oauth2, |
10 | pagination: pagination, | ||
9 | reqValidators: reqValidatorsMiddleware, | 11 | reqValidators: reqValidatorsMiddleware, |
10 | secure: secureMiddleware | 12 | secure: secureMiddleware |
11 | } | 13 | } |
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 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const paginationMiddleware = { | ||
4 | setPagination: setPagination | ||
5 | } | ||
6 | |||
7 | function setPagination (req, res, next) { | ||
8 | if (!req.query.start) req.query.start = 0 | ||
9 | else req.query.start = parseInt(req.query.start) | ||
10 | if (!req.query.count) req.query.count = 15 | ||
11 | else req.query.count = parseInt(req.query.count) | ||
12 | |||
13 | return next() | ||
14 | } | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | 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 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const paginationReqValidators = require('./pagination') | ||
3 | const podsReqValidators = require('./pods') | 4 | const podsReqValidators = require('./pods') |
4 | const remoteReqValidators = require('./remote') | 5 | const remoteReqValidators = require('./remote') |
5 | const videosReqValidators = require('./videos') | 6 | const videosReqValidators = require('./videos') |
6 | 7 | ||
7 | const reqValidators = { | 8 | const reqValidators = { |
9 | pagination: paginationReqValidators, | ||
8 | pods: podsReqValidators, | 10 | pods: podsReqValidators, |
9 | remote: remoteReqValidators, | 11 | remote: remoteReqValidators, |
10 | videos: videosReqValidators | 12 | 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 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const checkErrors = require('./utils').checkErrors | ||
4 | const logger = require('../../helpers/logger') | ||
5 | |||
6 | const reqValidatorsPagination = { | ||
7 | pagination: pagination | ||
8 | } | ||
9 | |||
10 | function pagination (req, res, next) { | ||
11 | req.checkParams('start', 'Should have a number start').optional().isInt() | ||
12 | req.checkParams('count', 'Should have a number count').optional().isInt() | ||
13 | |||
14 | logger.debug('Checking pagination parameters', { parameters: req.params }) | ||
15 | |||
16 | checkErrors(req, res, next) | ||
17 | } | ||
18 | |||
19 | // --------------------------------------------------------------------------- | ||
20 | |||
21 | module.exports = reqValidatorsPagination | ||