Skip to content

Commit 441cfa1

Browse files
author
hirsch88
committed
create new config area and update documentations
1 parent 51a34dd commit 441cfa1

38 files changed

+351
-171
lines changed

src/api/controllers/UserController.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { injectable, inject } from 'inversify';
1+
import { injectable, inject, named } from 'inversify';
22
import { Controller, Get, Post, Put, Delete, RequestParam, RequestBody, Response, Request } from 'inversify-express-utils';
33
import { my } from 'my-express';
4-
import { Log } from '../../core/log';
54
import { UserService } from '../services/UsersService';
65
import { Types } from '../../constants/Types';
6+
import { Service } from '../../constants/Targets';
77
import { authenticate, populateUser } from '../middlewares';
88

9-
const log = new Log('api:ctrl.UserController');
10-
119
/**
1210
* UserController is in charge of the user resource and should
1311
* provide all crud actions.
@@ -16,45 +14,39 @@ const log = new Log('api:ctrl.UserController');
1614
@Controller('/user', authenticate)
1715
export class UserController {
1816

19-
constructor( @inject(Types.UserService) private userService: UserService) { }
17+
constructor( @inject(Types.Service) @named(Service.UserService) private userService: UserService) { }
2018

2119
@Get('/')
2220
public async findAll( @Response() res: my.Response): Promise<any> {
23-
log.debug('findAll');
2421
const users = await this.userService.findAll();
2522
return res.found(users.toJSON());
2623
}
2724

2825
@Post('/')
2926
public async create( @Response() res: my.Response, @RequestBody() body: any): Promise<any> {
30-
log.debug('create ', body);
3127
const user = await this.userService.create(body);
3228
return res.created(user.toJSON());
3329
}
3430

3531
@Get('/me', populateUser)
3632
public async findMe( @Request() req: my.Request, @Response() res: my.Response): Promise<any> {
37-
log.debug('findMe');
3833
return res.found(req.user);
3934
}
4035

4136
@Get('/:id')
4237
public async findOne( @Response() res: my.Response, @RequestParam('id') id: string): Promise<any> {
43-
log.debug('findOne ', id);
4438
const user = await this.userService.findOne(parseInt(id, 10));
4539
return res.found(user.toJSON());
4640
}
4741

4842
@Put('/:id')
4943
public async update( @Response() res: my.Response, @RequestParam('id') id: string, @RequestBody() body: any): Promise<any> {
50-
log.debug('update ', body);
5144
const user = await this.userService.update(parseInt(id, 10), body);
5245
return res.updated(user.toJSON());
5346
}
5447

5548
@Delete('/:id')
5649
public async destroy( @Response() res: my.Response, @RequestParam('id') id: string): Promise<any> {
57-
log.debug('destroy ', id);
5850
await this.userService.destroy(parseInt(id, 10));
5951
return res.destroyed();
6052
}

src/api/middlewares/authenticate.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { RequestAPI, RequiredUriUrl, Options, Request, RequestResponse } from 'r
22
import { my } from 'my-express';
33
import { Log } from '../../core/log';
44

5-
// const log = new Log('api:middleware.authenticate');
6-
75
/**
86
* authenticate middleware
97
* -----------------------

src/api/middlewares/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
/**
2+
* Middlewares
3+
* ------------------------------------
4+
* We build them up here so we can easily use them in the controllers and
5+
* also be able to test the middlewares without any big effort.
6+
*/
7+
18
import * as request from 'request';
29
import container from '../../container';
310
import { Log } from '../../core/log';
411
import { Types } from '../../constants/Types';
12+
import { Service } from '../../constants/Targets';
513
import { UserService } from '../services/UsersService';
614

715
import { authenticate as Authenticate } from './authenticate';
816
import { populateUser as PopulateUser } from './populateUser';
917

18+
const logPrefix = 'api:middleware';
1019

11-
/**
12-
* Middlewares
13-
* ------------------------------------
14-
* We build them up here so we can easily use them in the controllers and
15-
* also be able to test the middlewares without any big effort.
16-
*/
17-
export const authenticate = Authenticate(request, new Log('api:middleware.authenticate'));
18-
export const populateUser = PopulateUser(() => container.get<UserService>(Types.UserService), new Log('api:middleware.populateUser'));
1920

