You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1018 B
JavaScript
46 lines
1018 B
JavaScript
11 years ago
|
|
||
|
'use strict';
|
||
|
|
||
|
var async = require('async'),
|
||
|
db = require('./../database'),
|
||
11 years ago
|
utils = require('./../../public/src/utils');
|
||
12 years ago
|
|
||
|
|
||
11 years ago
|
module.exports = function(Categories) {
|
||
|
|
||
|
Categories.update = function(modified, callback) {
|
||
12 years ago
|
|
||
11 years ago
|
function updateCategory(cid, next) {
|
||
12 years ago
|
var category = modified[cid];
|
||
11 years ago
|
var fields = Object.keys(category);
|
||
12 years ago
|
|
||
11 years ago
|
async.each(fields, function(key, next) {
|
||
|
updateCategoryField(cid, key, category[key], next);
|
||
|
}, next);
|
||
|
}
|
||
|
|
||
|
function updateCategoryField(cid, key, value, next) {
|
||
|
db.setObjectField('category:' + cid, key, value, function(err) {
|
||
|
if(err) {
|
||
|
return next(err);
|
||
|
}
|
||
12 years ago
|
|
||
11 years ago
|
if (key === 'name') {
|
||
11 years ago
|
var slug = cid + '/' + utils.slugify(value);
|
||
|
db.setObjectField('category:' + cid, 'slug', slug, next);
|
||
11 years ago
|
} else if (key === 'order') {
|
||
11 years ago
|
db.sortedSetAdd('categories:cid', value, cid, next);
|
||
|
} else {
|
||
|
next();
|
||
12 years ago
|
}
|
||
11 years ago
|
});
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
var cids = Object.keys(modified);
|
||
|
|
||
|
async.each(cids, updateCategory, function(err) {
|
||
|
callback(err, cids);
|
||
12 years ago
|
});
|
||
12 years ago
|
};
|
||
|
|
||
11 years ago
|
};
|