Skip to content

Commit

Permalink
feat: added multiple /tags endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter authored May 12, 2020
2 parents 784c17d + dd4fbca commit 09d8c83
Show file tree
Hide file tree
Showing 15 changed files with 765 additions and 39 deletions.
28 changes: 27 additions & 1 deletion lib/database/repositories/LogRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
*/

const { LogAdapter } = require('../adapters');
const { models: { Log } } = require('../');
const {
models: {
Log,
Tag,
},
utilities: {
QueryBuilder,
},
} = require('../');

/**
* Sequelize implementation of the LogRepository.
Expand All @@ -37,6 +45,24 @@ class LogRepository {
return Log.findAll(queryBuilder.toImplementation()).map(LogAdapter.toEntity);
}

/**
* Returns all entities.
*
* @param {Object} tagId The QueryBuilder to use.
* @param {Object} queryBuilder The QueryBuilder to use.
* @returns {Promise} Promise object representing the full mock data
*/
async findAllByTagId(tagId, queryBuilder = new QueryBuilder()) {
queryBuilder.include({
model: Tag,
as: 'tags',
required: true,
through: { where: { tag_id: tagId } },
});

return this.findAll(queryBuilder);
}

/**
* Returns a specific entity.
*
Expand Down
28 changes: 28 additions & 0 deletions lib/domain/dtos/CreateTagDto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @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.
*/

const Joi = require('@hapi/joi');

const BodyDto = Joi.object({
text: Joi.string()
.required()
.min(3),
});

const CreateTagDto = Joi.object({
query: Joi.object({}),
body: BodyDto,
params: Joi.object({}),
});

module.exports = CreateTagDto;
32 changes: 32 additions & 0 deletions lib/domain/dtos/GetTagDto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @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.
*/

const Joi = require('@hapi/joi');

const ParamsDto = Joi.object({
tagId: Joi.number()
.integer()
.positive(),
});

const QueryDto = Joi.object({
token: Joi.string(),
});

const GetLogDto = Joi.object({
body: Joi.object({}),
params: ParamsDto,
query: QueryDto,
});

module.exports = GetLogDto;
4 changes: 4 additions & 0 deletions lib/domain/dtos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
*/

const CreateLogDto = require('./CreateLogDto');
const CreateTagDto = require('./CreateTagDto');
const GetAllLogsDto = require('./GetAllLogsDto');
const GetAllTagsDto = require('./GetAllTagsDto');
const GetLogDto = require('./GetLogDto');
const GetTagDto = require('./GetTagDto');
const PaginationDto = require('./PaginationDto');

module.exports = {
CreateLogDto,
CreateTagDto,
GetAllLogsDto,
GetAllTagsDto,
GetLogDto,
GetTagDto,
PaginationDto,
};
110 changes: 77 additions & 33 deletions lib/server/controllers/tags.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
* or submit itself to any jurisdiction.
*/

const { tag: { GetAllTagsUseCase } } = require('../../usecases');
const { dtos: { GetAllTagsDto } } = require('../../domain');
const { tag: { CreateTagUseCase, GetAllTagsUseCase, GetLogsByTagUseCase, GetTagUseCase } } = require('../../usecases');
const { dtos: { CreateTagDto, GetAllTagsDto, GetTagDto } } = require('../../domain');
const { dtoValidator } = require('../utilities');

