Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jupe committed Jan 27, 2019
1 parent 613a5b9 commit fdf8224
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
35 changes: 15 additions & 20 deletions app/controllers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// 3rd party modules
const Promise = require('bluebird');
const _ = require('lodash');
const {CastError} = require('mongoose');

// own modules
// const eventBus = require('../tools/eventBus');
Expand Down Expand Up @@ -100,27 +99,23 @@ class EventsController extends DefaultController {
find.$or.push({_id: req.params.Resource});
}
find.$or.push({'hw.sn': req.params.Resource});
logger.debug(`find resource by ${JSON.stringify(find)} (model: Resource)`);
this.logger.debug(`find resource by ${JSON.stringify(find)} (model: Resource)`);
const {Model} = ResourceModel;
return Model.findOne(find)
.then((data) => {
if (!data) {
const error = new Error(`Document with id ${docId} not found`);
error.statusCode = 404;
throw error;
}
_.set(req, 'Resource', data);
next();
})
.catch(CastError, (error) => {
error.statusCode = 400;
throw error;
})
.catch((error) => {
const status = error.statusCode || 500;
logger.error(error);
res.status(status).json({message: `${error}`});
});
.then((data) => {
if (!data) {
const error = new Error(`Document with id/hw.sn ${req.params.Resource} not found`);
error.statusCode = 404;
throw error;
}
_.set(req, 'Resource', data);
next();
})
.catch((error) => {
const status = error.statusCode || 500;
this.logger.error(error);
res.status(status).json({message: `${error}`});
});
}
resourceEvents(req, res) {
const filter = {'ref.resource': req.Resource._id};
Expand Down
35 changes: 18 additions & 17 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DefaultController extends EventEmitter {
this._model = mongoose.model(modelName);
this.modelName = modelName;
this.docId = '_id';
this.logger = logger;
}

static isObjectId(str) {
Expand All @@ -36,24 +37,24 @@ class DefaultController extends EventEmitter {
const find = this._getModelParamQuery(req);
logger.debug(`find document by ${JSON.stringify(find)} (model: ${this.modelName})`);
return this.Model.findOne(find)
.then((data) => {
if (!data) {
const error = new Error(`Document with id ${docId} not found`);
error.statusCode = 404;
throw error;
}
_.set(req, this.modelName, data);
next();
})
.catch(mongoose.CastError, () => {
const error = new Error('Invalid document id');
error.statusCode = 400;
.then((data) => {
if (!data) {
const error = new Error(`Document with id ${docId} not found`);
error.statusCode = 404;
throw error;
})
.catch((error) => {
const status = error.statusCode || 500;
res.status(status).json({message: `${error}`});
});
}
_.set(req, this.modelName, data);
next();
})
.catch(mongoose.CastError, () => {
const error = new Error('Invalid document id');
error.statusCode = 400;
throw error;
})
.catch((error) => {
const status = error.statusCode || 500;
res.status(status).json({message: `${error}`});
});
}

get Model() {
Expand Down
5 changes: 1 addition & 4 deletions test/unittests/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ describe('controllers/index.js', function () {
after(teardown);

it('defaultModelParam', function (done) {
// Generate defaultModelParam function
const defaultModelParam = defaultController.defaultModelParam('DummyItem');

// Mock request and response
const req = {params: {DummyItem: mockDummies[0]._id}};
const res = new MockResponse((value) => {
Expand All @@ -50,7 +47,7 @@ describe('controllers/index.js', function () {
});

// Call the tested function
defaultModelParam(req, res, function () {
defaultController.modelParam(req, res, function () {
expect(req.DummyItem).to.containSubset(mockDummies[0]);
done();
}, undefined);
Expand Down

0 comments on commit fdf8224

Please sign in to comment.