@@ -30,7 +30,7 @@ import {defineCrudRestController} from '../..';
30
30
describe ( 'CrudRestController for a simple Product model' , ( ) => {
31
31
@model ( )
32
32
class Product extends Entity {
33
- @property ( { id : true } )
33
+ @property ( { id : true , generated : 1 } )
34
34
id : number ;
35
35
36
36
@property ( { required : true } )
@@ -44,8 +44,47 @@ describe('CrudRestController for a simple Product model', () => {
44
44
}
45
45
}
46
46
47
+ @model ( )
48
+ class User extends Entity {
49
+ @property ( { id : true , generated : 0 , defaultFn : 'uuid' } )
50
+ id : string ;
51
+
52
+ @property ( { required : true } )
53
+ username : string ;
54
+
55
+ @property ( )
56
+ email ?: string ;
57
+
58
+ constructor ( data : Partial < User > ) {
59
+ super ( data ) ;
60
+ }
61
+ }
62
+
63
+ @model ( )
64
+ class Customer extends Entity {
65
+ @property ( { id : true , generated : 0 } )
66
+ id : number ;
67
+
68
+ @property ( { required : true } )
69
+ name : string ;
70
+
71
+ @property ( )
72
+ email ?: string ;
73
+
74
+ constructor ( data : Partial < Customer > ) {
75
+ super ( data ) ;
76
+ }
77
+ }
78
+
47
79
let app : RestApplication ;
48
80
let repo : EntityCrudRepository < Product , typeof Product . prototype . id > ;
81
+ let customerRepo : EntityCrudRepository <
82
+ Customer ,
83
+ typeof Customer . prototype . id
84
+ > ;
85
+
86
+ let userRepo : EntityCrudRepository < User , typeof User . prototype . id > ;
87
+
49
88
let client : Client ;
50
89
51
90
// sample data - call `seedData` to populate these items
@@ -76,7 +115,7 @@ describe('CrudRestController for a simple Product model', () => {
76
115
expect ( toJSON ( found ) ) . to . deepEqual ( created ) ;
77
116
} ) ;
78
117
79
- it ( 'rejects request with `id` value' , async ( ) => {
118
+ it ( 'rejects request with `id` value when generated is set to 1 ' , async ( ) => {
80
119
const { body} = await client
81
120
. post ( '/products' )
82
121
. send ( { id : 1 , name : 'a name' } )
@@ -94,6 +133,29 @@ describe('CrudRestController for a simple Product model', () => {
94
133
] ,
95
134
} ) ;
96
135
} ) ;
136
+
137
+ it ( 'creates records when `id` has generated set to 0 with defaultFn' , async ( ) => {
138
+ const user = { username : 'johndoe' } ;
139
+ const { body : userCreated } = await client
140
+ . post ( '/users' )
141
+ . send ( user )
142
+ . expect ( 200 ) ;
143
+ expect ( userCreated ) . to . have . property ( 'id' ) . of . type ( 'string' ) ;
144
+ const userFound = ( await userRepo . find ( ) ) [ 0 ] ;
145
+ expect ( toJSON ( userFound ) ) . to . deepEqual ( userCreated ) ;
146
+ } ) ;
147
+
148
+ it ( 'accepts request with `id` value when generated is set to 0' , async ( ) => {
149
+ const customer = { id : 1 , name : 'a name' } ;
150
+ const { body : customerCreated } = await client
151
+ . post ( '/customers' )
152
+ . send ( customer )
153
+ . expect ( 200 ) ;
154
+ expect ( customerCreated ) . to . containEql ( customer ) ;
155
+ expect ( customerCreated ) . to . have . property ( 'id' ) . of . type ( 'number' ) ;
156
+ const customerFound = ( await customerRepo . find ( ) ) [ 0 ] ;
157
+ expect ( toJSON ( customerFound ) ) . to . deepEqual ( customerCreated ) ;
158
+ } ) ;
97
159
} ) ;
98
160
99
161
describe ( 'find' , ( ) => {
@@ -289,23 +351,53 @@ describe('CrudRestController for a simple Product model', () => {
289
351
const db = new juggler . DataSource ( { connector : 'memory' } ) ;
290
352
291
353
const ProductRepository = defineCrudRepositoryClass ( Product ) ;
354
+ const CustomerRepository = defineCrudRepositoryClass ( Customer ) ;
355
+ const UserRepository = defineCrudRepositoryClass ( User ) ;
292
356
293
357
repo = new ProductRepository ( db ) ;
358
+ customerRepo = new CustomerRepository ( db ) ;
359
+ userRepo = new UserRepository ( db ) ;
294
360
295
361
const CrudRestController = defineCrudRestController <
296
362
Product ,
297
363
typeof Product . prototype . id ,
298
364
'id'
299
365
> ( Product , { basePath : '/products' } ) ;
300
366
367
+ const CrudRestControllerForCustomer = defineCrudRestController <
368
+ Customer ,
369
+ typeof Customer . prototype . id ,
370
+ 'id'
371
+ > ( Customer , { basePath : '/customers' } ) ;
372
+
373
+ const CrudRestControllerForUser = defineCrudRestController <
374
+ User ,
375
+ typeof User . prototype . id ,
376
+ 'id'
377
+ > ( User , { basePath : '/users' } ) ;
378
+
301
379
class ProductController extends CrudRestController {
302
380
constructor ( ) {
303
381
super ( repo ) ;
304
382
}
305
383
}
306
384
385
+ class CustomerController extends CrudRestControllerForCustomer {
386
+ constructor ( ) {
387
+ super ( customerRepo ) ;
388
+ }
389
+ }
390
+
391
+ class UserController extends CrudRestControllerForUser {
392
+ constructor ( ) {
393
+ super ( userRepo ) ;
394
+ }
395
+ }
396
+
307
397
app = new RestApplication ( { rest : givenHttpServerConfig ( ) } ) ;
308
398
app . controller ( ProductController ) ;
399
+ app . controller ( CustomerController ) ;
400
+ app . controller ( UserController ) ;
309
401
310
402
await app . start ( ) ;
311
403
client = createRestAppClient ( app ) ;
0 commit comments