/**
* Create tag.
* Create a new tag.
*
* @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.
Expand All @@ -26,15 +26,31 @@ const { dtoValidator } = require('../utilities');
* next middleware function.
* @returns {undefined}
*/
const create = (request, response, next) => {
response.status(501).json({
errors: [
{
status: '501',
title: 'Not implemented',
},
],
});
const createTag = async (request, response, next) => {
const value = await dtoValidator(CreateTagDto, request, response);
if (!value) {
return;
}

const tag = await new CreateTagUseCase()
.execute(value);

if (tag) {
response.status(201).json({
data: tag,
});
} else {
response.status(409).json({
errors: [
{
status: '409',
source: { pointer: '/data/attributes/body/text' },
title: 'Conflict',
detail: 'The provided entity already exist',
},
],
});
}
};

/**
Expand Down Expand Up @@ -70,15 +86,29 @@ const deleteTag = (request, response, next) => {
* next middleware function.
* @returns {undefined}
*/
const getLogs = (request, response, next) => {
response.status(501).json({
errors: [
{
status: '501',
title: 'Not implemented',
},
],
});
const getLogsByTagId = async (request, response, next) => {
const value = await dtoValidator(GetTagDto, request, response);
if (!value) {
return;
}

const logs = await new GetLogsByTagUseCase()
.execute(value);

if (logs === null) {
response.status(404).json({
errors: [
{
status: '404',
title: `Tag with this id (${value.params.tagId}) could not be found`,
},
],
});
} else {
response.status(200).json({
data: logs,
});
}
};

/**
Expand Down Expand Up @@ -182,24 +212,38 @@ const patchRun = (request, response, next) => {
* next middleware function.
* @returns {undefined}
*/
const read = (request, response, next) => {
response.status(501).json({
errors: [
{
status: '501',
title: 'Not implemented',
},
],
});
const getTagById = async (request, response, next) => {
const value = await dtoValidator(GetTagDto, request, response);
if (!value) {
return;
}

const tag = await new GetTagUseCase()
.execute(value);

if (tag === null) {
response.status(404).json({
errors: [
{
status: '404',
title: `Tag with this id (${value.params.tagId}) could not be found`,
},
],
});
} else {
response.status(200).json({
data: tag,
});
}
};

module.exports = {
create,
createTag,
deleteTag,
getLogs,
getLogsByTagId,
getRuns,
getTagById,
listTags,
patchLog,
patchRun,
read,
};
9 changes: 4 additions & 5 deletions lib/server/routers/tags.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ module.exports = {
},
{
method: 'post',
path: '',
controller: TagsController.create,
controller: TagsController.createTag,
},
{
method: 'get',
path: ':id',
controller: TagsController.read,
path: ':tagId',
controller: TagsController.getTagById,
children: [
{
method: 'get',
Expand All @@ -39,7 +38,7 @@ module.exports = {
{
method: 'get',
path: '/logs',
controller: TagsController.getLogs,
controller: TagsController.getLogsByTagId,
},
{
method: 'get',
Expand Down
51 changes: 51 additions & 0 deletions lib/usecases/tag/CreateTagUseCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @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.
*/

const {
repositories: {
TagRepository,
},
utilities: {
QueryBuilder,
TransactionHelper,
},
} = require('../../database');

/**
* CreateTagUseCase
*/
class CreateTagUseCase {
/**
* Executes this use case.
*
* @param {Object} dto The CreateTagDto containing all data.
* @returns {Promise} Promise object represents the result of this use case.
*/
async execute(dto) {
const queryBuilder = new QueryBuilder();
const { body } = dto;

queryBuilder.where('text', body.text);

return TransactionHelper.provide(async () => {
const tag = await TagRepository.findOne(queryBuilder);
if (tag) {
return null;
}

return TagRepository.insert(body);
});
}
}

module.exports = CreateTagUseCase;
48 changes: 48 additions & 0 deletions lib/usecases/tag/GetLogsByTagUseCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @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.
*/

const {
repositories: {
LogRepository,
TagRepository,
},
utilities: {
QueryBuilder,
TransactionHelper,
},
} = require('../../database');

/**
* GetLogsByTagUseCase
*/
class GetLogsByTagUseCase {
/**
* Executes this use case.
*
* @param {Object} dto The GetTagDto containing all data.
* @returns {Promise} Promise object represents the result of this use case.
*/
async execute(dto) {
const queryBuilder = new QueryBuilder();
const { params } = dto;
const { tagId } = params;

queryBuilder.where('id', tagId);
return TransactionHelper.provide(async () => {
const tag = await TagRepository.findOne(queryBuilder);
return tag ? LogRepository.findAllByTagId(tag.id) : null;
});
}
}

module.exports = GetLogsByTagUseCase;
Loading

0 comments on commit 09d8c83

Please sign in to comment.