Skip to content

Commit 82cad2a

Browse files
author
hirsch88
committed
Update express handling and extend the express response object
1 parent d1e49fb commit 82cad2a

33 files changed

+422
-163
lines changed

.env.example

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
1-
APP_NAME=qt-api
1+
#
2+
# APPLICATION
3+
#
4+
APP_NAME=express-typescript-boilerplate
25
APP_ENV=local
36
APP_HOST=http://localhost
47
APP_PORT=3000
58

9+
#
10+
# LOGGING
11+
#
612
DEBUG=app*,api*,core*
713
LOG_LEVEL=debug
814
LOG_ADAPTER=debug
915

10-
DB_CLIENT=pg
11-
DB_CONNECTION=postgres://root:root@localhost:5432/my_database
16+
#
17+
# DATABASE
18+
#
19+
# MySql
20+
DB_CLIENT=mysql
21+
DB_CONNECTION=mysql://root@localhost:3306/my_database
22+
23+
# PostGres
24+
#DB_CLIENT=pg
25+
#DB_CONNECTION=postgres://root:root@localhost:5432/my_database
26+
27+
# Knex
1228
DB_POOL_MIN=0
1329
DB_POOL_MAX=7
1430
DB_MIGRATION_DIR=./src/database/migrations
1531
DB_MIGRATION_TABLE=version
1632
DB_SEEDS_DIR=./src/database/seeds
1733

18-
34+
#
35+
# Auth0
36+
#
1937
AUTH0_HOST=https://w3tecch.auth0.com
2038
AUTH0_OAUTH=/oauth
2139
AUTH0_API=/api/v2

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"typescript.tsdk": "./node_modules/typescript/lib",
33
"files.exclude": {
4-
"src/**/*.js": true,
4+
"src/**/*.js": false,
55
"test/**/*.js": true
66
},
77
"vsicons.presets.angular": false,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"db:migrate": "./node_modules/.bin/knex migrate:latest",
2121
"db:migrate:rollback": "./node_modules/.bin/knex migrate:rollback",
2222
"db:seed": "./node_modules/.bin/knex seed:run",
23-
"db:clean": "./node_modules/.bin/ts-node ./src/console/CleanDatabaseCommand.ts",
2423
"db:refresh": "npm run db:drop && npm run db:migrate",
2524
"start": "node src/index.js"
2625
},
@@ -74,6 +73,7 @@
7473
"body-parser": "^1.17.1",
7574
"bookshelf": "^0.10.3",
7675
"bookshelf-camelcase": "^1.1.4",
76+
"class-validator": "^0.7.0",
7777
"compression": "^1.6.2",
7878
"cors": "^2.8.1",
7979
"debug": "^2.6.0",
Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { injectable, inject } from 'inversify';
2-
import { Controller, Get, RequestParam } from 'inversify-express-utils';
3-
import TYPES from '../../constants/types';
4-
import * as core from '../../core';
2+
import { Controller, Get, Post, Put, Delete, RequestParam, RequestBody, Response } from 'inversify-express-utils';
3+
import { my } from 'my-express';
4+
import { Log } from '../../core';
55
import { UserService } from '../services';
6+
import TYPES from '../../constants/types';
67

7-
const log = new core.Log('api:ctrl.UserController');
8+
const log = new Log('api:ctrl.UserController');
89

