Skip to content

Commit

Permalink
feat: [CU-220ekmx] provide sort and pagination support in findAll and…
Browse files Browse the repository at this point in the history
… findAllInHierarchy methods
  • Loading branch information
cyp3rius committed Feb 8, 2022
1 parent 58a80ba commit 5c1ab4f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
8 changes: 4 additions & 4 deletions server/controllers/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ module.exports = {
},

async findAllFlat(ctx) {
const { params = {}, query } = ctx;
const { params = {}, query, sort, pagination } = ctx;
const { relation } = parseParams(params);
try {
return this.getService('common')
.findAllFlat(flatInput(relation, query));
.findAllFlat(flatInput(relation, query, sort, pagination));
} catch (e) {
throwError(ctx, e);
}
},

async findAllInHierarchy(ctx) {
const { params = {}, query } = ctx;
const { params = {}, query, sort } = ctx;
const { relation } = parseParams(params);
try {
return await this.getService('common')
.findAllInHierarchy({
...flatInput(relation, query),
...flatInput(relation, query, sort),
dropBlockedThreads: true,
});
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion server/controllers/utils/parsers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

module.exports = {
flatInput(relation, query) {
flatInput(relation, query, sort, pagination) {
return {
query: {
...query,
Expand All @@ -13,6 +13,8 @@ module.exports = {
populate: { authorUser: true },
},
},
pagination,
sort,
};
},

Expand Down
6 changes: 4 additions & 2 deletions server/graphql/queries/findAllFlat.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ module.exports = ({ strapi, nexus }) => {
sort: args.SortArg,
},
async resolve(obj, args) {
const { relation, filters } = args;
const { relation, filters, sort, pagination } = args;
return await getPluginService('common')
.findAllFlat(flatInput(
relation,
getPluginService('gql').graphQLFiltersToStrapiQuery(filters, contentType)
getPluginService('gql').graphQLFiltersToStrapiQuery(filters, contentType),
sort,
pagination
), undefined);
},
};
Expand Down
6 changes: 3 additions & 3 deletions server/graphql/queries/findAllInHierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ module.exports = ({ strapi, nexus }) => {
type: nonNull(list('CommentNested')),
args: {
relation: nonNull(stringArg()),
// filters: filtersArg,
sort: args.SortArg,
},
async resolve(obj, args) {
const { relation, filters } = args;
const { relation, filters, sort } = args;
return await getPluginService('common')
.findAllInHierarchy({
...flatInput(
relation,
getPluginService('gql').graphQLFiltersToStrapiQuery(filters, contentType)
getPluginService('gql').graphQLFiltersToStrapiQuery(filters, contentType),
sort
),
dropBlockedThreads: true,
});
Expand Down
35 changes: 31 additions & 4 deletions server/services/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const BadWordsFilter = require('bad-words');
const { isArray, isNumber, isObject, isNil, first, parseInt } = require('lodash');
const { isArray, isNumber, isObject, isNil, isString, first, parseInt, set } = require('lodash');
const { REGEX } = require('../utils/constants');
const PluginError = require('./../utils/error');
const {
getModelUid,
Expand All @@ -27,12 +28,36 @@ module.exports = ({ strapi }) => ({
},

// Find comments in the flat structure
async findAllFlat({ query = {}, populate = {} }, relatedEntity = null) {

async findAllFlat({ query = {}, populate = {}, sort, pagination }, relatedEntity = null) {
const defaultPopulate = {
authorUser: true,
};

let orderingAndPagination = {};

if (sort && (isString(sort) || isArray(sort))) {
orderingAndPagination = {
...orderingAndPagination,
orderBy: (isString(sort) ? [sort] : sort)
.map(_ => REGEX.sorting.test(_) ? _ : `${_}:asc`)
.reduce((prev, curr) => {
const [type = 'asc', ...parts] = curr.split(':').reverse();
return { ...set(prev, parts.reverse().join('.'), type) };
}, {})
};
}

if (pagination && isObject(pagination)) {
const { page = 1, pageSize = 10 } = pagination;
orderingAndPagination = {
...orderingAndPagination,
offset: (page - 1) * pageSize,
limit: pageSize,
};
}

console.log(orderingAndPagination);

const entries = await strapi.db.query(getModelUid('comment'))
.findMany({
where: {
Expand All @@ -42,6 +67,7 @@ module.exports = ({ strapi }) => ({
...defaultPopulate,
...populate,
},
...orderingAndPagination,
});

const entriesWithThreads = await Promise.all(entries.map(async _ => {
Expand Down Expand Up @@ -76,10 +102,11 @@ module.exports = ({ strapi }) => ({
async findAllInHierarchy ({
query,
populate = {},
sort,
startingFromId = null,
dropBlockedThreads = false,
}, relatedEntity) {
const entities = await this.findAllFlat({ query, populate }, relatedEntity);
const entities = await this.findAllFlat({ query, populate, sort }, relatedEntity);
return buildNestedStructure(entities, startingFromId, 'threadOf', dropBlockedThreads, false);
},

Expand Down
1 change: 1 addition & 0 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const REGEX = {
uid: /^(?<type>[a-z0-9-]+)\:{2}(?<api>[a-z0-9-]+)\.{1}(?<contentType>[a-z0-9-]+)$/i,
relatedUid: /^(?<uid>[a-z0-9-]+\:{2}[a-z0-9-]+\.[a-z0-9-]+)\:{1}(?<id>[a-z0-9-]+)$/i,
email: /\S+@\S+\.\S+/,
sorting: /^(?<path>[a-z0-9-_\:\.]+)\:+(asc|desc)$/i
};

module.exports = {
Expand Down

0 comments on commit 5c1ab4f

Please sign in to comment.