2013-05-16 15:21:13 +04:00
|
|
|
// # Ghost Data API
|
|
|
|
// Provides access to the data model
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is intended to replace the old dataProvider files and should access & manipulate the models directly
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global module, require */
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Ghost = require('../ghost'),
|
|
|
|
_ = require('underscore'),
|
|
|
|
|
|
|
|
ghost = new Ghost(),
|
2013-06-01 18:47:41 +04:00
|
|
|
dataProvider = ghost.dataProvider,
|
2013-05-16 15:21:13 +04:00
|
|
|
posts,
|
|
|
|
users,
|
2013-05-20 08:22:55 +04:00
|
|
|
settings,
|
2013-06-08 09:05:40 +04:00
|
|
|
requestHandler,
|
|
|
|
settingsObject,
|
|
|
|
settingsCollection;
|
2013-05-16 15:21:13 +04:00
|
|
|
|
|
|
|
// # Posts
|
|
|
|
posts = {
|
|
|
|
// takes filter / pagination parameters
|
2013-06-01 23:42:11 +04:00
|
|
|
// returns a page of posts in a json response
|
2013-05-16 15:21:13 +04:00
|
|
|
browse: function (options) {
|
2013-06-01 23:30:42 +04:00
|
|
|
return dataProvider.Post.findPage(options);
|
2013-05-16 15:21:13 +04:00
|
|
|
},
|
|
|
|
// takes an identifier (id or slug?)
|
|
|
|
// returns a single post in a json response
|
2013-05-17 00:56:26 +04:00
|
|
|
read: function (args) {
|
2013-06-01 18:47:41 +04:00
|
|
|
return dataProvider.Post.findOne(args);
|
2013-05-16 15:21:13 +04:00
|
|
|
},
|
|
|
|
// takes a json object with all the properties which should be updated
|
|
|
|
// returns the resulting post in a json response
|
|
|
|
edit: function (postData) {
|
2013-06-01 18:47:41 +04:00
|
|
|
return dataProvider.Post.edit(postData);
|
2013-05-16 15:21:13 +04:00
|
|
|
},
|
|
|
|
// takes a json object representing a post,
|
|
|
|
// returns the resulting post in a json response
|
|
|
|
add: function (postData) {
|
2013-06-01 18:47:41 +04:00
|
|
|
return dataProvider.Post.add(postData);
|
2013-05-16 15:21:13 +04:00
|
|
|
},
|
|
|
|
// takes an identifier (id or slug?)
|
|
|
|
// returns a json response with the id of the deleted post
|
2013-05-24 16:02:59 +04:00
|
|
|
destroy: function (args) {
|
2013-06-01 18:47:41 +04:00
|
|
|
return dataProvider.Post.destroy(args.id);
|
2013-05-16 15:21:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// # Users
|
2013-05-21 05:03:35 +04:00
|
|
|
users = {
|
|
|
|
add: function (postData) {
|
2013-06-01 18:47:41 +04:00
|
|
|
return dataProvider.User.add(postData);
|
2013-05-21 05:03:35 +04:00
|
|
|
},
|
2013-05-26 21:44:01 +04:00
|
|
|
check: function (postData) {
|
2013-06-01 18:47:41 +04:00
|
|
|
return dataProvider.User.check(postData);
|
2013-05-21 05:03:35 +04:00
|
|
|
}
|
|
|
|
};
|
2013-05-20 08:22:55 +04:00
|
|
|
|
|
|
|
// # Settings
|
2013-06-08 09:05:40 +04:00
|
|
|
|
|
|
|
// Turn a settings collection into a single object/hashmap
|
|
|
|
settingsObject = function (settings) {
|
|
|
|
return (settings.toJSON ? settings.toJSON() : settings).reduce(function (res, item) {
|
|
|
|
if (item.toJSON) { item = item.toJSON(); }
|
|
|
|
if (item.key) { res[item.key] = item.value; }
|
|
|
|
return res;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
// Turn an object into a collection
|
|
|
|
settingsCollection = function (settings) {
|
|
|
|
return _.map(settings, function (value, key) {
|
|
|
|
return { key: key, value: value };
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-20 08:22:55 +04:00
|
|
|
settings = {
|
|
|
|
browse: function (options) {
|
2013-06-08 09:05:40 +04:00
|
|
|
return dataProvider.Settings.browse(options).then(settingsObject);
|
2013-05-20 08:22:55 +04:00
|
|
|
},
|
2013-05-23 17:57:37 +04:00
|
|
|
read: function (options) {
|
2013-06-08 09:05:40 +04:00
|
|
|
return dataProvider.Settings.read(options.key).then(function (setting) {
|
|
|
|
return _.pick(setting.toJSON(), 'key', 'value');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
edit: function (settings) {
|
|
|
|
settings = settingsCollection(settings);
|
|
|
|
return dataProvider.Settings.edit(settings).then(settingsObject);
|
2013-05-20 08:22:55 +04:00
|
|
|
},
|
2013-06-08 09:05:40 +04:00
|
|
|
add: function (settings) {
|
|
|
|
settings = settingsCollection(settings);
|
|
|
|
return dataProvider.Settings.add(settings).then(settingsObject);
|
2013-05-20 08:22:55 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// categories: {};
|
2013-05-27 21:24:14 +04:00
|
|
|
// post_categories: {};
|
2013-05-16 15:21:13 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
|
2013-05-16 15:21:13 +04:00
|
|
|
// requestHandler
|
|
|
|
// decorator for api functions which are called via an HTTP request
|
|
|
|
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
|
|
|
|
requestHandler = function (apiMethod) {
|
|
|
|
return function (req, res) {
|
2013-06-02 03:45:02 +04:00
|
|
|
var options = _.extend(req.body, req.query, req.params);
|
2013-05-16 15:21:13 +04:00
|
|
|
return apiMethod(options).then(function (result) {
|
2013-05-31 09:58:20 +04:00
|
|
|
res.json(result || {});
|
2013-05-16 15:21:13 +04:00
|
|
|
}, function (error) {
|
|
|
|
res.json(400, {error: error});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.posts = posts;
|
|
|
|
module.exports.users = users;
|
2013-05-20 08:22:55 +04:00
|
|
|
module.exports.settings = settings;
|
2013-05-16 15:21:13 +04:00
|
|
|
module.exports.requestHandler = requestHandler;
|
|
|
|
}());
|