settings data provider and api

This commit is contained in:
Ricardo Tomasi 2013-05-20 01:22:55 -03:00
parent a49565c6c2
commit 245421a512
2 changed files with 47 additions and 3 deletions

View File

@ -16,6 +16,7 @@
ghost = new Ghost(),
posts,
users,
settings,
requestHandler;
// # Posts
@ -56,9 +57,22 @@
return when.call(ghost.dataProvider().users.check, postData);
}
};
// settings: {},
// categories: {},
// post_categories: {}
// # Settings
settings = {
browse: function (options) {
return when.call(ghost.dataProvider().settings.browse, options);
},
read: function (key) {
return when.call(ghost.dataProvider().settings.read, key);
},
edit: function (key, value) {
return when.call(ghost.dataProvider().settings.edit, key, value);
}
};
// categories: {};
// post_categories: {};
// requestHandler
@ -78,5 +92,6 @@
module.exports.posts = posts;
module.exports.users = users;
module.exports.settings = settings;
module.exports.requestHandler = requestHandler;
}());

View File

@ -9,6 +9,7 @@
var knex = require('./knex_init'),
models = require('./models'),
bcrypt = require('bcrypt'),
when = require("when"),
DataProvider,
instance;
@ -121,6 +122,34 @@
callback(null, _user);
});
});
// ## Settings
DataProvider.prototype.settings = function () { };
DataProvider.prototype.settings.browse = function (_args, callback) {
models.Settings.forge(_args).fetch().then(function(settings){
callback(null, settings);
}, callback);
};
DataProvider.prototype.settings.read = function (_key, callback) {
models.Settings.forge({ key: _key }).fetch().then(function(setting){
callback(null, setting);
}, callback);
};
DataProvider.prototype.settings.edit = function (_key, _value, callback) {
var promise;
if (typeof _key === 'object') {
promise = when.all(_.map(settings, function (value, key) {
models.Setting.forge({ key: _key }).set('value', _value).save();
}));
} else {
promise = models.Setting.forge({ key: _key }).set('value', _value).save();
}
promise.then(function (settings) {
callback(null, settings);
}, callback);
};
module.exports = DataProvider;