0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Merge pull request #5223 from edsadr/api-featured-posts

API: Adding `featured` filter option to posts.browse
This commit is contained in:
Hannah Wolfe 2015-05-14 11:57:44 +01:00
commit 03e5fcac66
4 changed files with 48 additions and 2 deletions

View file

@ -45,7 +45,7 @@ posts = {
* Can return posts for a particular tag by passing a tag slug in
*
* @public
* @param {{context, page, limit, status, staticPages, tag}} options (optional)
* @param {{context, page, limit, status, staticPages, tag, featured}} options (optional)
* @returns {Promise(Posts)} Posts Collection with Meta
*/
browse: function browse(options) {

View file

@ -288,7 +288,7 @@ Post = ghostBookshelf.Model.extend({
validOptions = {
findAll: ['withRelated'],
findOne: ['importing', 'withRelated'],
findPage: ['page', 'limit', 'status', 'staticPages'],
findPage: ['page', 'limit', 'status', 'staticPages', 'featured'],
add: ['importing']
};
@ -385,6 +385,14 @@ Post = ghostBookshelf.Model.extend({
options.where.page = options.staticPages;
}
if (options.featured) {
// convert string true/false to boolean
if (!_.isBoolean(options.featured)) {
options.featured = options.featured === 'true' || options.featured === '1' ? true : false;
}
options.where.featured = options.featured;
}
// Unless `all` is passed as an option, filter on
// the status provided.
if (options.status !== 'all') {

View file

@ -127,6 +127,28 @@ describe('Post API', function () {
});
});
it('can retrieve just featured posts', function (done) {
request.get(testUtils.API.getApiQuery('posts/?featured=true'))
.set('Authorization', 'Bearer ' + accesstoken)
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules['private'])
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
should.not.exist(res.headers['x-cache-invalidate']);
var jsonResponse = res.body;
jsonResponse.posts.should.exist;
testUtils.API.checkResponse(jsonResponse, 'posts');
jsonResponse.posts.should.have.length(4);
testUtils.API.checkResponse(jsonResponse.posts[0], 'post');
testUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
done();
});
});
it('can retrieve just draft posts', function (done) {
request.get(testUtils.API.getApiQuery('posts/?status=draft'))
.set('Authorization', 'Bearer ' + accesstoken)

View file

@ -158,6 +158,22 @@ describe('Post Model', function () {
paginationResult.meta.pagination.pages.should.equal(1);
paginationResult.posts.length.should.equal(1);
// Test featured pages
return PostModel.findPage({limit: 10, featured: true});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(1);
paginationResult.meta.pagination.limit.should.equal(10);
paginationResult.meta.pagination.pages.should.equal(6);
paginationResult.posts.length.should.equal(10);
// Test both boolean formats for featured pages
return PostModel.findPage({limit: 10, featured: '1'});
}).then(function (paginationResult) {
paginationResult.meta.pagination.page.should.equal(1);
paginationResult.meta.pagination.limit.should.equal(10);
paginationResult.meta.pagination.pages.should.equal(6);
paginationResult.posts.length.should.equal(10);
return PostModel.findPage({limit: 10, page: 2, status: 'all'});
}).then(function (paginationResult) {
paginationResult.meta.pagination.pages.should.equal(11);