🐛 Fixed default resource ordering in Content API (#10371)

refs https://github.com/TryGhost/Ghost/issues/10354

- Added ordering on input serialization layer for posts, pages, tags, and authors
- At the moment ordering is dependent on DB engine which will be resolved with https://github.com/TryGhost/Ghost/issues/6104
This commit is contained in:
Naz Gargol 2019-01-15 12:21:04 +00:00 committed by GitHub
parent 3924acd152
commit 1a4497fc9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 98 additions and 1 deletions

View File

@ -0,0 +1,26 @@
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:authors');
const utils = require('../../index');
function setDefaultOrder(frame) {
if (!frame.options.order) {
frame.options.order = 'name asc';
}
}
module.exports = {
browse(apiConfig, frame) {
debug('browse');
if (utils.isContentAPI(frame)) {
setDefaultOrder(frame);
}
},
read(apiConfig, frame) {
debug('read');
if (utils.isContentAPI(frame)) {
setDefaultOrder(frame);
}
}
};

View File

@ -1,3 +1,4 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:pages');
function removeMobiledocFormat(frame) {
@ -8,6 +9,19 @@ function removeMobiledocFormat(frame) {
}
}
function setDefaultOrder(frame) {
let includesOrderedRelations = false;
if (frame.options.withRelated) {
const orderedRelations = ['author', 'authors', 'tag', 'tags'];
includesOrderedRelations = _.intersection(orderedRelations, frame.options.withRelated).length > 0;
}
if (!frame.options.order && !includesOrderedRelations) {
frame.options.order = 'title asc';
}
}
module.exports = {
browse(apiConfig, frame) {
debug('browse');
@ -28,6 +42,8 @@ module.exports = {
removeMobiledocFormat(frame);
setDefaultOrder(frame);
debug(frame.options);
},
@ -37,6 +53,8 @@ module.exports = {
frame.data.page = true;
removeMobiledocFormat(frame);
setDefaultOrder(frame);
debug(frame.options);
}
};

View File

@ -20,6 +20,19 @@ function includeTags(frame) {
}
}
function setDefaultOrder(frame) {
let includesOrderedRelations = false;
if (frame.options.withRelated) {
const orderedRelations = ['author', 'authors', 'tag', 'tags'];
includesOrderedRelations = _.intersection(orderedRelations, frame.options.withRelated).length > 0;
}
if (!frame.options.order && !includesOrderedRelations) {
frame.options.order = 'published_at desc';
}
}
module.exports = {
browse(apiConfig, frame) {
debug('browse');
@ -53,6 +66,8 @@ module.exports = {
if (labs.isSet('members')) {
includeTags(frame);
}
setDefaultOrder(frame);
}
debug(frame.options);
@ -76,6 +91,8 @@ module.exports = {
// CASE: Members needs to have the tags to check if its allowed access
includeTags(frame);
}
setDefaultOrder(frame);
}
debug(frame.options);

View File

@ -1,7 +1,28 @@
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:tags');
const url = require('./utils/url');
const utils = require('../../index');
function setDefaultOrder(frame) {
if (!frame.options.order) {
frame.options.order = 'name asc';
}
}
module.exports = {
browse(apiConfig, frame) {
debug('browse');
if (utils.isContentAPI(frame)) {
setDefaultOrder(frame);
}
},
read() {
debug('read');
this.browse(...arguments);
},
add(apiConfig, frame) {
debug('add');
frame.data.tags[0] = url.forTag(Object.assign({}, frame.data.tags[0]));

View File

@ -50,6 +50,10 @@ describe('Authors Content API V2', function () {
// We don't expose the email address, status and other attrs.
localUtils.API.checkResponse(jsonResponse.authors[0], 'author', ['url'], null, null);
// Default order 'name asc' check
jsonResponse.authors[0].name.should.eql('Ghost');
jsonResponse.authors[2].name.should.eql('Slimer McEctoplasm');
should.exist(res.body.authors[0].url);
should.exist(url.parse(res.body.authors[0].url).protocol);
should.exist(url.parse(res.body.authors[0].url).host);

View File

@ -52,7 +52,7 @@ describe('Posts', function () {
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
_.isBoolean(jsonResponse.posts[0].featured).should.eql(true);
// Default order check
// Default order 'published_at desc' check
jsonResponse.posts[0].slug.should.eql('welcome');
jsonResponse.posts[6].slug.should.eql('themes');

View File

@ -46,6 +46,17 @@ describe('Tags Content API V2', function () {
localUtils.API.checkResponse(jsonResponse.tags[0], 'tag', ['url']);
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
// Default order 'name asc' check
// the ordering difference is described in https://github.com/TryGhost/Ghost/issues/6104
// this condition should be removed once issue mentioned above ^ is resolved
if (process.env.NODE_ENV === 'testing-mysql') {
jsonResponse.tags[0].name.should.eql('bacon');
jsonResponse.tags[3].name.should.eql('kitchen sink');
} else {
jsonResponse.tags[0].name.should.eql('Getting Started');
jsonResponse.tags[3].name.should.eql('kitchen sink');
}
should.exist(res.body.tags[0].url);
should.exist(url.parse(res.body.tags[0].url).protocol);
should.exist(url.parse(res.body.tags[0].url).host);