diff --git a/src/posts/category.js b/src/posts/category.js index a1f3e8afbc..9c877d91dd 100644 --- a/src/posts/category.js +++ b/src/posts/category.js @@ -21,36 +21,36 @@ module.exports = function (Posts) { }; Posts.getCidsByPids = function (pids, callback) { - Posts.getPostsFields(pids, ['tid'], function (err, posts) { - if (err) { - return callback(err); - } - - var tids = posts.map(function (post) { - return post.tid; - }).filter(function (tid, index, array) { - return tid && array.indexOf(tid) === index; - }); - - topics.getTopicsFields(tids, ['cid'], function (err, topics) { - if (err) { - return callback(err); - } + var tids; + var postData; + async.waterfall([ + function (next) { + Posts.getPostsFields(pids, ['tid'], next); + }, + function (_postData, next) { + postData = _postData; + tids = postData.map(function (post) { + return post.tid; + }).filter(function (tid, index, array) { + return tid && array.indexOf(tid) === index; + }); + topics.getTopicsFields(tids, ['cid'], next); + }, + function (topicData, next) { var map = {}; - topics.forEach(function (topic, index) { + topicData.forEach(function (topic, index) { if (topic) { map[tids[index]] = topic.cid; } }); - var cids = posts.map(function (post) { + var cids = postData.map(function (post) { return map[post.tid]; }); - - callback(null, cids); - }); - }); + next(null, cids); + } + ], callback); }; Posts.filterPidsByCid = function (pids, cid, callback) { diff --git a/test/controllers.js b/test/controllers.js index 91983e940a..71b8e32a6d 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -16,6 +16,7 @@ describe('Controllers', function () { var tid; var cid; + var pid; var fooUid; before(function (done) { @@ -38,6 +39,7 @@ describe('Controllers', function () { topics.post({uid: results.user, title: 'test topic title', content: 'test topic content', cid: results.category.cid}, function (err, result) { tid = result.topicData.tid; + pid = result.postData.pid; done(err); }); }); @@ -462,6 +464,33 @@ describe('Controllers', function () { }); }); + it('should get post data', function (done) { + request(nconf.get('url') + '/api/post/pid/' + pid, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should get topic data', function (done) { + request(nconf.get('url') + '/api/topic/tid/' + tid, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should get category data', function (done) { + request(nconf.get('url') + '/api/category/cid/' + cid, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + after(function (done) { db.emptydb(done); });