Skip to content

Commit

Permalink
wrapping up more on #9 add user and get one
Browse files Browse the repository at this point in the history
  • Loading branch information
snurby7 committed Feb 10, 2019
1 parent 3cd927a commit 94b1ac0
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/CoffeeRatesServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
*
* created by Sean Maxwell Jan 21, 2019
*/

import * as path from 'path';
import * as express from 'express';
import * as bodyParser from 'body-parser';
import { Server } from '@overnightjs/core';
import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as path from 'path';

import UserController from './controllers/user/UserController';

class CoffeeRatesServer extends Server {

private readonly _SERVER_START_MSG = 'Demo server started on port: ';
private readonly _DEV_MSG = 'Express Server is running in development mode. Start the React ' +
'development server "npm run start:react" to develop front-end content. Back-end is ' +
'currently running on port: ';
private readonly _SERVER_START_MSG = 'Coffee Rates server started on port: ';
private readonly _DEV_MSG = 'Express Server currently running on port: ';

private _port = 3001;


constructor() {
super();

Expand All @@ -40,7 +37,10 @@ class CoffeeRatesServer extends Server {


private _setupControllers(): void {
// super.addControllers(controllers);
const controllers = [
new UserController(),
];
super.addControllers(controllers);
}


Expand Down
1 change: 1 addition & 0 deletions src/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './user.interface';
4 changes: 4 additions & 0 deletions src/contracts/user.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IUser {
name: string;
id: string;
}
Empty file removed src/controllers/UserController.ts
Empty file.
23 changes: 23 additions & 0 deletions src/controllers/shared/TestServer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as bodyParser from 'body-parser';
import { Application } from 'express';
import { Server } from '@overnightjs/core';


class TestServer extends Server {

constructor() {
super();
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({extended: true}));
}

public setController(ctlr: object): void {
super.addControllers(ctlr);
}

public getExpressInstance(): Application {
return this.app;
}
}

export default TestServer;
55 changes: 55 additions & 0 deletions src/controllers/user/UserController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as supertest from 'supertest';

import {} from 'jasmine';
import { SuperTest, Test } from 'supertest';

import TestServer from '../shared/TestServer.test';
import UserController from './UserController';
import { IUser } from '../../contracts';

describe('UserController', () => {
const userController = new UserController();
let agent: SuperTest<Test>;

beforeAll(done => {
// Activate the routes
const server = new TestServer();
server.setController(userController);

// Start supertest
agent = supertest.agent(server.getExpressInstance());
done();
});

describe('API: "/api/user/:id"', () => {
it(`should return a JSON object with user Data`, (done: DoneFn) => {
agent.get('/api/user/123').end((err, res) => {
expect(res.status).toBe(250);
const user: IUser = res.body.response;
expect(user.id).toBe('sampleId');
expect(user.name).toBe('sample');
done();
});
});
});

describe('API: "/api/user/"', () => {
it(`should post a new user the endpoint`, (done: DoneFn) => {
const testUser: IUser = {
id: 'test',
name: 'waffles',
};
agent.post('/api/user')
.type('form')
.expect(250)
.send(testUser)
.end((err, res) => {
if (err) {
console.error(err);
}
expect(res.body.success).toBeTruthy();
done();
});
});
});
});
37 changes: 37 additions & 0 deletions src/controllers/user/UserController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Controller, Get, Post } from '@overnightjs/core';
import { Request, Response } from 'express';

import { IUser } from '../../contracts';


@Controller('api/user')
class UserController {

public readonly SUC_MSG = 'hello';
public readonly ERR_MSG = 'can\'t say hello';


@Get(':id')
private getUser(req: Request, res: Response): void {
try {
const userId = req.params.id;
const response: IUser = {
name: 'sample',
id: 'sampleId',
};
res.status(250).json({response});
} catch (err) {
console.error(err);
res.status(400).json({response: this.ERR_MSG});
}
}

@Post('')
private addUser(req: Request, res: Response): void {
const newUser = req.body;
// TODO save the user somewhere
res.status(250).json({success: true});
}
}

export default UserController;

0 comments on commit 94b1ac0

Please sign in to comment.