From 5c39adb7313e0696aabb4b71196ab7b0b378c359 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Aug 2016 22:31:45 +0200 Subject: Server: add user list sort/pagination --- server/controllers/api/v1/users.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js index 704df770c..975e25e68 100644 --- a/server/controllers/api/v1/users.js +++ b/server/controllers/api/v1/users.js @@ -11,6 +11,10 @@ const logger = require('../../../helpers/logger') const middlewares = require('../../../middlewares') const admin = middlewares.admin const oAuth = middlewares.oauth +const pagination = middlewares.pagination +const sort = middlewares.sort +const validatorsPagination = middlewares.validators.pagination +const validatorsSort = middlewares.validators.sort const validatorsUsers = middlewares.validators.users const User = mongoose.model('User') @@ -18,9 +22,16 @@ const Video = mongoose.model('Video') const router = express.Router() -router.get('/', listUsers) router.get('/me', oAuth.authenticate, getUserInformation) +router.get('/', + validatorsPagination.pagination, + validatorsSort.usersSort, + sort.setUsersSort, + pagination.setPagination, + listUsers +) + router.post('/', oAuth.authenticate, admin.ensureIsAdmin, @@ -73,10 +84,10 @@ function getUserInformation (req, res, next) { } function listUsers (req, res, next) { - User.list(function (err, usersList) { + User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) { if (err) return next(err) - res.json(getFormatedUsers(usersList)) + res.json(getFormatedUsers(usersList, usersTotal)) }) } @@ -145,7 +156,7 @@ function success (req, res, next) { // --------------------------------------------------------------------------- -function getFormatedUsers (users) { +function getFormatedUsers (users, usersTotal) { const formatedUsers = [] users.forEach(function (user) { @@ -153,6 +164,7 @@ function getFormatedUsers (users) { }) return { + total: usersTotal, data: formatedUsers } } -- cgit v1.2.3