Moved raw order for author filtering to correct place (#10166) (#10171)

refs #10105

- ordering !== filtering
This commit is contained in:
Katharina Irrgang 2018-11-15 16:17:51 +01:00 committed by GitHub
parent e89a27f3ab
commit 090a936e16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 10 deletions

View File

@ -139,15 +139,6 @@ filter = function filter(Bookshelf) {
.query('leftOuterJoin', 'posts_authors', 'posts_authors.post_id', '=', 'posts.id')
.query('leftOuterJoin', 'users as authors', 'posts_authors.author_id', '=', 'authors.id');
// The order override should ONLY happen if we are doing an "IN" query
// TODO move the order handling to the query building that is currently inside pagination
// TODO make the order handling in pagination handle orderByRaw
// TODO extend this handling to all joins
if (gql.json.findStatement(this._filters.statements, {prop: /^authors/, op: 'IN'})) {
// TODO make this count the number of MATCHING authors, not just the number of authors
this.query('orderByRaw', 'count(authors.id) DESC');
}
// We need to add a group by to counter the double left outer join
// TODO improve on the group by handling
options.groups = options.groups || [];

View File

@ -590,6 +590,11 @@ Post = ghostBookshelf.Model.extend({
order = `count(tags.id) DESC, ${order}`;
}
// CASE: if the filter contains an `IN` operator, we should return the posts first, which match both authors
if (options.filter && options.filter.match(/(authors|author):\s?\[.*\]/)) {
order = `count(authors.id) DESC, ${order}`;
}
return order;
},

View File

@ -86,7 +86,7 @@ describe('Unit: models/post', function () {
withRelated: ['authors', 'tags']
}).then(() => {
queries.length.should.eql(2);
queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null)) order by count(authors.id) DESC');
queries[0].sql.should.eql('select count(distinct posts.id) as aggregate from `posts` left outer join `posts_tags` on `posts_tags`.`post_id` = `posts`.`id` left outer join `tags` on `posts_tags`.`tag_id` = `tags`.`id` left outer join `posts_authors` on `posts_authors`.`post_id` = `posts`.`id` left outer join `users` as `authors` on `posts_authors`.`author_id` = `authors`.`id` where (`posts`.`page` = ? and `posts`.`status` = ?) and (`authors`.`slug` in (?, ?) and (`tags`.`slug` = ? or `posts`.`feature_image` is not null))');
queries[0].bindings.should.eql([
false,
'published',