Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 8 additions & 40 deletions lib/server/controllers/status.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
* or submit itself to any jurisdiction.
*/

const {
status: {
GetGuiStatusUseCase,
GetDatabaseStatusUseCase,
} } = require('../../usecases');
const { statusService } = require('../services/status/StatusService.js');

/**
* Show gui information.
Expand All @@ -26,24 +22,10 @@ const {
*
* @returns {undefined} current gui information
*/
const getGuiInformation = async (request, response) => {
const gui = await new GetGuiStatusUseCase()
.execute(request);

if (gui === null) {
response.status(501).json({
errors: [
{
status: 501,
title: 'Something went wrong',
},
],
});
} else {
response.status(200).json({
data: gui,
});
}
const getGuiInformation = (request, response) => {
response.status(200).json({
data: statusService.getGuiStatus({ host: request.hostname, port: request.socket.localPort }),
});
};

/**
Expand All @@ -56,23 +38,9 @@ const getGuiInformation = async (request, response) => {
* @returns {undefined} current database information
*/
const getDatabaseInformation = async (request, response) => {
const information = await new GetDatabaseStatusUseCase()
.execute();

if (information === null) {
response.status(501).json({
errors: [
{
status: 501,
title: 'Something went wrong',
},
],
});
} else {
response.status(200).json({
data: information,
});
}
response.status(200).json({
data: await statusService.getDatabaseStatus(),
});
};

module.exports = {
Expand Down
46 changes: 17 additions & 29 deletions lib/server/controllers/tags.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,12 @@
* or submit itself to any jurisdiction.
*/

const {
tag: {
CreateTagUseCase,
DeleteTagUseCase,
GetAllTagsUseCase,
GetLogsByTagUseCase,
GetTagUseCase,
GetTagByNameUseCase,
UpdateTagUseCase,
},
} = require('../../usecases');
const { dtos: { CreateTagDto, GetAllTagsDto, GetTagDto, UpdateTagDto, GetTagByNameDto } } = require('../../domain');
const GetAllLogsByTagDto = require('../../domain/dtos/GetAllLogsByTagDto.js');
const { dtoValidator } = require('../utilities');
const { updateExpressResponseFromNativeError } = require('../express/updateExpressResponseFromNativeError.js');
const { tagService } = require('../services/tag/TagService.js');
const { ApiConfig } = require('../../config/index.js');
const GetAllLogsByTagDto = require('../../domain/dtos/GetAllLogsByTagDto.js');

/**
* Create a new tag.
Expand All @@ -44,7 +34,7 @@ const createTag = async (request, response) => {
}

try {
const tag = await new CreateTagUseCase().execute(value);
const tag = await tagService.create(value.body, { userId: value.session?.id });
response.status(201).json({ data: tag });
} catch (error) {
updateExpressResponseFromNativeError(response, error);
Expand All @@ -68,8 +58,7 @@ const deleteTag = async (request, response, next) => {
return;
}

const tag = await new DeleteTagUseCase()
.execute(value);
const tag = await tagService.remove(value.params.tagId);

if (tag === null) {
response.status(404).json({
Expand Down Expand Up @@ -102,12 +91,12 @@ const getLogsByTagId = async (request, response) => {
return;
}

const { logs, count } = await new GetLogsByTagUseCase().execute(value);
const { query: { page = {} } } = value;
const { limit = ApiConfig.pagination.limit } = page;

const { query: { page: { limit = ApiConfig.pagination.limit } = {} } } = value;
const totalPages = Math.ceil(count / limit);
const result = await tagService.getLogs(value.params.tagId, page);

if (logs === null) {
if (result === null) {
response.status(404).json({
errors: [
{
Expand All @@ -117,11 +106,12 @@ const getLogsByTagId = async (request, response) => {
],
});
} else {
const { logs, count } = result;
response.status(200).json({
data: logs,
meta: {
page: {
pageCount: totalPages,
pageCount: Math.ceil(count / limit),
totalCount: count,
},
},
Expand All @@ -146,10 +136,9 @@ const listTags = async (request, response, next) => {
return;
}

const { count, tags } = await new GetAllTagsUseCase()
.execute(value);
const tags = await tagService.getAll({ filter: value.query.filter });

response.status(count === 0 ? 204 : 200).json({ data: tags });
response.status(tags.length === 0 ? 204 : 200).json({ data: tags });
};

/**
Expand All @@ -169,8 +158,7 @@ const getTagById = async (request, response, next) => {
return;
}

const tag = await new GetTagUseCase()
.execute(value);
const tag = await tagService.getByIdentifier({ id: value.params.tagId });

if (tag === null) {
response.status(404).json({
Expand Down Expand Up @@ -201,15 +189,15 @@ const getTagByName = async (request, response, next) => {
return;
}

const tag = await new GetTagByNameUseCase()
.execute(value);
const name = decodeURIComponent(value.query.name);
const tag = await tagService.getByIdentifier({ text: name });

if (tag === null) {
response.status(404).json({
errors: [
{
status: '404',
title: `Tag with this name (${decodeURIComponent(value.query.name)}) could not be found`,
title: `Tag with this name (${name}) could not be found`,
},
],
});
Expand All @@ -235,7 +223,7 @@ const updateTagById = async (request, response) => {
}

try {
const tag = await new UpdateTagUseCase().execute(value);
const tag = await tagService.update(value.params.tagId, value.body, { userId: value.session?.id });
response.status(201).json({ data: tag });
} catch (error) {
updateExpressResponseFromNativeError(response, error);
Expand Down
61 changes: 61 additions & 0 deletions lib/server/services/status/StatusService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @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 { version } = require('../../../../package.json');
const database = require('../../../database');
const { DatabaseConfig: { host: databaseHost, port: databasePort } } = require('../../../config/index.js');

/**
* Service to read the status of the application and of its database
*/
class StatusService {
/**
* Return the status of the Bookkeeping application
*
* @param {object} server the address on which the application received the request
* @param {string} server.host the host
* @param {number} server.port the port
* @return {{host: string, port: number, version: string, status: {ok: boolean, configured: boolean}}} the application status
*/
getGuiStatus({ host, port }) {
return {
host,
port,
version,
status: {
ok: true,
configured: true,
},
};
}

/**
* Return the status of the database connection
*
* @return {Promise<{host: string, port: number, status: {ok: boolean, configured: boolean}}>} the database status
*/
async getDatabaseStatus() {
return {
host: databaseHost,
port: databasePort,
status: {
ok: Boolean(await database.ping()),
configured: true,
},
};
}
}

exports.StatusService = StatusService;

exports.statusService = new StatusService();
Loading
Loading