recursively get all children
calculate topic/post count from children
new sorted set `cid:<id>:children`
fix search query params
This commit is contained in:
barisusakli
2015-08-18 14:17:16 -04:00
parent a990e9c3bf
commit 5b87af4389
12 changed files with 181 additions and 43 deletions

View File

@@ -50,17 +50,63 @@ module.exports = function(Categories) {
};
function updateCategoryField(cid, key, value, callback) {
if (key === 'parentCid') {
return updateParent(cid, value, callback);
}
db.setObjectField('category:' + cid, key, value, function(err) {
if (err) {
return callback(err);
}
if (key === 'order') {
db.sortedSetAdd('categories:cid', value, cid, callback);
updateOrder(cid, value, callback);
} else {
callback();
}
});
}
function updateParent(cid, newParent, callback) {
Categories.getCategoryField(cid, 'parentCid', function(err, oldParent) {
if (err) {
return callback(err);
}
async.series([
function (next) {
oldParent = parseInt(oldParent, 10) || 0;
db.sortedSetRemove('cid:' + oldParent + ':children', cid, next);
},
function (next) {
newParent = parseInt(newParent, 10) || 0;
db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next);
},
function (next) {
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
}
], function(err, results) {
callback(err);
});
});
}
function updateOrder(cid, order, callback) {
Categories.getCategoryField(cid, 'parentCid', function(err, parentCid) {
if (err) {
return callback(err);
}
async.parallel([
function (next) {
db.sortedSetAdd('categories:cid', order, cid, next);
},
function (next) {
parentCid = parseInt(parentCid, 10) || 0;
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
}
], callback);
});
}
};