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:
commit
03e5fcac66
4 changed files with 48 additions and 2 deletions
|
@ -45,7 +45,7 @@ posts = {
|
||||||
* Can return posts for a particular tag by passing a tag slug in
|
* Can return posts for a particular tag by passing a tag slug in
|
||||||
*
|
*
|
||||||
* @public
|
* @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
|
* @returns {Promise(Posts)} Posts Collection with Meta
|
||||||
*/
|
*/
|
||||||
browse: function browse(options) {
|
browse: function browse(options) {
|
||||||
|
|
|
@ -288,7 +288,7 @@ Post = ghostBookshelf.Model.extend({
|
||||||
validOptions = {
|
validOptions = {
|
||||||
findAll: ['withRelated'],
|
findAll: ['withRelated'],
|
||||||
findOne: ['importing', 'withRelated'],
|
findOne: ['importing', 'withRelated'],
|
||||||
findPage: ['page', 'limit', 'status', 'staticPages'],
|
findPage: ['page', 'limit', 'status', 'staticPages', 'featured'],
|
||||||
add: ['importing']
|
add: ['importing']
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -385,6 +385,14 @@ Post = ghostBookshelf.Model.extend({
|
||||||
options.where.page = options.staticPages;
|
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
|
// Unless `all` is passed as an option, filter on
|
||||||
// the status provided.
|
// the status provided.
|
||||||
if (options.status !== 'all') {
|
if (options.status !== 'all') {
|
||||||
|
|
|
@ -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) {
|
it('can retrieve just draft posts', function (done) {
|
||||||
request.get(testUtils.API.getApiQuery('posts/?status=draft'))
|
request.get(testUtils.API.getApiQuery('posts/?status=draft'))
|
||||||
.set('Authorization', 'Bearer ' + accesstoken)
|
.set('Authorization', 'Bearer ' + accesstoken)
|
||||||
|
|
|
@ -158,6 +158,22 @@ describe('Post Model', function () {
|
||||||
paginationResult.meta.pagination.pages.should.equal(1);
|
paginationResult.meta.pagination.pages.should.equal(1);
|
||||||
paginationResult.posts.length.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'});
|
return PostModel.findPage({limit: 10, page: 2, status: 'all'});
|
||||||
}).then(function (paginationResult) {
|
}).then(function (paginationResult) {
|
||||||
paginationResult.meta.pagination.pages.should.equal(11);
|
paginationResult.meta.pagination.pages.should.equal(11);
|
||||||
|
|
Loading…
Reference in a new issue