From c0a2004ea89034e4b99a2e481bc2558b738a6709 Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Tue, 10 Oct 2017 19:03:52 +0200 Subject: [PATCH] add limit offset middleware for users --- api/middleware/filter-handler-mongo.js | 2 +- .../limitoffset-middleware-mongo.js | 70 +++++++++++++++++++ api/routes/users.js | 5 +- package.json | 3 +- 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 api/middleware/limitoffset-middleware-mongo.js diff --git a/api/middleware/filter-handler-mongo.js b/api/middleware/filter-handler-mongo.js index 5dd9ad0..9ae5878 100644 --- a/api/middleware/filter-handler-mongo.js +++ b/api/middleware/filter-handler-mongo.js @@ -22,7 +22,7 @@ "use strict"; const express = require('express'); -const logger = require('debug')('me2:filterware'); +const logger = require('debug')('middleware:filterware'); /** * private helper function to filter Objects by given keys diff --git a/api/middleware/limitoffset-middleware-mongo.js b/api/middleware/limitoffset-middleware-mongo.js new file mode 100644 index 0000000..12eab7e --- /dev/null +++ b/api/middleware/limitoffset-middleware-mongo.js @@ -0,0 +1,70 @@ +/** This module defines a express.Router() instance + * - supporting offset= and limit=* + * - calls next with error if a impossible offset and/or limit value is given + * + * Note: it expects to be called BEFORE any data fetched from DB + * Note: it sets an object { limit: 0, skip: 0 } with the proper number values in req.locals.limitskip + * Note: it sets an Error-Object to next with error.status set to HTTP status code 400 + * + * @author Johannes Konert + * @licence CC BY-SA 4.0 + * + * @module restapi/limitoffset-middleware-mongo + * @type {Router} + */ + +// remember: in modules you have 3 variables given by CommonJS +// 1.) require() function +// 2.) module.exports +// 3.) exports (which is module.exports) +"use strict"; + +const router = require('express').Router(); +const logger = require('debug')('middleware:offsetlimit'); + + +// the exported router with handler +router.use((req, res, next) => { + let offset = undefined; + let limit = undefined; + const offsetString = req.query.offset; + const limitString = req.query.limit; + let err = null; + + + if (offsetString) { + console.log(offsetString) + if (!isNaN(offsetString)) { + offset = parseInt(offsetString); + if (offset < 0) { + err = new Error('offset is negative') + } + } + else { + err = new Error('given offset is not a valid number ' + offsetString); + } + } + if (limitString) { + if (!isNaN(limitString)) { + limit = parseInt(limitString); + if (limit < 1) { + err = new Error('limit is zero or negative') + } + } + else { + err = new Error('given limit is not a valid number ' + limitString); + } + } + if (err) { + logger('problem occurred with limit/offset values'); + err.status = 400; + next(err) + } else { + res.locals.limitskip = {}; // mongoDB uses parameter object for skip/limit + if (limit) res.locals.limitskip.limit = limit; + if (offset) res.locals.limitskip.skip = offset; + next() + } +}); + +module.exports = router; diff --git a/api/routes/users.js b/api/routes/users.js index 948b879..23feea4 100644 --- a/api/routes/users.js +++ b/api/routes/users.js @@ -11,6 +11,7 @@ const codes = require('./http-codes'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const checkHl = require('../middleware/permission-check').checkHl; const sortCollectionBy = require('../middleware/util').sortCollection; +const offsetlimitMiddleware = require('../middleware/limitoffset-middleware-mongo'); const routerHandling = require('../middleware/router-handling'); // Mongoose Model using mongoDB @@ -23,6 +24,8 @@ const resultSet = {'__v': 0, 'updatedAt': 0, 'timestamp': 0}; const users = express.Router(); +users.get('/', offsetlimitMiddleware); + // routes ********************** users.route('/') .get((req, res, next) => { @@ -44,7 +47,7 @@ users.route('/') const fractionFilter = req.query.fractFilter; const squadFilter = req.query.squadId; - UserModel.find({}, (err, users) => { + UserModel.find({}, res.locals.filter, res.locals.limitskip, (err, users) => { if (err) return next(err); if (users.length === 0) { res.locals.items = users; diff --git a/package.json b/package.json index 3976222..755ff8b 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,7 @@ "start-e2e": "npm run deploy-static && npm run e2e --prefix ./api", "test-e2e": "npm run e2e --prefix ./static" }, - "dependencies": { - }, + "dependencies": {}, "devDependencies": { "concurrently": "^3.4.0", "wait-on": "^2.0.2"