Skip to content

Commit

Permalink
feat: added meta section to GET /api/logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed May 18, 2020
1 parent 3e92b10 commit b50cc92
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
14 changes: 14 additions & 0 deletions lib/database/repositories/LogRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ class LogRepository {
return Log.findAll(queryBuilder.toImplementation()).map(LogAdapter.toEntity);
}

/**
* Returns all entities.
*
* @param {Object} queryBuilder The QueryBuilder to use.
* @returns {Promise} Promise object representing the full mock data
*/
async findAndCountAll(queryBuilder) {
const { count, rows } = await Log.findAndCountAll(queryBuilder.toImplementation());
return {
count,
logs: rows.map(LogAdapter.toEntity),
};
}

/**
* Returns all entities.
*
Expand Down
11 changes: 10 additions & 1 deletion lib/server/controllers/logs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,19 @@ const listLogs = async (request, response, next) => {
return;
}

const logs = await new GetAllLogsUseCase()
const { count, logs } = await new GetAllLogsUseCase()
.execute(value);

const { query: { page: { limit = 100 } = {} } } = value;
const totalPages = Math.ceil(count / limit);

response.status(200).json({
data: logs,
meta: {
page: {
total: totalPages,
},
},
});
};

Expand Down
2 changes: 1 addition & 1 deletion lib/usecases/log/GetAllLogsUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class GetAllLogsUseCase {

queryBuilder.include('tags');

return TransactionHelper.provide(() => LogRepository.findAll(queryBuilder));
return TransactionHelper.provide(() => LogRepository.findAndCountAll(queryBuilder));
}
}

Expand Down
22 changes: 22 additions & 0 deletions spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,22 @@ components:
description: Response containing multiple logs.
type: object
properties:
meta:
$ref: '#/components/schemas/ArrayOfLogsResponseMeta'
data:
$ref: '#/components/schemas/ArrayOfLogs'
required:
- data
additionalProperties: false
ArrayOfLogsResponseMeta:
description: The metadata related to an array of logs response.
type: object
properties:
page:
$ref: '#/components/schemas/PaginationMeta'
required:
- page
additionalProperties: false
ArrayOfTags:
description: A list of Tag objects.
type: array
Expand Down Expand Up @@ -469,6 +480,17 @@ components:
minimum: 1
maximum: 100
default: 100
PaginationMeta:
description: The metadata related to pagination.
type: object
properties:
total:
description: The number of pages which contain data.
type: integer
minimum: 1
required:
- total
additionalProperties: false
PaginationOffset:
description: The number of items to skip before starting to collect the result set.
type: integer
Expand Down
10 changes: 5 additions & 5 deletions test/usecases/log/GetAllLogsUseCase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ module.exports = () => {
});

it('should return an array', async () => {
const result = await new GetAllLogsUseCase()
const { logs } = await new GetAllLogsUseCase()
.execute();

expect(result).to.be.an('array');
expect(logs).to.be.an('array');
});

it('should return an array, only containing human originated logs', async () => {
getAllLogsDto.query = { filter: { origin: 'human' } };
const result = await new GetAllLogsUseCase()
const { logs } = await new GetAllLogsUseCase()
.execute(getAllLogsDto);

expect(result).to.be.an('array');
for (const log of result) {
expect(logs).to.be.an('array');
for (const log of logs) {
expect(log.origin).to.equal('human');
}
});
Expand Down

0 comments on commit b50cc92

Please sign in to comment.