2014-02-28 15:25:50 -05:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
|
|
|
|
db = require('./../database'),
|
2014-04-01 17:12:03 -04:00
|
|
|
utils = require('./../../public/src/utils');
|
2013-05-24 11:18:41 -04:00
|
|
|
|
|
|
|
|
|
2014-04-01 17:12:03 -04:00
|
|
|
module.exports = function(Categories) {
|
|
|
|
|
|
|
|
|
|
Categories.update = function(modified, callback) {
|
2013-06-24 17:18:58 -04:00
|
|
|
|
2014-02-28 15:25:50 -05:00
|
|
|
function updateCategory(cid, next) {
|
2013-06-24 17:18:58 -04:00
|
|
|
var category = modified[cid];
|
2014-02-28 15:25:50 -05:00
|
|
|
var fields = Object.keys(category);
|
2013-09-11 16:04:34 -04:00
|
|
|
|
2014-02-28 15:25:50 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2013-06-24 17:18:58 -04:00
|
|
|
|
2014-02-22 18:56:37 -05:00
|
|
|
if (key === 'name') {
|
2014-02-28 15:25:50 -05:00
|
|
|
var slug = cid + '/' + utils.slugify(value);
|
|
|
|
|
db.setObjectField('category:' + cid, 'slug', slug, next);
|
2014-02-22 18:56:37 -05:00
|
|
|
} else if (key === 'order') {
|
2014-02-28 15:25:50 -05:00
|
|
|
db.sortedSetAdd('categories:cid', value, cid, next);
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
2013-06-24 17:18:58 -04:00
|
|
|
}
|
2014-02-28 15:25:50 -05:00
|
|
|
});
|
2013-06-24 17:18:58 -04:00
|
|
|
}
|
|
|
|
|
|
2014-02-28 15:25:50 -05:00
|
|
|
var cids = Object.keys(modified);
|
|
|
|
|
|
|
|
|
|
async.each(cids, updateCategory, function(err) {
|
|
|
|
|
callback(err, cids);
|
2013-06-24 17:18:58 -04:00
|
|
|
});
|
2013-05-24 11:18:41 -04:00
|
|
|
};
|
|
|
|
|
|
2014-04-10 20:31:57 +01:00
|
|
|
};
|