Skip to content

Commit

Permalink
Merge pull request #337 from center-for-threat-informed-defense/301-r…
Browse files Browse the repository at this point in the history
…efactor-data-sources-service-for-improved-structure-and-asynchronicity

Refactored data sources service for improved structure and asynchronicity
  • Loading branch information
ElJocko authored Jan 12, 2024
2 parents 7c7a3c3 + 1125eab commit 00f34ce
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 739 deletions.
172 changes: 82 additions & 90 deletions app/controllers/data-sources-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const dataSourcesService = require('../services/data-sources-service');
const logger = require('../lib/logger');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError } = 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 @@ -16,82 +17,76 @@ exports.retrieveAll = function(req, res) {
lastUpdatedBy: req.query.lastUpdatedBy,
includePagination: req.query.includePagination
}

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

};

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

dataSourcesService.retrieveById(req.params.stixId, options, function (err, dataSources) {
if (err) {
if (err.message === dataSourcesService.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 === dataSourcesService.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 sources. Server error.');
}
try {
const dataSources = await dataSourcesService.retrieveById(req.params.stixId, options);
if (dataSources.length === 0) {
return res.status(404).send('Data source not found.');
}
else {
if (dataSources.length === 0) {
return res.status(404).send('Data source not found.');
}
else {
logger.debug(`Success: Retrieved ${ dataSources.length } data source(s) with id ${ req.params.stixId }`);
return res.status(200).send(dataSources);
}
logger.debug(`Success: Retrieved ${ dataSources.length } data source(s) with id ${ req.params.stixId }`);
return res.status(200).send(dataSources);
}
} 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 {
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get data sources. Server error.');
}
}
};

exports.retrieveVersionById = function(req, res) {
exports.retrieveVersionById = async function(req, res) {
const options = {
retrieveDataComponents: req.query.retrieveDataComponents
}

dataSourcesService.retrieveVersionById(req.params.stixId, req.params.modified, options, function (err, dataSource) {
if (err) {
if (err.message === dataSourcesService.errors.badlyFormattedParameter) {
try {
const dataSource = await dataSourcesService.retrieveVersionById(req.params.stixId, req.params.modified, options);
if (!dataSource) {
return res.status(404).send('Data source not found.');
}
else {
logger.debug(`Success: Retrieved data source with id ${dataSource.id}`);
return res.status(200).send(dataSource);
}
} 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 source. Server error.');
}
} else {
if (!dataSource) {
return res.status(404).send('Data source not found.');
}
else {
logger.debug(`Success: Retrieved data source with id ${dataSource.id}`);
return res.status(200).send(dataSource);
}
}
});
}
};

exports.create = async function(req, res) {
Expand All @@ -109,7 +104,7 @@ exports.create = async function(req, res) {
return res.status(201).send(dataSource);
}
catch(err) {
if (err.message === dataSourcesService.errors.duplicateId) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create data source. Duplicate stix.id and stix.modified properties.');
}
Expand All @@ -120,58 +115,55 @@ exports.create = async function(req, res) {
}
};

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

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

};

exports.deleteVersionById = function(req, res) {
dataSourcesService.deleteVersionById(req.params.stixId, req.params.modified, function (err, dataSource) {
if (err) {
exports.deleteVersionById = async function(req, res) {

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

};

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

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

class DataSourcesRepository extends BaseRepository { }

module.exports = new DataSourcesRepository(DataSource);
Loading

0 comments on commit 00f34ce

Please sign in to comment.