Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored data components service #314

187 changes: 92 additions & 95 deletions app/controllers/data-components-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const dataComponentsService = require('../services/data-components-service');
const logger = require('../lib/logger');
const { BadlyFormattedParameterError, InvalidQueryStringParameterError, DuplicateIdError } = require('../exceptions');

exports.retrieveAll = function(req, res) {
exports.retrieveAll = async function(req, res) {
const options = {
offset: req.query.offset || 0,
limit: req.query.limit || 0,
Expand All @@ -15,94 +16,91 @@ exports.retrieveAll = function(req, res) {
includePagination: req.query.includePagination
}

dataComponentsService.retrieveAll(options, function(err, results) {
if (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get data components. Server error.');
try {
const results = await dataComponentsService.retrieveAll(options);
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total data component(s)`);
}
else {
if (options.includePagination) {
logger.debug(`Success: Retrieved ${ results.data.length } of ${ results.pagination.total } total data component(s)`);
}
else {
logger.debug(`Success: Retrieved ${ results.length } data component(s)`);
}
return res.status(200).send(results);
logger.debug(`Success: Retrieved ${ results.length } data component(s)`);
}
});
return res.status(200).send(results);
} catch (err) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get data components. Server error.');
}

};

exports.retrieveById = function(req, res) {
exports.retrieveById = async function(req, res) {
const options = {
versions: req.query.versions || 'latest'
}

dataComponentsService.retrieveById(req.params.stixId, options, function (err, dataComponents) {
if (err) {
if (err.message === dataComponentsService.errors.badlyFormattedParameter) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else if (err.message === dataComponentsService.errors.invalidQueryStringParameter) {
logger.warn('Invalid query string: versions=' + req.query.versions);
return res.status(400).send('Query string parameter versions is invalid.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get data component. Server error.');
}
try {
const dataComponents = await dataComponentsService.retrieveById(req.params.stixId, options);
if (dataComponents.length === 0) {
return res.status(404).send('Data component not found.');
}
else {
logger.debug(`Success: Retrieved ${ dataComponents.length } data component(s) with id ${ req.params.stixId }`);
return res.status(200).send(dataComponents);
}
} catch (err) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else if (err instanceof InvalidQueryStringParameterError) {
logger.warn('Invalid query string: versions=' + req.query.versions);
return res.status(400).send('Query string parameter versions is invalid.');
}
else {
if (dataComponents.length === 0) {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get data component. Server error.');
}
}

};

exports.retrieveVersionById = async function(req, res) {
try {
const dataComponent = await dataComponentsService.retrieveVersionById(req.params.stixId, req.params.modified);
if (!dataComponent) {
return res.status(404).send('Data component not found.');
}
else {
logger.debug(`Success: Retrieved ${ dataComponents.length } data component(s) with id ${ req.params.stixId }`);
return res.status(200).send(dataComponents);
logger.debug(`Success: Retrieved data component with id ${dataComponent.id}`);
return res.status(200).send(dataComponent);
}
}
});
};

exports.retrieveVersionById = function(req, res) {
dataComponentsService.retrieveVersionById(req.params.stixId, req.params.modified, function (err, dataComponent) {
if (err) {
if (err.message === dataComponentsService.errors.badlyFormattedParameter) {
} catch (err) {
if (err instanceof BadlyFormattedParameterError) {
logger.warn('Badly formatted stix id: ' + req.params.stixId);
return res.status(400).send('Stix id is badly formatted.');
}
else {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get data component. Server error.');
}
} else {
if (!dataComponent) {
return res.status(404).send('Data component not found.');
}
else {
logger.debug(`Success: Retrieved data component with id ${dataComponent.id}`);
return res.status(200).send(dataComponent);
}
}
});
}
};

exports.create = async function(req, res) {
// Get the data from the request
const dataComponentData = req.body;
const options = {
import: false,
userAccountId: req.user?.userAccountId
};

// Create the data component
try {
const options = {
import: false,
userAccountId: req.user?.userAccountId
};
const dataComponent = await dataComponentsService.create(dataComponentData, options);
logger.debug("Success: Created data component with id " + dataComponent.stix.id);
return res.status(201).send(dataComponent);
}
catch(err) {
if (err.message === dataComponentsService.errors.duplicateId) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create data component. Duplicate stix.id and stix.modified properties.');
}
Expand All @@ -113,58 +111,57 @@ exports.create = async function(req, res) {
}
};

exports.updateFull = function(req, res) {
exports.updateFull = async function(req, res) {
// Get the data from the request
const dataComponentData = req.body;

// Create the data component
dataComponentsService.updateFull(req.params.stixId, req.params.modified, dataComponentData, function(err, dataComponent) {
if (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update data component. Server error.");
}
else {
if (!dataComponent) {
return res.status(404).send('Data component not found.');
} else {
logger.debug("Success: Updated data component with id " + dataComponent.stix.id);
return res.status(200).send(dataComponent);
}

try {
const dataComponent = await dataComponentsService.updateFull(req.params.stixId, req.params.modified, dataComponentData);
if (!dataComponent) {
return res.status(404).send('Data component not found.');
} else {
logger.debug("Success: Updated data component with id " + dataComponent.stix.id);
return res.status(200).send(dataComponent);
}
});
} catch (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update data component. Server error.");
}

};

exports.deleteVersionById = function(req, res) {
dataComponentsService.deleteVersionById(req.params.stixId, req.params.modified, function (err, dataComponent) {
if (err) {
logger.error('Delete data component failed. ' + err);
return res.status(500).send('Unable to delete data component. Server error.');
}
else {
if (!dataComponent) {
return res.status(404).send('Data component not found.');
} else {
logger.debug("Success: Deleted data component with id " + dataComponent.stix.id);
return res.status(204).end();
}
exports.deleteVersionById = async function(req, res) {

try {
const dataComponent = await dataComponentsService.deleteVersionById(req.params.stixId, req.params.modified);
if (!dataComponent) {
return res.status(404).send('Data component not found.');
} else {
logger.debug("Success: Deleted data component with id " + dataComponent.stix.id);
return res.status(204).end();
}
});
} catch (err) {
logger.error('Delete data component failed. ' + err);
return res.status(500).send('Unable to delete data component. Server error.');
}

};

exports.deleteById = function(req, res) {
dataComponentsService.deleteById(req.params.stixId, function (err, dataComponents) {
if (err) {
logger.error('Delete data component failed. ' + err);
return res.status(500).send('Unable to delete data component. Server error.');
exports.deleteById = async function(req, res) {

try {
const dataComponents = await dataComponentsService.deleteById(req.params.stixId);
if (dataComponents.deletedCount === 0) {
return res.status(404).send('Data Component not found.');
}
else {
if (dataComponents.deletedCount === 0) {
return res.status(404).send('Data Component not found.');
}
else {
logger.debug(`Success: Deleted data component with id ${ req.params.stixId }`);
return res.status(204).end();
}
logger.debug(`Success: Deleted data component with id ${ req.params.stixId }`);
return res.status(204).end();
}
});
} catch (err) {
logger.error('Delete data component failed. ' + err);
return res.status(500).send('Unable to delete data component. Server error.');
}
};
10 changes: 10 additions & 0 deletions app/repository/data-components-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const BaseRepository = require('./_base.repository');
const DataComponent = require('../models/data-component-model');

class DataComponentsRepository extends BaseRepository {

}

module.exports = new DataComponentsRepository(DataComponent);
Loading