From 7b21760f0009045a8a3a64d4c67ba344b5352579 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 9 Dec 2015 17:43:28 +0200 Subject: [PATCH] refactor get user by x --- src/controllers/api.js | 69 ++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/src/controllers/api.js b/src/controllers/api.js index aebf4a746e..74c4d008c8 100644 --- a/src/controllers/api.js +++ b/src/controllers/api.js @@ -179,18 +179,11 @@ apiController.getObject = function(req, res, next) { apiController.getUserByUID = function(req, res, next) { var uid = req.params.uid ? req.params.uid : 0; - async.parallel({ - userData: async.apply(user.getUserData, uid), - settings: async.apply(user.getSettings, uid) - }, function(err, results) { - if (err || !results.userData) { + getUserByUID(uid, function(err, userData) { + if (err || !userData) { return next(err); } - - results.userData.email = results.settings.showemail ? results.userData.email : undefined; - results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined; - - res.json(results.userData); + res.json(userData); }); }; @@ -203,20 +196,14 @@ apiController.getUserByUsername = function(req, res, next) { user.getUidByUsername(username, next); }, function(uid, next) { - async.parallel({ - userData: async.apply(user.getUserData, uid), - settings: async.apply(user.getSettings, uid) - }, function(err, results) { - if (err || !results.userData) { - return next(err); - } - results.userData.email = results.settings.showemail ? results.userData.email : undefined; - results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined; - - res.json(results.userData); - }); + getUserByUID(uid, next); + } + ], function(err, userData) { + if (err || !userData) { + return next(err); } - ], next); + res.json(userData); + }); }; @@ -228,23 +215,33 @@ apiController.getUserByEmail = function(req, res, next) { user.getUidByEmail(email, next); }, function(uid, next) { - async.parallel({ - userData: async.apply(user.getUserData, uid), - settings: async.apply(user.getSettings, uid) - }, function(err, results) { - if (err || !results.userData) { - return next(err); - } - results.userData.email = results.settings.showemail ? results.userData.email : undefined; - results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined; - - res.json(results.userData); - }); + getUserByUID(uid, next); + } + ], function(err, userData) { + if (err || !userData) { + return next(err); } - ], next); + res.json(userData); + }); }; +function getUserByUID(uid, callback) { + async.parallel({ + userData: async.apply(user.getUserData, uid), + settings: async.apply(user.getSettings, uid) + }, function(err, results) { + if (err || !results.userData) { + return callback(err, null); + } + + results.userData.email = results.settings.showemail ? results.userData.email : undefined; + results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined; + + callback(null, results.userData); + }); +} + apiController.getModerators = function(req, res, next) { categories.getModerators(req.params.cid, function(err, moderators) { if (err) {