2015-06-15 15:38:25 +01:00
|
|
|
/**
|
|
|
|
* # Utils
|
|
|
|
* Parts of the model code which can be split out and unit tested
|
|
|
|
*/
|
2015-06-17 14:55:39 +01:00
|
|
|
var _ = require('lodash'),
|
2015-07-29 19:07:58 +01:00
|
|
|
collectionQuery,
|
|
|
|
filtering,
|
|
|
|
addPostCount;
|
|
|
|
|
|
|
|
addPostCount = function addPostCount(options, itemCollection) {
|
|
|
|
if (options.include && options.include.indexOf('post_count') > -1) {
|
|
|
|
itemCollection.query('columns', 'tags.*', function (qb) {
|
|
|
|
qb.count('posts_tags.post_id').from('posts_tags').whereRaw('tag_id = tags.id').as('post_count');
|
|
|
|
});
|
|
|
|
|
|
|
|
options.withRelated = _.pull([].concat(options.withRelated), 'post_count');
|
|
|
|
options.include = _.pull([].concat(options.include), 'post_count');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
collectionQuery = {
|
|
|
|
count: function count(collection, options) {
|
|
|
|
addPostCount(options, collection);
|
|
|
|
}
|
|
|
|
};
|
2015-06-15 15:38:25 +01:00
|
|
|
|
2015-06-17 14:55:39 +01:00
|
|
|
filtering = {
|
|
|
|
preFetch: function preFetch(filterObjects) {
|
|
|
|
var promises = [];
|
|
|
|
_.forOwn(filterObjects, function (obj) {
|
|
|
|
promises.push(obj.fetch());
|
|
|
|
});
|
2015-06-15 15:38:25 +01:00
|
|
|
|
2015-06-17 14:55:39 +01:00
|
|
|
return promises;
|
|
|
|
},
|
|
|
|
query: function query(filterObjects, itemCollection) {
|
|
|
|
if (filterObjects.tags) {
|
|
|
|
itemCollection
|
|
|
|
.query('join', 'posts_tags', 'posts_tags.post_id', '=', 'posts.id')
|
|
|
|
.query('where', 'posts_tags.tag_id', '=', filterObjects.tags.id);
|
|
|
|
}
|
2015-06-15 15:38:25 +01:00
|
|
|
|
2015-06-17 14:55:39 +01:00
|
|
|
if (filterObjects.author) {
|
|
|
|
itemCollection
|
|
|
|
.query('where', 'author_id', '=', filterObjects.author.id);
|
2015-06-15 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 14:55:39 +01:00
|
|
|
if (filterObjects.roles) {
|
|
|
|
itemCollection
|
|
|
|
.query('join', 'roles_users', 'roles_users.user_id', '=', 'users.id')
|
|
|
|
.query('where', 'roles_users.role_id', '=', filterObjects.roles.id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
formatResponse: function formatResponse(filterObjects, options, data) {
|
|
|
|
if (!_.isEmpty(filterObjects)) {
|
|
|
|
data.meta.filters = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
_.forOwn(filterObjects, function (obj, key) {
|
|
|
|
if (!filterObjects[key].isNew()) {
|
|
|
|
data.meta.filters[key] = [filterObjects[key].toJSON(options)];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2015-06-15 15:38:25 +01:00
|
|
|
};
|
2015-06-17 14:55:39 +01:00
|
|
|
|
|
|
|
module.exports.filtering = filtering;
|
2015-07-29 19:07:58 +01:00
|
|
|
module.exports.collectionQuery = collectionQuery;
|
|
|
|
module.exports.addPostCount = addPostCount;
|