Skip to content

Commit

Permalink
feat: added GET /api/subystems endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DdeHoog authored and mvegter committed Jun 6, 2020
1 parent 035c0bb commit 986e7a3
Show file tree
Hide file tree
Showing 19 changed files with 578 additions and 10 deletions.
39 changes: 39 additions & 0 deletions lib/database/adapters/SubsystemAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @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.
*/

/**
* SubsystemAdapter
*/
class SubsystemAdapter {
/**
* Converts the given database object to an entity object.
*
* @param {Object} databaseObject Object to convert.
* @returns {Object} Converted entity object.
*/
static toEntity({ id, name }) {
return { id, name };
}

/**
* Converts the given entity object to a database object.
*
* @param {Object} entityObject Object to convert.
* @returns {Object} Converted database object.
*/
static toDatabase(entityObject) {
return entityObject;
}
}

module.exports = SubsystemAdapter;
2 changes: 2 additions & 0 deletions lib/database/adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*/

const LogAdapter = require('./LogAdapter');
const SubsystemAdapter = require('./SubsystemAdapter');
const TagAdapter = require('./TagAdapter');

module.exports = {
LogAdapter,
SubsystemAdapter,
TagAdapter,
};
92 changes: 92 additions & 0 deletions lib/database/repositories/SubsystemRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @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 { SubsystemAdapter } = require('../adapters');
const {
models: {
Subsystem,
},
} = require('../');

/**
* Sequelize implementation of the SubsystemRepository.
*/
class SubsystemRepository {
/**
* Returns all entities.
*
* @param {QueryBuilder} queryBuilder The QueryBuilder to use.
* @returns {Promise} Promise object representing the full mock data
*/
async findAll(queryBuilder) {
return Subsystem.findAll(queryBuilder.toImplementation()).map(SubsystemAdapter.toEntity);
}

/**
* Returns all entities.
*
* @param {QueryBuilder} queryBuilder The QueryBuilder to use.
* @returns {Promise} Promise object representing the full mock data
*/
async findAndCountAll(queryBuilder) {
queryBuilder.set('distinct', true);

const { count, rows } = await Subsystem.findAndCountAll(queryBuilder.toImplementation());
return {
count,
subsystems: rows.map(SubsystemAdapter.toEntity),
};
}

/**
* Returns a specific entity.
*
* @param {QueryBuilder} queryBuilder The QueryBuilder to use.
* @returns {Promise|Null} Promise object representing the full mock data
*/
async findOne(queryBuilder) {
queryBuilder.limit(1);
const result = await Subsystem.findOne(queryBuilder.toImplementation());
return result ? SubsystemAdapter.toEntity(result) : null;
}

/**
* Inserts a specific entity.
*
* @param {Object} entity The entity to insert.
* @returns {Promise|Null} Promise object representing the full mock data
*/
async insert(entity){
const result = await Subsystem.create(SubsystemAdapter.toDatabase(entity));
return SubsystemAdapter.toEntity(result);
}

/**
* Removes a specific entity.
*
* @param {QueryBuilder} queryBuilder The QueryBuilder to use.
* @returns {Promise|Null} Promise object representing the full mock data
*/
async removeOne(queryBuilder) {
queryBuilder.limit(1);

const tag = await this.findOne(queryBuilder);
if (tag) {
await Subsystem.destroy(queryBuilder.toImplementation());
}

return tag;
}
}

module.exports = new SubsystemRepository();
2 changes: 2 additions & 0 deletions lib/database/repositories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
*/

const LogRepository = require('./LogRepository');
const SubsystemRepository = require('./SubsystemRepository');
const TagRepository = require('./TagRepository');

module.exports = {
LogRepository,
SubsystemRepository,
TagRepository,
};
28 changes: 28 additions & 0 deletions lib/database/seeders/20200606151010-subsystems.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.
*/

