add limit offset middleware for users
parent
4e8d6b4279
commit
c0a2004ea8
|
@ -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
|
||||
|
|
|
@ -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;
|
|
@ -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;
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue