Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge d9b6524 into 0ba1ae9
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobueno authored Aug 15, 2017
2 parents 0ba1ae9 + d9b6524 commit 0176caa
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cloud/wfm-rest-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Example `/workorders/B1r71fOBr`
### Update object

> PUT {object}/
> PUT {object}/:objectId
### Delete object

Expand Down
17 changes: 13 additions & 4 deletions cloud/wfm-rest-api/src/impl/ApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,16 @@ export class ApiController<T> {
return Bluebird.reject(error);
}

return this.repository.update(req.body);
const data = req.body;
const id = req.params.id;
if (!id) {
const error = new ApiError(errorCodes.CLIENT_ERROR, 'Missing entity id for update', 204);
return Bluebird.reject(error);
}

data.id = id;

return this.repository.update(data);
}

public buildExpressHandler(handlerFn: (this: this, req: Request) => Bluebird<T | T[] | undefined>): RequestHandler {
Expand All @@ -117,11 +126,11 @@ export class ApiController<T> {

router.route('/')
.get(this.buildExpressHandler(this.listHandler))
.post(this.buildExpressHandler(this.postHandler))
.put(this.buildExpressHandler(this.putHandler));
.post(this.buildExpressHandler(this.postHandler));
router.route('/:id')
.get(this.buildExpressHandler(this.getHandler))
.delete(this.buildExpressHandler(this.deleteHandler));
.delete(this.buildExpressHandler(this.deleteHandler))
.put(this.buildExpressHandler(this.putHandler));

return router;
}
Expand Down
8 changes: 7 additions & 1 deletion cloud/wfm-rest-api/src/impl/MongoDbRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Bluebird from 'bluebird';
import { Collection, Cursor, CursorCommentOptions, Db } from 'mongodb';
import { ObjectID } from 'mongodb';
import { generate } from 'shortid';
import { ApiError } from '../data-api/ApiError';
import { defaultPaginationEngine } from '../data-api/MongoPaginationEngine';
Expand All @@ -13,7 +14,10 @@ const dbError: ApiError = new ApiError(errorCodes.DB_ERROR, 'MongoDbRepository d
/**
* Service for performing data operations on mongodb database
*/
export class MongoDbRepository<T extends { id: string }> implements PagingDataRepository<T> {
export class MongoDbRepository<T extends {
id: string,
_id?: string | ObjectID
}> implements PagingDataRepository<T> {

public db: Db;
protected collection: Collection<T>;
Expand Down Expand Up @@ -53,6 +57,8 @@ export class MongoDbRepository<T extends { id: string }> implements PagingDataRe
if (!this.db) {
return Bluebird.reject(dbError);
}
// Find update target by id, do not ask mongo to update the ObjectID
delete object._id;
const id = object.id;
return Bluebird.resolve(this.collection.updateOne({ id }, object))
.then(() => this.get(object.id));
Expand Down
3 changes: 2 additions & 1 deletion cloud/wfm-rest-api/test/ApiControllerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ describe('FeedHenry ApiController Tests', function() {
});

it('verify put middleware success', function() {
return expect(testSubject.putHandler({ body: testObj } as Request)).to.eventually.equal(testObj);
const request = { params: { id: 1 }, body: testObj };
return expect(testSubject.putHandler(request as Request)).to.eventually.equal(testObj);
});

it('verify delete middleware success', function() {
Expand Down
6 changes: 3 additions & 3 deletions demo/server/config-dev.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"morganOptions": null,
"morganOptions": "dev",
"logStackTraces": true,
"seedDemoData": true,
"security": {
"apiRole": "admin",
"syncRole": "user"
},
"sync": {
"customDataHandlers": true,
"seedDemoData": true
"customDataHandlers": true
},
"bunyanConfig": {
"name": "Demo application",
Expand Down
1 change: 0 additions & 1 deletion demo/server/config-prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"morganOptions": null,
"logStackTraces": false,
"sync": {
"customDataHandlers": true,
"seedDemoData": true
},
"security": {
Expand Down
9 changes: 9 additions & 0 deletions demo/server/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"watch": [
"src/**/*.ts"
],
"ignore": [
"test/**/*.ts"
],
"exec": "ts-node ./src/index.ts"
}
10 changes: 6 additions & 4 deletions demo/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"clean": "del coverage_report src/*.js src/**/*.js src/*.map src/**/*.map test/**/*.js test/**/*.map",
"build": "tsc",
"start": "ts-node src/index.ts",
"watch": "nodemon",
"test": "nyc mocha"
},
"nyc": {
Expand All @@ -33,25 +34,26 @@
"branches": 70
},
"devDependencies": {
"@types/mongodb": "^2.2.7",
"@raincatcher/logger": "1.0.0",
"@types/bluebird": "^3.5.5",
"@types/lodash": "^4.14.66",
"@types/body-parser": "^1.16.3",
"@types/cookie-parser": "^1.3.30",
"@types/cors": "^2.8.1",
"@types/express": "^4.0.35",
"@types/express-handlebars": "0.0.29",
"@types/lodash": "^4.14.66",
"@types/mocha": "^2.2.41",
"@types/mongodb": "^2.2.7",
"@types/morgan": "^1.7.32",
"@types/serve-favicon": "^2.2.28",
"@types/cors": "^2.8.1",
"@types/node": "^8.0.7",
"@types/serve-favicon": "^2.2.28",
"del-cli": "^1.0.0",
"grunt": "^1.0.1",
"grunt-contrib-watch": "^1.0.0",
"grunt-env": "^0.4.4",
"grunt-exec": "^2.0.0",
"mocha": "^3.4.2",
"nodemon": "^1.11.0",
"nyc": "^11.0.1",
"source-map-support": "^0.4.15",
"ts-node": "^3.0.4",
Expand Down
6 changes: 4 additions & 2 deletions demo/server/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

import { getLogger } from '@raincatcher/logger';
import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';
import * as cors from 'cors';
import * as express from 'express';
import * as expressHbs from 'express-handlebars';
import * as logger from 'morgan';
import * as morgan from 'morgan';
import * as path from 'path';
import * as favicon from 'serve-favicon';
import { securityMiddleware, setupModules } from './modules';
Expand Down Expand Up @@ -37,7 +38,7 @@ function getCorsConfig() {
}

if (config.morganOptions) {
app.use(logger(config.morganOptions));
app.use(morgan(config.morganOptions));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
Expand All @@ -62,6 +63,7 @@ let errHandler: express.ErrorRequestHandler;

errHandler = (err: any, req: express.Request, res: express.Response, next: () => void) => {
res.status(err.status || 500);
getLogger().error(err);
res.json({
title: 'error',
message: err.message,
Expand Down

0 comments on commit 0176caa

Please sign in to comment.