module.exports = {
up: (queryInterface, _Sequelize) => queryInterface.bulkInsert('subsystems', [
{
name: 'Subsystem Plant #1',
},
{
name: 'Subsystem Plant #2',
},
{
name: 'Subsystem Plant #3',
},
]),

down: (queryInterface, _Sequelize) => queryInterface.bulkDelete('subsystems', null, {}),
};
28 changes: 28 additions & 0 deletions lib/domain/dtos/GetAllSubsystemsDto.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 PaginationDto = require('./PaginationDto');

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

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

module.exports = GetAllSubsystemsDto;
2 changes: 2 additions & 0 deletions lib/domain/dtos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const CreateLogDto = require('./CreateLogDto');
const CreateTagDto = require('./CreateTagDto');
const EntityIdDto = require('./EntityIdDto');
const GetAllLogsDto = require('./GetAllLogsDto');
const GetAllSubsystemsDto = require('./GetAllSubsystemsDto');
const GetAllTagsDto = require('./GetAllTagsDto');
const GetLogDto = require('./GetLogDto');
const GetTagDto = require('./GetTagDto');
Expand All @@ -25,6 +26,7 @@ module.exports = {
CreateTagDto,
EntityIdDto,
GetAllLogsDto,
GetAllSubsystemsDto,
GetAllTagsDto,
GetLogDto,
GetTagDto,
Expand Down
38 changes: 31 additions & 7 deletions lib/server/controllers/subsystems.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
* or submit itself to any jurisdiction.
*/

const {
subsystem: {
GetAllSubsystemsUseCase,
},
} = require('../../usecases');
const {
dtos: {
GetAllSubsystemsDto,
},
} = require('../../domain');
const { dtoValidator } = require('../utilities');

/**
* Get all subsystems.
*
Expand All @@ -22,14 +34,26 @@
* next middleware function.
* @returns {undefined}
*/
const index = (request, response, next) => {
response.status(501).json({
errors: [
{
status: '501',
title: 'Not implemented',
const index = async (request, response, next) => {
const value = await dtoValidator(GetAllSubsystemsDto, request, response);
if (!value) {
return;
}

const { count, subsystems } = await new GetAllSubsystemsUseCase()
.execute(value);

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

response.status(200).json({
data: subsystems,
meta: {
page: {
pageCount: totalPages,
totalCount: count,
},
],
},
});
};

Expand Down
6 changes: 4 additions & 2 deletions lib/server/routers/subsystems.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
const { SubsystemsController } = require('../controllers');

module.exports = {
method: 'get',
path: 'subsystems',
controller: SubsystemsController.index,
args: { public: true },
children: [
{
method: 'get',
controller: SubsystemsController.index,
},
{
method: 'get',
path: '/:id',
Expand Down
2 changes: 2 additions & 0 deletions lib/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

const log = require('./log');
const server = require('./server');
const subsystem = require('./subsystem');
const tag = require('./tag');

module.exports = {
log,
server,
subsystem,
tag,
};
47 changes: 47 additions & 0 deletions lib/usecases/subsystem/GetAllSubsystemsUseCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @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: {
SubsystemRepository,
},
utilities: {
QueryBuilder,
TransactionHelper,
},
} = require('../../database');

/**
* GetAllSubsystemsUseCase
*/
class GetAllSubsystemsUseCase {
/**
* Executes this use case.
*
* @param {Object} dto The GetAllSubsystems DTO which contains all request data.
* @returns {Promise} Promise object represents the result of this use case.
*/
async execute(dto = {}) {
const queryBuilder = new QueryBuilder();
const { query = {} } = dto;
const { page = {} } = query;

const { limit = 100, offset = 0 } = page;
queryBuilder.limit(limit);
queryBuilder.offset(offset);

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

module.exports = GetAllSubsystemsUseCase;
17 changes: 17 additions & 0 deletions lib/usecases/subsystem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @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 GetAllSubsystemsUseCase = require('./GetAllSubsystemsUseCase');

module.exports = {
GetAllSubsystemsUseCase,
};
Loading

0 comments on commit 986e7a3

Please sign in to comment.