From 245421a512acedebd952d3e8921a95c7edf68793 Mon Sep 17 00:00:00 2001 From: Ricardo Tomasi Date: Mon, 20 May 2013 01:22:55 -0300 Subject: [PATCH] settings data provider and api --- core/shared/api.js | 21 ++++++++++++-- core/shared/models/dataProvider.bookshelf.js | 29 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/core/shared/api.js b/core/shared/api.js index 6c15dfe9ef..61f48dc0dc 100644 --- a/core/shared/api.js +++ b/core/shared/api.js @@ -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; }()); \ No newline at end of file diff --git a/core/shared/models/dataProvider.bookshelf.js b/core/shared/models/dataProvider.bookshelf.js index a42a145e2e..ef9ad56c85 100644 --- a/core/shared/models/dataProvider.bookshelf.js +++ b/core/shared/models/dataProvider.bookshelf.js @@ -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;