Skip to content

Commit

Permalink
feat: added GET /logs/{logId}/tags endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter authored May 11, 2020
2 parents f20dcb0 + cdac4ef commit b15508e
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 31 deletions.
40 changes: 40 additions & 0 deletions lib/database/migrations/20200511132541-log-tags-relation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/

module.exports = {
up: (queryInterface, _Sequelize) => queryInterface.sequelize.transaction((t) => Promise.all([
queryInterface.addConstraint('log_tags', {
fields: ['log_id'],
type: 'foreign key',
name: 'fk_log_id_log_tags',
references: {
table: 'logs',
field: 'id',
},
}, { transaction: t }),
queryInterface.addConstraint('log_tags', {
fields: ['tag_id'],
type: 'foreign key',
name: 'fk_tag_id_log_tags',
references: {
table: 'tags',
field: 'id',
},
}, { transaction: t }),
])),

down: (queryInterface, _Sequelize) => queryInterface.sequelize.transaction((t) => Promise.all([
queryInterface.removeConstraint('log_tags', 'fk_log_id_log_tags', { transaction: t }),
queryInterface.removeConstraint('log_tags', 'fk_tag_id_log_tags', { transaction: t }),
])),
};
40 changes: 40 additions & 0 deletions lib/database/migrations/20200511132542-log-runs-relation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/

module.exports = {
up: (queryInterface, _Sequelize) => queryInterface.sequelize.transaction((t) => Promise.all([
queryInterface.addConstraint('log_runs', {
fields: ['log_id'],
type: 'foreign key',
name: 'fk_log_id_log_runs',
references: {
table: 'logs',
field: 'id',
},
}, { transaction: t }),
queryInterface.addConstraint('log_runs', {
fields: ['run_id'],
type: 'foreign key',
name: 'fk_run_id_log_runs',
references: {
table: 'runs',
field: 'id',
},
}, { transaction: t }),
])),

down: (queryInterface, _Sequelize) => queryInterface.sequelize.transaction((t) => Promise.all([
queryInterface.removeConstraint('log_runs', 'fk_log_id_log_runs', { transaction: t }),
queryInterface.removeConstraint('log_runs', 'fk_run_id_log_runs', { transaction: t }),
])),
};
2 changes: 1 addition & 1 deletion lib/domain/dtos/GetLogDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const Joi = require('@hapi/joi');

const ParamsDto = Joi.object({
id: Joi.number()
logId: Joi.number()
.integer()
.positive(),
});
Expand Down
39 changes: 38 additions & 1 deletion lib/server/controllers/logs.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,42 @@ const listLogs = async (request, response, next) => {
});
};

/**
* Lists all tags associated with a log.
*
* @param {Object} request The *request* object represents the HTTP request and has properties for the request query
* string, parameters, body, HTTP headers, and so on.
* @param {Object} response The *response* object represents the HTTP response that an Express app sends when it gets an
* HTTP request.
* @param {Function} next The *next* object represents the next middleware function which is used to pass control to the
* next middleware function.
* @returns {undefined}
*/
const listTagsByLogId = async (request, response, next) => {
const value = await dtoValidator(GetLogDto, request, response);
if (!value) {
return;
}

const log = await new GetLogUseCase()
.execute(value);

if (log === null) {
response.status(404).json({
errors: [
{
status: '404',
title: `Log with this id (${request.params.logId}) could not be found`,
},
],
});
} else {
response.status(200).json({
data: log.tags,
});
}
};

/**
* Patch run on log.
*
Expand Down Expand Up @@ -155,7 +191,7 @@ const getLogById = async (request, response, next) => {
errors: [
{
status: '404',
title: `Log with this id (${value.params.id}) could not be found`,
title: `Log with this id (${value.params.logId}) could not be found`,
},
],
});
Expand Down Expand Up @@ -193,6 +229,7 @@ module.exports = {
createAttachment,
getAttachment,
listLogs,
listTagsByLogId,
getLogById,
patchAttachment,
patchRun,
Expand Down
7 changes: 6 additions & 1 deletion lib/server/routers/logs.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
},
{
method: 'get',
path: ':id',
path: ':logId',
controller: LogsController.getLogById,
children: [
{
Expand All @@ -44,6 +44,11 @@ module.exports = {
path: 'runs',
controller: LogsController.patchRun,
},
{
method: 'get',
path: 'tags',
controller: LogsController.listTagsByLogId,
},
],
},
],
Expand Down
4 changes: 2 additions & 2 deletions lib/usecases/log/GetLogUseCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class GetLogUseCase {
*/
async execute(dto) {
const { params } = dto;
const { id } = params;
return TransactionHelper.provide(() => LogRepository.findOneById(id));
const { logId } = params;
return TransactionHelper.provide(() => LogRepository.findOneById(logId));
}
}

