Skip to content

Commit 3976f38

Browse files
author
Ryan Kotzen
committed
Merge branch 'develop'
2 parents ec1b2c0 + 2c84b30 commit 3976f38

14 files changed

Lines changed: 204 additions & 29 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"scripts": {
99
"start": "nodejs-dashboard node index.js",
1010
"watch": "nodemon .",
11-
"test": "node_modules/.bin/istanbul --include-all-sources cover node_modules/mocha/bin/_mocha 'test/**/*.js'",
11+
"test": "node_modules/.bin/istanbul --include-all-sources cover -x \"public/**\" -x \"report/**\" node_modules/mocha/bin/_mocha \"test/**/*.js\"",
1212
"mocha": "node_modules/.bin/_mocha 'test/**/*.js'",
1313
"lint": "eslint \"modules/**/*.js\" \"src/**/*.js\" \"test/**/*.js\" index.js --fix",
1414
"lint-ci": "eslint \"modules/**/*.js\" \"src/**/*.js\" \"test/**/*.js\" index.js",

src/crud/create.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var schemaName = 'creation';
99
var config = require('nconf');
1010
var moment = require('moment');
1111

12-
module.exports = function addRoute(router, crudMiddleware, maps) {
12+
module.exports = function addCreateRoute(router, crudMiddleware, maps) {
1313
ensureSchemaSet(router.metadata, schemaName, 'Input');
1414
router.post('/', getSteps(router, crudMiddleware, maps))
1515
.describe(router.metadata.creationDescription || description(router.metadata));

src/crud/delete-by-id.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
var output = require('../output');
3+
var applyMaps = require('./shared/apply-maps');
4+
var _ = require('lodash');
5+
var addModel = require('../swagger/add-model');
6+
var config = require('nconf');
7+
8+
module.exports = function addDeleteByIdRoute(router, crudMiddleware, maps) {
9+
router.delete('/:' + router.metadata.identifierName, getSteps(router, crudMiddleware, maps))
10+
.describe(router.metadata.getByIdDescription || description(router.metadata));
11+
return router;
12+
};
13+
14+
function getSteps(router, crudMiddleware, maps) {
15+
var steps = {
16+
findByIdentifier: crudMiddleware.findByIdentifier,
17+
deleteByIdentifier: crudMiddleware.deleteByIdentifier,
18+
writeHistoryItem: crudMiddleware.writeHistoryItem,
19+
sendOutput: output.sendNoContent
20+
};
21+
return applyMaps(maps, steps);
22+
}
23+
24+
function description(metadata) {
25+
addModel(metadata.schemas.output);
26+
var correlationIdOptions = config.get('logging').correlationId;
27+
return {
28+
security: true,
29+
summary: "Removes " + metadata.aOrAn + " " + metadata.title + " By " + _.startCase(metadata.identifierName) + ".",
30+
tags: [metadata.tag.name],
31+
parameters: [
32+
{
33+
name: metadata.identifierName.toLowerCase(),
34+
description: "The field to uniquely identify this " + metadata.title.toLowerCase() + ".",
35+
required: true,
36+
in: "path",
37+
type: "string"
38+
}
39+
],
40+
common: {
41+
responses: ["500", "400", "401", "404"],
42+
parameters: {
43+
header: [correlationIdOptions.reqHeader]
44+
}
45+
},
46+
responses: {
47+
"204": {
48+
description: "Shows that the delete request was successfully carried out",
49+
commonHeaders: [correlationIdOptions.resHeader]
50+
}
51+
}
52+
};
53+
}

src/crud/get-by-id-and-use.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var applyMaps = require('./shared/apply-maps');
44
var util = require('util');
55
var _ = require('lodash');
66

7-
module.exports = function (router, path, routerOrMiddleware, crudMiddleware, maps) {
7+
module.exports = function addGetByIdAndUseRoute(router, path, routerOrMiddleware, crudMiddleware, maps) {
88
if (_.isObject(path)) {
99
//path omitted, move all args up by one.
1010
maps = crudMiddleware;

src/crud/get-by-id.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var _ = require('lodash');
55
var addModel = require('../swagger/add-model');
66
var config = require('nconf');
77

8-
module.exports = function addRoute(router, crudMiddleware, maps) {
8+
module.exports = function addGetByIdRoute(router, crudMiddleware, maps) {
99
router.get('/:' + router.metadata.identifierName, getSteps(router, crudMiddleware, maps))
1010
.describe(router.metadata.getByIdDescription || description(router.metadata));
1111
return router;

src/crud/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var addModel = require('../swagger/add-model');
55
var config = require('nconf');
66

77

8-
module.exports = function addRoute(router, crudMiddleware, maps) {
8+
module.exports = function addQueryRoute(router, crudMiddleware, maps) {
99
router.get('/', getSteps(router, crudMiddleware, maps))
1010
.describe(router.metadata.queryDescription || description(router.metadata));
1111
return router;

src/crud/router/add-standard-routes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var query = require('../query');
66
var update = require('../update');
77
var updateStatus = require('../update-status');
88
var getByIdAndUse = require('../get-by-id-and-use');
9+
var deleteById = require('../delete-by-id');
910
module.exports = function addStandardRoutes(router) {
1011
if (!_.isObject(router.metadata)) {
1112
throw new Error("Router.metadata must be set!");
@@ -50,6 +51,14 @@ module.exports = function addStandardRoutes(router) {
5051
}
5152
return updateStatus(router, crudMiddleware, maps);
5253
};
54+
router.deleteById = function (crudMiddleware, maps) {
55+
if (router.crudMiddleware) {
56+
if (_.isNil(maps)) {
57+
return deleteById(router, router.crudMiddleware, crudMiddleware);
58+
}
59+
}
60+
return deleteById(router, crudMiddleware, maps);
61+
};
5362
router.getByIdAndUse = function (path, routerOrMiddleware, crudMiddleware, maps) {
5463
if (router.crudMiddleware) {
5564
if (_.isNil(maps)) {

src/crud/update-status.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var validator = require('../validate/validator');
1010
var boom = require('boom');
1111
var util = require('util');
1212

13-
module.exports = function addRoute(router, crudMiddleware, maps) {
13+
module.exports = function addUpdateStatusRoute(router, crudMiddleware, maps) {
1414
if (!router.metadata.schemas.core.statuses) {
1515
throw new Error("No statuses defined in metadata.schemas.core.statuses");
1616
}
@@ -51,7 +51,7 @@ function description(metadata) {
5151
var correlationIdOptions = config.get('logging').correlationId;
5252
return {
5353
security: true,
54-
summary: "Updates the status of a " + metadata.title + " By " + _.startCase(metadata.identifierName),
54+
summary: "Updates the status of " + metadata.aOrAn + " " + metadata.title + " By " + _.startCase(metadata.identifierName),
5555
tags: [metadata.tag.name],
5656
parameters: [
5757
{

src/crud/update.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var schemaName = 'update';
99
var versionInfo = require('../version-info');
1010
var config = require('nconf');
1111

12-
module.exports = function addRoute(router, crudMiddleware, maps) {
12+
module.exports = function addUpdateRoute(router, crudMiddleware, maps) {
1313
ensureSchemaSet(router.metadata, schemaName, 'Input');
1414
router.put('/:' + router.metadata.identifierName, getSteps(router, crudMiddleware, maps))
1515
.describe(router.metadata.updateDescription || description(router.metadata));
@@ -33,7 +33,7 @@ function description(metadata) {
3333
var correlationIdOptions = config.get('logging').correlationId;
3434
return {
3535
security: true,
36-
summary: "Updates a " + metadata.title + " By " + _.startCase(metadata.identifierName),
36+
summary: "Updates " + metadata.aOrAn + " " + metadata.title + " By " + _.startCase(metadata.identifierName),
3737
tags: [metadata.tag.name],
3838
parameters: [
3939
{

src/mongo/crud.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = function (metadata) {
1515
update: update(metadata),
1616
updateStatus: updateStatus(metadata),
1717
getExistingMetadata: getExistingMetadata(metadata),
18-
writeHistoryItem: writeHistoryItem(metadata)
18+
writeHistoryItem: writeHistoryItem(metadata),
19+
deleteByIdentifier: deleteByIdentifier(metadata)
1920
};
2021
};
2122

@@ -213,4 +214,27 @@ function getExistingMetadata(metadata) {
213214
return next();
214215
}
215216
};
217+
}
218+
219+
function deleteByIdentifier(metadata) {
220+
return function (req, res, next) {
221+
req.process.originalItem = req.process[metadata.name];
222+
var identifier = req.params[metadata.identifierName];
223+
if (_.isNil(identifier)) {
224+
return next(new Error("Object has no identifier"));
225+
}
226+
var mongoQuery = getIdentifierQuery(identifier, metadata);
227+
mongo.db.collection(metadata.collectionName)
228+
.deleteOne(mongoQuery, documentDeleted);
229+
230+
function documentDeleted(err, result) {
231+
if (err) {
232+
return next(err);
233+
}
234+
if (result.deletedCount !== 1) {
235+
console.warn(util.format('Expected 1 item to be deleted, but result was %s. Query : %j. Original Item : %j', result.deletedCount, mongoQuery, req.process.originalItem));
236+
}
237+
return next();
238+
}
239+
};
216240
}

0 commit comments

Comments
 (0)