add limit offset middleware for users

pull/14/head
Florian Hartwich 2017-10-10 19:03:52 +02:00
parent 4e8d6b4279
commit c0a2004ea8
4 changed files with 76 additions and 4 deletions

View File

@ -22,7 +22,7 @@
"use strict"; "use strict";
const express = require('express'); 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 * private helper function to filter Objects by given keys

View File

@ -0,0 +1,70 @@
/** This module defines a express.Router() instance
* - supporting offset=<number> and limit=<number>*
* - 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;

View File

@ -11,6 +11,7 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl; const checkHl = require('../middleware/permission-check').checkHl;
const sortCollectionBy = require('../middleware/util').sortCollection; const sortCollectionBy = require('../middleware/util').sortCollection;
const offsetlimitMiddleware = require('../middleware/limitoffset-middleware-mongo');
const routerHandling = require('../middleware/router-handling'); const routerHandling = require('../middleware/router-handling');
// Mongoose Model using mongoDB // Mongoose Model using mongoDB
@ -23,6 +24,8 @@ const resultSet = {'__v': 0, 'updatedAt': 0, 'timestamp': 0};
const users = express.Router(); const users = express.Router();
users.get('/', offsetlimitMiddleware);
// routes ********************** // routes **********************
users.route('/') users.route('/')
.get((req, res, next) => { .get((req, res, next) => {
@ -44,7 +47,7 @@ users.route('/')
const fractionFilter = req.query.fractFilter; const fractionFilter = req.query.fractFilter;
const squadFilter = req.query.squadId; 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 (err) return next(err);
if (users.length === 0) { if (users.length === 0) {
res.locals.items = users; res.locals.items = users;

View File

@ -15,8 +15,7 @@
"start-e2e": "npm run deploy-static && npm run e2e --prefix ./api", "start-e2e": "npm run deploy-static && npm run e2e --prefix ./api",
"test-e2e": "npm run e2e --prefix ./static" "test-e2e": "npm run e2e --prefix ./static"
}, },
"dependencies": { "dependencies": {},
},
"devDependencies": { "devDependencies": {
"concurrently": "^3.4.0", "concurrently": "^3.4.0",
"wait-on": "^2.0.2" "wait-on": "^2.0.2"