Expand Down
82 changes: 66 additions & 16 deletions spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ paths:
$ref: '#/components/schemas/ApiInformation'
'503':
$ref: '#/components/responses/ServiceUnavailable'
default:
$ref: '#/components/responses/UnexpectedError'
/logs:
get:
operationId: listLogs
summary: List all logs
parameters:
- $ref: '#/components/parameters/filterOrigin'
- $ref: '#/components/parameters/pageOffset'
- $ref: '#/components/parameters/pageLimit'
- $ref: '#/components/parameters/sortLogs'
- $ref: '#/components/parameters/FilterOrigin'
- $ref: '#/components/parameters/PageOffset'
- $ref: '#/components/parameters/PageLimit'
- $ref: '#/components/parameters/SortLogs'
responses:
'200':
$ref: '#/components/responses/ArrayOfLogs'
'400':
$ref: '#/components/responses/BadRequest'
default:
$ref: '#/components/responses/UnexpectedError'
tags:
- log
post:
Expand All @@ -45,17 +49,13 @@ paths:
$ref: '#/components/responses/Log'
'400':
$ref: '#/components/responses/BadRequest'
default:
$ref: '#/components/responses/UnexpectedError'
tags:
- log
/logs/{id}:
/logs/{logId}:
parameters:
- name: id
in: path
description: The id of the log to retrieve
required: true
schema:
type: integer
format: int64
- $ref: '#/components/parameters/LogId'
get:
operationId: getLogById
summary: Gets a log by Id
Expand All @@ -66,8 +66,28 @@ paths:
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
default:
$ref: '#/components/responses/UnexpectedError'
tags:
- log
/logs/{logId}/tags:
parameters:
- $ref: '#/components/parameters/LogId'
get:
operationId: listTagsByLogId
summary: Lists all tags associated with a log
responses:
'200':
$ref: '#/components/responses/ArrayOfTags'
'400':
$ref: '#/components/responses/BadRequest'
'404':
$ref: '#/components/responses/NotFound'
default:
$ref: '#/components/responses/UnexpectedError'
tags:
- log
- tag
/status:
get:
operationId: getDeployInformation
Expand All @@ -81,17 +101,27 @@ paths:
$ref: '#/components/schemas/DeployInformation'
'503':
$ref: '#/components/responses/ServiceUnavailable'
default:
$ref: '#/components/responses/UnexpectedError'

components:
parameters:
filterOrigin:
FilterOrigin:
in: query
name: filter[origin]
description: Filter logs by their origin
schema:
type: string
enum: [human, process]
pageLimit:
LogId:
name: logId
description: The id of the log to retrieve
in: path
required: true
schema:
type: integer
format: int64
PageLimit:
in: query
name: page[limit]
description: The numbers of items to return.
Expand All @@ -101,7 +131,7 @@ components:
minimum: 1
maximum: 100
default: 100
pageOffset:
PageOffset:
in: query
name: page[offset]
description: The number of items to skip before starting to collect the result set.
Expand All @@ -110,7 +140,7 @@ components:
type: integer
minimum: 0
default: 0
sortLogs:
SortLogs:
in: query
name: sort
description: The sort order of the returned items.
Expand All @@ -130,6 +160,12 @@ components:
application/json:
schema:
$ref: '#/components/schemas/ArrayOfLogsResponse'
ArrayOfTags:
description: Expected response to a valid request.
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayOfTagsResponse'
BadRequest:
description: Bad Request
content:
Expand All @@ -154,6 +190,12 @@ components:
application/json:
schema:
$ref: '#/components/schemas/Errors'
UnexpectedError:
description: Unexpected Error
content:
application/json:
schema:
$ref: '#/components/schemas/Errors'
schemas:
ApiInformation:
description: API information
Expand Down Expand Up @@ -186,6 +228,14 @@ components:
type: array
items:
$ref: '#/components/schemas/Tag'
ArrayOfTagsResponse:
description: Response containing multiple tags.
type: object
properties:
data:
$ref: '#/components/schemas/ArrayOfTags'
required:
- data
DeployInformation:
description: Deploy information
type: object
Expand Down
2 changes: 1 addition & 1 deletion test/application/usecases/log/GetLogUseCase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = () => {
beforeEach(async () => {
getLogDto = await GetLogDto.validateAsync({
params: {
id: 1,
logId: 1,
},
});
});
Expand Down
Loading

0 comments on commit b15508e

Please sign in to comment.