@@ -88,17 +88,6 @@ export class CrudRoutesFactory {
8888 } ;
8989 }
9090
91- protected resolveService ( controller : any ) : any {
92- const prop = this . options . serviceProperty ?? 'service' ;
93- const svc = controller [ prop ] ;
94- if ( ! svc ) {
95- throw new Error (
96- `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
97- ) ;
98- }
99- return svc ;
100- }
101-
10291 protected create ( ) {
10392 const routesSchema = this . getRoutesSchema ( ) ;
10493 this . mergeOptions ( ) ;
@@ -256,58 +245,106 @@ export class CrudRoutesFactory {
256245 }
257246
258247 protected getManyBase ( name : BaseRouteName ) {
259- const resolve = this . resolveService . bind ( this ) ;
248+ const prop = this . options . serviceProperty ?? 'service' ;
260249 this . targetProto [ name ] = function getManyBase ( req : CrudRequest ) {
261- return resolve ( this ) . getMany ( req ) ;
250+ const svc = this [ prop ] ;
251+ if ( ! svc ) {
252+ throw new Error (
253+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
254+ ) ;
255+ }
256+ return svc . getMany ( req ) ;
262257 } ;
263258 }
264259
265260 protected getOneBase ( name : BaseRouteName ) {
266- const resolve = this . resolveService . bind ( this ) ;
261+ const prop = this . options . serviceProperty ?? 'service' ;
267262 this . targetProto [ name ] = function getOneBase ( req : CrudRequest ) {
268- return resolve ( this ) . getOne ( req ) ;
263+ const svc = this [ prop ] ;
264+ if ( ! svc ) {
265+ throw new Error (
266+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
267+ ) ;
268+ }
269+ return svc . getOne ( req ) ;
269270 } ;
270271 }
271272
272273 protected createOneBase ( name : BaseRouteName ) {
273- const resolve = this . resolveService . bind ( this ) ;
274+ const prop = this . options . serviceProperty ?? 'service' ;
274275 this . targetProto [ name ] = function createOneBase ( req : CrudRequest , dto : any ) {
275- return resolve ( this ) . createOne ( req , dto ) ;
276+ const svc = this [ prop ] ;
277+ if ( ! svc ) {
278+ throw new Error (
279+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
280+ ) ;
281+ }
282+ return svc . createOne ( req , dto ) ;
276283 } ;
277284 }
278285
279286 protected createManyBase ( name : BaseRouteName ) {
280- const resolve = this . resolveService . bind ( this ) ;
287+ const prop = this . options . serviceProperty ?? 'service' ;
281288 this . targetProto [ name ] = function createManyBase ( req : CrudRequest , dto : any ) {
282- return resolve ( this ) . createMany ( req , dto ) ;
289+ const svc = this [ prop ] ;
290+ if ( ! svc ) {
291+ throw new Error (
292+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
293+ ) ;
294+ }
295+ return svc . createMany ( req , dto ) ;
283296 } ;
284297 }
285298
286299 protected updateOneBase ( name : BaseRouteName ) {
287- const resolve = this . resolveService . bind ( this ) ;
300+ const prop = this . options . serviceProperty ?? 'service' ;
288301 this . targetProto [ name ] = function updateOneBase ( req : CrudRequest , dto : any ) {
289- return resolve ( this ) . updateOne ( req , dto ) ;
302+ const svc = this [ prop ] ;
303+ if ( ! svc ) {
304+ throw new Error (
305+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
306+ ) ;
307+ }
308+ return svc . updateOne ( req , dto ) ;
290309 } ;
291310 }
292311
293312 protected replaceOneBase ( name : BaseRouteName ) {
294- const resolve = this . resolveService . bind ( this ) ;
313+ const prop = this . options . serviceProperty ?? 'service' ;
295314 this . targetProto [ name ] = function replaceOneBase ( req : CrudRequest , dto : any ) {
296- return resolve ( this ) . replaceOne ( req , dto ) ;
315+ const svc = this [ prop ] ;
316+ if ( ! svc ) {
317+ throw new Error (
318+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
319+ ) ;
320+ }
321+ return svc . replaceOne ( req , dto ) ;
297322 } ;
298323 }
299324
300325 protected deleteOneBase ( name : BaseRouteName ) {
301- const resolve = this . resolveService . bind ( this ) ;
326+ const prop = this . options . serviceProperty ?? 'service' ;
302327 this . targetProto [ name ] = function deleteOneBase ( req : CrudRequest ) {
303- return resolve ( this ) . deleteOne ( req ) ;
328+ const svc = this [ prop ] ;
329+ if ( ! svc ) {
330+ throw new Error (
331+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
332+ ) ;
333+ }
334+ return svc . deleteOne ( req ) ;
304335 } ;
305336 }
306337
307338 protected recoverOneBase ( name : BaseRouteName ) {
308- const resolve = this . resolveService . bind ( this ) ;
339+ const prop = this . options . serviceProperty ?? 'service' ;
309340 this . targetProto [ name ] = function recoverOneBase ( req : CrudRequest ) {
310- return resolve ( this ) . recoverOne ( req ) ;
341+ const svc = this [ prop ] ;
342+ if ( ! svc ) {
343+ throw new Error (
344+ `@Crud: controller property "${ prop } " is undefined — did you forget to inject the CrudService field?` ,
345+ ) ;
346+ }
347+ return svc . recoverOne ( req ) ;
311348 } ;
312349 }
313350
0 commit comments