Skip to content

Commit

Permalink
Implement Filter param using GQL
Browse files Browse the repository at this point in the history
refs #5604
  • Loading branch information
ErisDS committed Sep 18, 2015
1 parent 3594729 commit aad4c9f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions core/server/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ utils = {
// ### Manual Default Options
// These must be provided by the endpoint
// browseDefaultOptions - valid for all browse api endpoints
browseDefaultOptions: ['page', 'limit', 'fields'],
browseDefaultOptions: ['page', 'limit', 'fields', 'filter'],
// idDefaultOptions - valid whenever an id is valid
idDefaultOptions: ['id'],

Expand Down Expand Up @@ -116,7 +116,7 @@ utils = {
name: {}
},
// these values are sanitised/validated separately
noValidation = ['data', 'context', 'include'],
noValidation = ['data', 'context', 'include', 'filter'],
errors = [];

_.each(options, function (value, key) {
Expand Down
11 changes: 11 additions & 0 deletions core/server/models/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ = require('lodash'),
validation = require('../../data/validation'),
baseUtils = require('./utils'),
pagination = require('./pagination'),
gql = require('ghost-gql'),

ghostBookshelf;

Expand Down Expand Up @@ -283,6 +284,16 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
itemCollection.query('where', options.where);
}

// Apply FILTER
if (options.filter) {
options.filter = gql.parse(options.filter);
itemCollection.query(function (qb) {
gql.knexify(qb, options.filter);
});

baseUtils.filtering.joins(options.filter.joins, itemCollection);
}

// Setup filter joins / queries
baseUtils.filtering.query(filterObjects, itemCollection);

Expand Down
14 changes: 14 additions & 0 deletions core/server/models/base/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ filtering = {
.query('where', 'roles_users.role_id', '=', filterObjects.roles.id);
}
},
joins: function joins(joinTables, itemCollection) {
if (joinTables && joinTables.indexOf('tags') > -1) {
itemCollection
.query('join', 'posts_tags', 'posts_tags.post_id', '=', 'posts.id')
.query('join', 'tags', 'posts_tags.tag_id', '=', 'tags.id')
.query('groupBy', 'posts.id')
.query('orderByRaw', 'count(tags.id) DESC');
}

if (joinTables && joinTables.indexOf('author') > -1) {
itemCollection
.query('join', 'users as author', 'author.id', '=', 'posts.author_id');
}
},
formatResponse: function formatResponse(filterObjects, options, data) {
if (!_.isEmpty(filterObjects)) {
data.meta.filters = {};
Expand Down
8 changes: 4 additions & 4 deletions core/server/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,23 +358,23 @@ Post = ghostBookshelf.Model.extend({
if (!_.isBoolean(options.staticPages)) {
options.staticPages = _.contains(['true', '1'], options.staticPages);
}
options.where.page = options.staticPages;
options.where['posts.page'] = options.staticPages;
}

if (_.has(options, 'featured')) {
// convert string true/false to boolean
if (!_.isBoolean(options.featured)) {
options.featured = _.contains(['true', '1'], options.featured);
}
options.where.featured = options.featured;
options.where['posts.featured'] = options.featured;
}

// Unless `all` is passed as an option, filter on
// the status provided.
if (options.status !== 'all') {
// make sure that status is valid
options.status = _.contains(['published', 'draft'], options.status) ? options.status : 'published';
options.where.status = options.status;
options.where['posts.status'] = options.status;
}

return options;
Expand All @@ -393,7 +393,7 @@ Post = ghostBookshelf.Model.extend({
validOptions = {
findAll: ['withRelated'],
findOne: ['importing', 'withRelated'],
findPage: ['page', 'limit', 'columns', 'status', 'staticPages', 'featured'],
findPage: ['page', 'limit', 'columns', 'filter', 'status', 'staticPages', 'featured'],
add: ['importing']
};

Expand Down
2 changes: 1 addition & 1 deletion core/test/unit/api_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('API Utils', function () {
describe('Default Options', function () {
it('should provide a set of default options', function () {
apiUtils.globalDefaultOptions.should.eql(['context', 'include']);
apiUtils.browseDefaultOptions.should.eql(['page', 'limit', 'fields']);
apiUtils.browseDefaultOptions.should.eql(['page', 'limit', 'fields', 'filter']);
apiUtils.dataDefaultOptions.should.eql(['data']);
apiUtils.idDefaultOptions.should.eql(['id']);
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"express-hbs": "0.8.4",
"extract-zip": "1.0.3",
"fs-extra": "0.18.4",
"ghost-gql": "ErisDS/GQL.git#initial",
"glob": "4.3.2",
"html-to-text": "1.3.0",
"intl": "1.0.0",
Expand Down

0 comments on commit aad4c9f

Please sign in to comment.