11import { 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' ;
55import { 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 ' )
1718export 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}
0 commit comments