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 sources service for improved structure and asynchronicity #337

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 @@
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.');

Check warning on line 31 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L30-L31

Added lines #L30 - L31 were not covered by tests
}
});

};

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.');

Check warning on line 53 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L51-L53

Added lines #L51 - L53 were not covered by tests
}
});
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.');
}
}

Check warning on line 63 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L55-L63

Added lines #L55 - L63 were not covered by tests
};

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.');
}

Check warning on line 75 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L74-L75

Added lines #L74 - L75 were not covered by tests
else {
logger.debug(`Success: Retrieved data source with id ${dataSource.id}`);
return res.status(200).send(dataSource);
}
} catch (err) {
if (err instanceof BadlyFormattedParameterError) {

Check warning on line 81 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L81

Added line #L81 was not covered by tests
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 @@
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.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.');

Check warning on line 126 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L126

Added line #L126 was not covered by tests
} 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.');

Check warning on line 143 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L143

Added line #L143 was not covered by tests
} 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.');

Check warning on line 167 in app/controllers/data-sources-controller.js

View check run for this annotation

Codecov / codecov/patch

app/controllers/data-sources-controller.js#L166-L167

Added lines #L166 - L167 were not covered by tests
}
});
};
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
Loading