Updating controllers to use the api + some minor changes to the api calls

This commit is contained in:
Hannah Wolfe 2013-05-16 21:56:26 +01:00
parent bb6880ea49
commit 58926d1ce4
3 changed files with 13 additions and 31 deletions

View File

@ -61,13 +61,13 @@
},
'editor': function (req, res) {
if (req.params.id !== undefined) {
api.posts.read(parseInt(req.params.id, 10))
api.posts.read({id: parseInt(req.params.id, 10)})
.then(function (post) {
res.render('editor', {
bodyClass: 'editor',
adminNav: setSelected(adminNavbar, 'blog'),
title: post.title,
content: post.content
title: post.get('title'),
content: post.get('content')
});
});
} else {
@ -83,7 +83,7 @@
res.render('blog', {
bodyClass: 'manage',
adminNav: setSelected(adminNavbar, 'blog'),
posts: posts
posts: posts.toJSON()
});
});
},

View File

@ -7,45 +7,27 @@
'use strict';
var Ghost = require('../../ghost'),
_ = require('underscore'),
api = require('../../shared/api'),
ghost = new Ghost(),
frontendControllers;
frontendControllers = {
'homepage': function (req, res) {
var featureCount = 0,
postCount = 0,
data;
ghost.dataProvider().posts.findAll(function (error, posts) {
data = _.groupBy(posts, function (post) {
var group = null;
if (post.featured === true && featureCount < ghost.config().homepage.features) {
featureCount += 1;
group = 'features';
} else if (postCount < ghost.config().homepage.posts) {
postCount += 1;
group = 'posts';
}
return group;
});
ghost.doFilter('prepostsRender', data.posts, function (posts) {
res.render('index', {features: data.features, posts: posts, ghostGlobals: ghost.globals()});
api.posts.browse().then(function (posts) {
ghost.doFilter('prePostsRender', posts.toJSON(), function (posts) {
res.render('index', {posts: posts, ghostGlobals: ghost.globals()});
});
});
},
'single': function (req, res) {
ghost.dataProvider().posts.findOne({'slug': req.params.slug}, function (error, post) {
ghost.doFilter('prePostsRender', post, function (post) {
api.posts.read({'slug': req.params.slug}).then(function (post) {
ghost.doFilter('prePostsRender', post.toJSON(), function (post) {
res.render('single', {post: post, ghostGlobals: ghost.globals()});
});
});
}
};
module.exports = frontendControllers;
}());

View File

@ -23,12 +23,12 @@
// takes filter / pagination parameters
// returns a list of posts in a json response
browse: function (options) {
return when.call(ghost.dataProvider().posts.findAll);
return when.call(ghost.dataProvider().posts.findAll, options);
},
// takes an identifier (id or slug?)
// returns a single post in a json response
read: function (id) {
return when.call(ghost.dataProvider().posts.findOne, {id: id});
read: function (args) {
return when.call(ghost.dataProvider().posts.findOne, args);
},
// takes a json object with all the properties which should be updated
// returns the resulting post in a json response