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 software service #312

Closed
wants to merge 11 commits into from
226 changes: 112 additions & 114 deletions app/controllers/software-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const softwareService = require('../services/software-service');
const logger = require('../lib/logger');
const { DuplicateIdError, BadlyFormattedParameterError, InvalidQueryStringParameterError, MissingPropertyError, PropertyNotAllowedError } = 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,103 +17,101 @@ exports.retrieveAll = function(req, res) {
lastUpdatedBy: req.query.lastUpdatedBy,
includePagination: req.query.includePagination
}

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

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

softwareService.retrieveById(req.params.stixId, options, function (err, software) {
if (err) {
if (err.message === softwareService.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 === softwareService.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 software. Server error.');
}
try {
const software = await softwareService.retrieveById(req.params.stixId, options);
if (software.length === 0) {
return res.status(404).send('Software not found.');
}
else {
logger.debug(`Success: Retrieved ${ software.length } software with id ${ req.params.stixId }`);
return res.status(200).send(software);
}

} 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 (software.length === 0) {
return res.status(404).send('Software not found.');
}
else {
logger.debug(`Success: Retrieved ${ software.length } software with id ${ req.params.stixId }`);
return res.status(200).send(software);
}
}
});
logger.error('Failed with error: ' + err);
return res.status(500).send('Unable to get software. Server error.');
}
}

};

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

if (!software) {
return res.status(404).send('Software not found.');
}
else {
logger.debug(`Success: Retrieved software with id ${software.id}`);
return res.status(200).send(software);
}
} 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 software. Server error.');
}
}

};

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

const options = {
import: false,
userAccountId: req.user?.userAccountId
};

// Create the software
try {
const options = {
import: false,
userAccountId: req.user?.userAccountId
};
const software = await softwareService.create(softwareData, options);
const software = await await softwareService.create(softwareData, options);
logger.debug("Success: Created software with id " + software.stix.id);
return res.status(201).send(software);
}
catch(err) {
if (err.message === softwareService.errors.duplicateId) {
} catch(err) {
if (err instanceof DuplicateIdError) {
logger.warn("Duplicate stix.id and stix.modified");
return res.status(409).send('Unable to create software. Duplicate stix.id and stix.modified properties.');
}
else if (err.message === softwareService.errors.missingProperty) {
else if (err instanceof MissingPropertyError) {
logger.warn(`Unable to create software, missing property ${ err.propertyName }`);
return res.status(400).send(`Unable to create software, missing property ${ err.propertyName }`);
}
else if (err.message === softwareService.errors.propertyNotAllowed) {
else if (err instanceof PropertyNotAllowedError) {
logger.warn(`Unable to create software, property ${ err.propertyName } is not allowed`);
return res.status(400).send(`Unable to create software, property ${ err.propertyName } is not allowed`);
}
Expand All @@ -123,58 +122,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 softwareData = req.body;

// Create the software
softwareService.updateFull(req.params.stixId, req.params.modified, softwareData, function(err, software) {
if (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update software. Server error.");
try {
// Create the software
const software = await softwareService.updateFull(req.params.stixId, req.params.modified, softwareData);

if (!software) {
return res.status(404).send('Software not found.');
} else {
logger.debug("Success: Updated software with id " + software.stix.id);
return res.status(200).send(software);
}
else {
if (!software) {
return res.status(404).send('Software not found.');
} else {
logger.debug("Success: Updated software with id " + software.stix.id);
return res.status(200).send(software);
}
}
});
} catch (err) {
logger.error("Failed with error: " + err);
return res.status(500).send("Unable to update software. Server error.");
}
};

exports.deleteVersionById = function(req, res) {
softwareService.deleteVersionById(req.params.stixId, req.params.modified, function (err, software) {
if (err) {
logger.error('Delete software failed. ' + err);
return res.status(500).send('Unable to delete software. Server error.');
exports.deleteVersionById = async function(req, res) {
try {
const software = await softwareService.deleteVersionById(req.params.stixId, req.params.modified);

if (!software) {
return res.status(404).send('Software not found.');
} else {
logger.debug("Success: Deleted software with id " + software.stix.id);
return res.status(204).end();
}
else {
if (!software) {
return res.status(404).send('Software not found.');
} else {
logger.debug("Success: Deleted software with id " + software.stix.id);
return res.status(204).end();
}
}
});

} catch (err) {
logger.error('Delete software failed. ' + err);
return res.status(500).send('Unable to delete software. Server error.');
}
};

exports.deleteById = function(req, res) {
softwareService.deleteById(req.params.stixId, function (err, softwares) {
if (err) {
logger.error('Delete software failed. ' + err);
return res.status(500).send('Unable to delete software. Server error.');
exports.deleteById = async function(req, res) {
try {
const softwares = await softwareService.deleteById(req.params.stixId);

if (softwares.deletedCount === 0) {
return res.status(404).send('Software not found.');
}
else {
if (softwares.deletedCount === 0) {
return res.status(404).send('Software not found.');
}
else {
logger.debug(`Success: Deleted software with id ${ req.params.stixId }`);
return res.status(204).end();
}
}
});
logger.debug(`Success: Deleted software with id ${ req.params.stixId }`);
return res.status(204).end();
}

} catch (err) {
logger.error('Delete software failed. ' + err);
return res.status(500).send('Unable to delete software. Server error.');
}
};
14 changes: 14 additions & 0 deletions app/exceptions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ class NotImplementedError extends CustomError {
}
}

class MissingPropertyError extends CustomError {
constructor(propertyName, options) {
super(`Unable to create software, missing property ${propertyName}`, options);
}
}

class PropertyNotAllowedError extends CustomError {
constructor(propertyName, options) {
super(`Unable to create software, property ${propertyName} is not allowed`, options);
}
}

module.exports = {

//** General errors */
Expand All @@ -101,4 +113,6 @@ module.exports = {
IdentityServiceError,
TechniquesServiceError,
TacticsServiceError,
MissingPropertyError,
PropertyNotAllowedError,
};
13 changes: 13 additions & 0 deletions app/repository/software-repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const BaseRepository = require('./_base.repository');
const Software = require('../models/software-model');

class SoftwareRepository extends BaseRepository {

constructor() {
super(Software);
}
}

module.exports = new SoftwareRepository();
Loading
Loading