910
/**
1011
* UserController
@@ -13,23 +14,44 @@ const log = new core.Log('api:ctrl.UserController');
1314
* @class UserController
1415
*/
1516
@injectable()
16-
@Controller('/users')
17+
@Controller('/v1/user')
1718
export class UserController {
1819

1920
constructor( @inject(TYPES.UserService) private userService: UserService) { }
2021

2122
@Get('/')
22-
public async findAll(): Promise<any> {
23+
public async findAll( @Response() res: my.Response): Promise<any> {
2324
log.debug('findAll');
2425
const users = await this.userService.findAll();
25-
return users.toJSON();
26+
return res.found(users.toJSON());
2627
}
2728

2829
@Get('/:id')
29-
public async findOne( @RequestParam('id') id: string): Promise<any> {
30+
public async findOne( @Response() res: my.Response, @RequestParam('id') id: string): Promise<any> {
3031
log.debug('findOne ', id);
3132
const user = await this.userService.findOne(parseInt(id, 10));
32-
return user.toJSON();
33+
return res.found(user.toJSON());
34+
}
35+
36+
@Post('/')
37+
public async create( @Response() res: my.Response, @RequestBody() body: any): Promise<any> {
38+
log.debug('create ', body);
39+
const user = await this.userService.create(body);
40+
return res.created(user.toJSON());
41+
}
42+
43+
@Put('/:id')
44+
public async update( @Response() res: my.Response, @RequestParam('id') id: string, @RequestBody() body: any): Promise<any> {
45+
log.debug('update ', body);
46+
const user = await this.userService.update(parseInt(id, 10), body);
47+
return res.updated(user.toJSON());
48+
}
49+
50+
@Delete('/:id')
51+
public async destroy( @Response() res: my.Response, @RequestParam('id') id: string): Promise<any> {
52+
log.debug('destroy ', id);
53+
await this.userService.destroy(parseInt(id, 10));
54+
return res.destroyed();
3355
}
3456

3557
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Exception } from '../../core/api/Exception';
2+
3+
4+
export class DatabaseException extends Exception {
5+
constructor(text: string, error: any) {
6+
let value: string = error.stack.split('\n')[0];
7+
super(400, text, [
8+
value.substring(7)
9+
]);
10+
}
11+
}

src/api/exceptions/NotFoundException.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as core from '../../core';
1+
import { Exception } from '../../core/api/Exception';
22

33

4-
export class NotFoundException extends core.Exception {
4+
export class NotFoundException extends Exception {
55
constructor(id?: number | string) {
66
super(404, `Entity with identifier ${id} does not exist`);
77
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Exception } from '../../core/api/Exception';
2+
3+
4+
export class ValidationException extends Exception {
5+
constructor(text: string, errors: any) {
6+
const info = errors.map((e) => ({
7+
property: e.property,
8+
constraints: e.constraints
9+
}));
10+
super(400, text, info);
11+
}
12+
}

src/api/exceptions/index.ts

100755100644
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from './DatabaseException';
12
export * from './NotFoundException';
3+
export * from './ValidationException';

src/api/models/User.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ export class User extends core.Bookshelf.Model<User> {
2323
public get Email(): string { return this.get('email'); }
2424
public set Email(value: string) { this.set({ id: value }); };
2525

26-
public get Picture(): string { return this.get('picture'); }
27-
public set Picture(value: string) { this.set({ id: value }); };
28-
2926
public get UpdatedAt(): Date { return this.get('updatedAt'); }
3027
public set UpdatedAt(value: Date) { this.set({ id: value }); };
3128

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
import * as Bookshelf from 'bookshelf';
2-
import * as models from '../models';
3-
// import { injectable } from 'inversify';
2+
import { User } from '../models';
3+
import { DatabaseException, NotFoundException } from '../exceptions';
44

55
/**
66
* UserRepository
77
*
88
* @export
99
* @class UserRepository
1010
*/
11-
// @injectable()
1211
export class UserRepository {
1312

14-
public static async findAll(): Promise<Bookshelf.Collection<models.User>> {
15-
const users = await models.User.fetchAll();
16-
return <Bookshelf.Collection<models.User>>users;
13+
public static async findAll(): Promise<Bookshelf.Collection<User>> {
14+
const users = await User.fetchAll();
15+
return <Bookshelf.Collection<User>>users;
1716
}
1817

19-
public static async findOne(id: number): Promise<models.User> {
20-
return models.User.fetchById(id);
18+
public static async findOne(id: number): Promise<User> {
19+
return User.fetchById(id);
20+
}
21+
22+
public static async create(data: any): Promise<User> {
23+
const user = User.forge<User>(data);
24+
try {
25+
return await user.save();
26+
} catch (error) {
27+
throw new DatabaseException('Could not create the user!', error);
28+
}
29+
}
30+
31+
public static async update(id: number, data: any): Promise<User> {
32+
const user = User.forge<User>({ id: id });
33+
try {
34+
return await user.save(data, { patch: true });
35+
} catch (error) {
36+
throw new DatabaseException('Could not update the user!', error);
37+
}
38+
}
39+
40+
public static async destroy(id: number): Promise<void> {
41+
let user = User.forge<User>({ id: id });
42+
try {
43+
user = await user.fetch({ require: true });
44+
} catch (error) {
45+
throw new NotFoundException(id);
46+
}
47+
48+
try {
49+
await user.destroy();
50+
return;
51+
} catch (error) {
52+
throw new DatabaseException('Could not delete the user!', error);
53+
}
2154
}
2255

2356
}

0 commit comments

Comments
 (0)