@@ -32,12 +32,9 @@ export abstract class ResourceController<M extends AbstractModel> extends Abstra
32
32
* @returns {any }
33
33
*/
34
34
@Route ( 'GET' , '/:id' )
35
- public getOne ( request : Request , response : Response ) : Promise < Response > {
36
-
37
- return this . modelStore
38
- . findOne ( request . params ( )
39
- . get ( 'id' ) )
40
- . then ( ( model : M ) => response . data ( model ) ) ;
35
+ public async getOne ( request : Request , response : Response ) : Promise < Response > {
36
+ const model : M = await this . modelStore . findOne ( request . params ( ) . get ( 'id' ) ) ;
37
+ return response . data ( model ) ;
41
38
}
42
39
43
40
/**
@@ -47,11 +44,9 @@ export abstract class ResourceController<M extends AbstractModel> extends Abstra
47
44
* @returns {any }
48
45
*/
49
46
@Route ( 'GET' , '/' )
50
- public getMany ( request : Request , response : Response ) : Promise < Response > {
51
-
52
- return this . modelStore
53
- . findMany ( )
54
- . then ( ( collection : Collection < M > ) => response . data ( collection ) ) ;
47
+ public async getMany ( request : Request , response : Response ) : Promise < Response > {
48
+ const collection : Collection < M > = await this . modelStore . findMany ( ) ;
49
+ return response . data ( collection ) ;
55
50
}
56
51
57
52
/**
@@ -85,12 +80,11 @@ export abstract class ResourceController<M extends AbstractModel> extends Abstra
85
80
* @returns {Promise<Response> }
86
81
*/
87
82
@Route ( 'DELETE' , '/:id' )
88
- public deleteOne ( request : Request , response : Response ) : Promise < Response > {
83
+ public async deleteOne ( request : Request , response : Response ) : Promise < Response > {
89
84
90
- return request . getPayload ( )
91
- . then ( ( data : any ) => this . modelStore . hydrate ( data ) )
92
- . then ( ( model : M ) => this . modelStore . deleteOne ( model ) )
93
- . then ( ( model : M ) => response . data ( model ) ) ;
85
+ const model : M = await this . modelStore . hydrate ( await request . getPayload ( ) ) ;
86
+ await this . modelStore . deleteOne ( model ) ;
87
+ return response . data ( model ) ;
94
88
}
95
89
96
90
/**
@@ -101,28 +95,25 @@ export abstract class ResourceController<M extends AbstractModel> extends Abstra
101
95
* @param checkExists
102
96
* @returns {Promise<Response> }
103
97
*/
104
- protected savePayload ( request : Request , response : Response , checkExists : boolean = false , validatorOptions : ValidatorOptions = { } ) : Promise < Response > {
105
- let modelPayload = request . getPayload ( )
106
- . then ( ( data : any ) => this . modelStore . hydrate ( data ) ) ;
98
+ protected async savePayload (
99
+ request : Request ,
100
+ response : Response ,
101
+ checkExists : boolean = false ,
102
+ validatorOptions : ValidatorOptions = { } ,
103
+ ) : Promise < Response > | never {
107
104
108
- if ( checkExists ) {
109
-
110
- modelPayload = modelPayload . then ( ( payload : M ) => {
111
- return this . modelStore . hasOne ( payload )
112
- . then ( ( exists : boolean ) => {
113
- if ( ! exists ) {
114
- throw new NotFoundException ( `Model with id [${ payload . getIdentifier ( ) } ] does not exist` ) ;
115
- }
116
- return payload ;
117
- } ) ;
118
- } )
105
+ const model = await this . modelStore . hydrate ( await request . getPayload ( ) ) ;
119
106
107
+ if ( checkExists ) {
108
+ const exists = await this . modelStore . hasOne ( model ) ;
109
+ if ( ! exists ) {
110
+ throw new NotFoundException ( `Model with id [${ model . getIdentifier ( ) } ] does not exist` ) ;
111
+ }
120
112
}
121
113
122
- return modelPayload
123
- . then ( ( model : M ) => this . modelStore . validate ( model , validatorOptions ) )
124
- . then ( ( model : M ) => this . modelStore . saveOne ( model ) )
125
- . then ( ( model : M ) => response . data ( model ) ) ;
114
+ await this . modelStore . validate ( model , validatorOptions ) ;
115
+ await this . modelStore . saveOne ( model ) ;
116
+ return response . data ( model ) ;
126
117
}
127
118
128
119
}
0 commit comments