21+
export const authenticate = Authenticate(request, new Log(`${logPrefix}:authenticate`));
22+
export const populateUser = PopulateUser(() => container.getNamed<UserService>(Types.Service, Service.UserService), new Log(`${logPrefix}:populateUser`));

src/api/middlewares/populateUser.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { my } from 'my-express';
44
import { Log } from '../../core/log';
55
import { UserService } from '../services/UsersService';
66

7-
// const log = new Log('api:middleware.populateUser');
8-
97
/**
108
* populateUser middleware
119
* -----------------------
@@ -18,11 +16,11 @@ import { UserService } from '../services/UsersService';
1816
*/
1917
export const populateUser = (lazyUserService: () => UserService, log: Log) =>
2018
(req: my.Request, res: my.Response, next: my.NextFunction) => {
21-
19+
// Check if the authenticate middleware was successful
2220
if (!req.tokeninfo || !req.tokeninfo.user_id) {
2321
return res.failed(400, 'Missing token information!');
2422
}
25-
23+
// Find user from the token and store him in the request object
2624
const userService = lazyUserService();
2725
userService.findByUserId(req.tokeninfo.user_id)
2826
.then((user) => {

src/api/models/User.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Bookshelf } from '../../core/Database';
1+
import { Bookshelf } from '../../config/Database';
22
import { Tables } from '../../constants/Tables';
33

44
/**
@@ -51,6 +51,9 @@ export class User extends Bookshelf.Model<User> {
5151
public get CreatedAt(): Date { return this.get('createdAt'); }
5252
public set CreatedAt(value: Date) { this.set('createdAt', value); }
5353

54+
/**
55+
* Helper methods
56+
*/
5457
public fullName(): string {
5558
return this.FirstName + ' ' + this.LastName;
5659
}

src/api/requests/UserCreateRequest.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export class UserCreateRequest extends RequestBody {
2323
email: string;
2424

2525
picture: string;
26-
2726
auth0UserId: string;
2827

2928
}

src/api/requests/UserUpdateRequest.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export class UserUpdateRequest extends RequestBody {
2727
@IsNotEmpty()
2828
auth0UserId: string;
2929

30+
/**
31+
* We override the validate method so we can skip the missing
32+
* properties.
33+
*/
3034
public async validate(): Promise<void> {
3135
return super.validate(true);
3236
}

src/api/services/UsersService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const log = new Log('api:services:UserService');
2626
export class UserService {
2727

2828
constructor(
29-
@inject(Types.UserRepository) @named(Repository.UserRepository) public userRepo: typeof UserRepository
29+
@inject(Types.Repository) @named(Repository.UserRepository) public userRepo: typeof UserRepository
3030
) { }
3131

3232
/**

src/app.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* APPLICATION CONFIGURATION
3+
* ----------------------------------------
4+
*
5+
* This is the place to add any other express module and register
6+
* all your custom middlewares and routes.
7+
*/
8+
19
import * as path from 'path';
210
import * as cors from 'cors';
311
import * as morgan from 'morgan';
@@ -11,14 +19,9 @@ import { Bootstrap } from './core/Bootstrap';
1119
import { Log } from './core/log';
1220
import container from './container';
1321

14-
/**
15-
* APPLICATION CONFIGURATION
16-
* ----------------------------------------
17-
* This is the place to add any other express module and register
18-
* all your custom middlewares and routes
19-
*/
22+
2023
const app = Bootstrap.getApp()
21-
// Report realtime server metrics for Express-based node servers
24+
// Report real time server metrics for Express-based node servers
2225
.use(monitor())
2326

2427
// Enabling the cors headers
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
/**
2+
* config.Database
3+
* ------------------------------------
4+
*
5+
* Here we configure our database connection and
6+
* our ORM 'bookshelf'.
7+
*
8+
* Here would be the place to add more bookshelf plugins.
9+
*/
10+
111
import * as knex from 'knex';
212
import * as bookshelf from 'bookshelf';
3-
import { Environment } from './Environment';
13+
import { Environment } from '../core/Environment';
414

515

616
export const Knex = (): knex => knex({

0 commit comments

Comments
 (0)