Skip to content

Commit

Permalink
feat: allow sorting on /api/logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed May 11, 2020
1 parent 1130a96 commit 3b574d9
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 4 deletions.
25 changes: 25 additions & 0 deletions lib/database/utilities/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ class QueryBuilder {
return this;
}

/**
* Set the order of elements.
*
* @param {String|Array} order Either a string or an array of strings.
* @returns {Object} The current QueryBuilder instance.
*/
orderBy(order) {
if (Array.isArray(order)) {
for (const orderElement of order) {
this.orderBy(orderElement);
}
return this;
}

if (!this.options.order) {
this.options.order = [];
}

if (order.startsWith('-')) {
this.options.order.push([order.substr(1), 'DESC']);
} else {
this.options.order.push([order, 'ASC']);
}
}

/**
* Returns the implementation specific query.
*
Expand Down
12 changes: 11 additions & 1 deletion lib/domain/dtos/GetAllLogsDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ const FilterDto = attributes({
const QueryDto = attributes({
filter: FilterDto,
page: PaginationDto,
})(class QueryDto {});
sort: {
type: Array,
itemType: String,
},
})(class QueryDto {
// eslint-disable-next-line require-jsdoc
get sort() {
const val = this.get('sort');
return !val ? val : val.join('').split(',');
}
});

const GetAllLogsDto = attributes({
query: QueryDto,
Expand Down
4 changes: 3 additions & 1 deletion lib/usecases/log/GetAllLogsUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GetAllLogsUseCase {
async execute(dto = {}) {
const queryBuilder = new QueryBuilder();
const { query = {} } = dto;
const { filter, page = {} } = query;
const { filter, page = {}, sort = ['-id'] } = query;

if (filter) {
const { origin } = filter;
Expand All @@ -47,6 +47,8 @@ class GetAllLogsUseCase {
queryBuilder.limit(limit);
queryBuilder.offset(offset);

queryBuilder.orderBy(sort);

return TransactionHelper.provide(() => LogRepository.findAll(queryBuilder));
}
}
Expand Down
14 changes: 14 additions & 0 deletions spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ paths:
- $ref: '#/components/parameters/filterOrigin'
- $ref: '#/components/parameters/pageOffset'
- $ref: '#/components/parameters/pageLimit'
- $ref: '#/components/parameters/sortLogs'
responses:
'200':
$ref: '#/components/responses/ArrayOfLogs'
Expand Down Expand Up @@ -109,6 +110,19 @@ components:
type: integer
minimum: 0
default: 0
sortLogs:
in: query
name: sort
description: The sort order of the returned items.
required: false
schema:
type: array
items:
type: string
default:
- -id
style: form
explode: false
responses:
ArrayOfLogs:
description: Expected response to a valid request.
Expand Down
44 changes: 42 additions & 2 deletions test/e2e/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ module.exports = () => {

it('should support pagination, offset 0 and limit 1', (done) => {
request(server)
.get('/api/logs?page[offset]=0&page[limit]=1')
.get('/api/logs?page[offset]=0&page[limit]=1&sort=id')
.expect(200)
.end((err, res) => {
if (err) {
Expand All @@ -129,7 +129,7 @@ module.exports = () => {

it('should support pagination, offset 1 and limit 1', (done) => {
request(server)
.get('/api/logs?page[offset]=1&page[limit]=1')
.get('/api/logs?page[offset]=1&page[limit]=1&sort=id')
.expect(200)
.end((err, res) => {
if (err) {
Expand Down Expand Up @@ -167,6 +167,46 @@ module.exports = () => {
done();
});
});

it('should support sorting, id DESC', (done) => {
request(server)
.get('/api/logs?sort=-id')
.expect(200)
.end((err, res) => {
if (err) {
done(err);
return;
}

// Response must satisfy the OpenAPI specification
expect(res).to.satisfyApiSpec;

const { data } = res.body;
expect(data[0].entryId).to.be.greaterThan(data[1].entryId);

done();
});
});

it('should support sorting, id ASC', (done) => {
request(server)
.get('/api/logs?sort=id')
.expect(200)
.end((err, res) => {
if (err) {
done(err);
return;
}

// Response must satisfy the OpenAPI specification
expect(res).to.satisfyApiSpec;

const { data } = res.body;
expect(data[1].entryId).to.be.greaterThan(data[0].entryId);

done();
});
});
});

describe('POST /api/logs', () => {
Expand Down

0 comments on commit 3b574d9

Please sign in to comment.