@@ -103,8 +103,7 @@ public virtual async Task<TResource> GetAsync(TId id, CancellationToken cancella
103
103
104
104
_hookExecutor . BeforeReadSingle < TResource , TId > ( id , ResourcePipeline . GetSingle ) ;
105
105
106
- TResource primaryResource = await TryGetPrimaryResourceByIdAsync ( id , TopFieldSelection . PreserveExisting , cancellationToken ) ;
107
- AssertPrimaryResourceExists ( primaryResource ) ;
106
+ TResource primaryResource = await GetPrimaryResourceByIdAsync ( id , TopFieldSelection . PreserveExisting , cancellationToken ) ;
108
107
109
108
_hookExecutor . AfterReadSingle ( primaryResource , ResourcePipeline . GetSingle ) ;
110
109
_hookExecutor . OnReturnSingle ( primaryResource , ResourcePipeline . GetSingle ) ;
@@ -225,10 +224,7 @@ public virtual async Task<TResource> CreateAsync(TResource resource, Cancellatio
225
224
throw ;
226
225
}
227
226
228
- TResource resourceFromDatabase =
229
- await TryGetPrimaryResourceByIdAsync ( resourceForDatabase . Id , TopFieldSelection . WithAllAttributes , cancellationToken ) ;
230
-
231
- AssertPrimaryResourceExists ( resourceFromDatabase ) ;
227
+ TResource resourceFromDatabase = await GetPrimaryResourceByIdAsync ( resourceForDatabase . Id , TopFieldSelection . WithAllAttributes , cancellationToken ) ;
232
228
233
229
await _resourceDefinitionAccessor . OnAfterCreateResourceAsync ( resourceFromDatabase , cancellationToken ) ;
234
230
@@ -247,13 +243,14 @@ public virtual async Task<TResource> CreateAsync(TResource resource, Cancellatio
247
243
return resourceFromDatabase ;
248
244
}
249
245
250
- private async Task AssertResourcesToAssignInRelationshipsExistAsync ( TResource resource , CancellationToken cancellationToken )
246
+ protected async Task AssertResourcesToAssignInRelationshipsExistAsync ( TResource primaryResource , CancellationToken cancellationToken )
251
247
{
252
248
var missingResources = new List < MissingResourceInRelationship > ( ) ;
253
249
254
- foreach ( ( QueryLayer queryLayer , RelationshipAttribute relationship ) in _queryLayerComposer . ComposeForGetTargetedSecondaryResourceIds ( resource ) )
250
+ foreach ( ( QueryLayer queryLayer , RelationshipAttribute relationship ) in _queryLayerComposer . ComposeForGetTargetedSecondaryResourceIds (
251
+ primaryResource ) )
255
252
{
256
- object rightValue = relationship . GetValue ( resource ) ;
253
+ object rightValue = relationship . GetValue ( primaryResource ) ;
257
254
ICollection < IIdentifiable > rightResourceIds = _collectionConverter . ExtractResources ( rightValue ) ;
258
255
259
256
IAsyncEnumerable < MissingResourceInRelationship > missingResourcesInRelationship =
@@ -287,7 +284,7 @@ private async IAsyncEnumerable<MissingResourceInRelationship> GetMissingRightRes
287
284
}
288
285
289
286
/// <inheritdoc />
290
- public async Task AddToToManyRelationshipAsync ( TId primaryId , string relationshipName , ISet < IIdentifiable > secondaryResourceIds ,
287
+ public virtual async Task AddToToManyRelationshipAsync ( TId primaryId , string relationshipName , ISet < IIdentifiable > secondaryResourceIds ,
291
288
CancellationToken cancellationToken )
292
289
{
293
290
_traceWriter . LogMethodStart ( new
@@ -316,10 +313,8 @@ public async Task AddToToManyRelationshipAsync(TId primaryId, string relationshi
316
313
}
317
314
catch ( DataStoreUpdateException )
318
315
{
319
- TResource primaryResource = await TryGetPrimaryResourceByIdAsync ( primaryId , TopFieldSelection . OnlyIdAttribute , cancellationToken ) ;
320
- AssertPrimaryResourceExists ( primaryResource ) ;
321
-
322
- await AssertResourcesExistAsync ( secondaryResourceIds , cancellationToken ) ;
316
+ _ = await GetPrimaryResourceByIdAsync ( primaryId , TopFieldSelection . OnlyIdAttribute , cancellationToken ) ;
317
+ await AssertRightResourcesExistAsync ( secondaryResourceIds , cancellationToken ) ;
323
318
throw ;
324
319
}
325
320
}
@@ -340,16 +335,22 @@ private async Task RemoveExistingIdsFromSecondarySetAsync(TId primaryId, ISet<II
340
335
secondaryResourceIds . ExceptWith ( existingRightResourceIds ) ;
341
336
}
342
337
343
- private async Task AssertResourcesExistAsync ( ICollection < IIdentifiable > secondaryResourceIds , CancellationToken cancellationToken )
338
+ protected async Task AssertRightResourcesExistAsync ( object rightResourceIds , CancellationToken cancellationToken )
344
339
{
345
- QueryLayer queryLayer = _queryLayerComposer . ComposeForGetRelationshipRightIds ( _request . Relationship , secondaryResourceIds ) ;
340
+ ICollection < IIdentifiable > secondaryResourceIds = _collectionConverter . ExtractResources ( rightResourceIds ) ;
346
341
347
- List < MissingResourceInRelationship > missingResources =
348
- await GetMissingRightResourcesAsync ( queryLayer , _request . Relationship , secondaryResourceIds , cancellationToken ) . ToListAsync ( cancellationToken ) ;
349
-
350
- if ( missingResources . Any ( ) )
342
+ if ( secondaryResourceIds . Any ( ) )
351
343
{
352
- throw new ResourcesInRelationshipsNotFoundException ( missingResources ) ;
344
+ QueryLayer queryLayer = _queryLayerComposer . ComposeForGetRelationshipRightIds ( _request . Relationship , secondaryResourceIds ) ;
345
+
346
+ List < MissingResourceInRelationship > missingResources =
347
+ await GetMissingRightResourcesAsync ( queryLayer , _request . Relationship , secondaryResourceIds , cancellationToken )
348
+ . ToListAsync ( cancellationToken ) ;
349
+
350
+ if ( missingResources . Any ( ) )
351
+ {
352
+ throw new ResourcesInRelationshipsNotFoundException ( missingResources ) ;
353
+ }
353
354
}
354
355
}
355
356
@@ -385,8 +386,7 @@ public virtual async Task<TResource> UpdateAsync(TId id, TResource resource, Can
385
386
throw ;
386
387
}
387
388
388
- TResource afterResourceFromDatabase = await TryGetPrimaryResourceByIdAsync ( id , TopFieldSelection . WithAllAttributes , cancellationToken ) ;
389
- AssertPrimaryResourceExists ( afterResourceFromDatabase ) ;
389
+ TResource afterResourceFromDatabase = await GetPrimaryResourceByIdAsync ( id , TopFieldSelection . WithAllAttributes , cancellationToken ) ;
390
390
391
391
await _resourceDefinitionAccessor . OnAfterUpdateResourceAsync ( afterResourceFromDatabase , cancellationToken ) ;
392
392
@@ -429,7 +429,7 @@ public virtual async Task SetRelationshipAsync(TId primaryId, string relationshi
429
429
}
430
430
catch ( DataStoreUpdateException )
431
431
{
432
- await AssertResourcesExistAsync ( _collectionConverter . ExtractResources ( secondaryResourceIds ) , cancellationToken ) ;
432
+ await AssertRightResourcesExistAsync ( secondaryResourceIds , cancellationToken ) ;
433
433
throw ;
434
434
}
435
435
@@ -454,8 +454,7 @@ public virtual async Task DeleteAsync(TId id, CancellationToken cancellationToke
454
454
}
455
455
catch ( DataStoreUpdateException )
456
456
{
457
- TResource primaryResource = await TryGetPrimaryResourceByIdAsync ( id , TopFieldSelection . OnlyIdAttribute , cancellationToken ) ;
458
- AssertPrimaryResourceExists ( primaryResource ) ;
457
+ _ = await GetPrimaryResourceByIdAsync ( id , TopFieldSelection . OnlyIdAttribute , cancellationToken ) ;
459
458
throw ;
460
459
}
461
460
@@ -465,7 +464,7 @@ public virtual async Task DeleteAsync(TId id, CancellationToken cancellationToke
465
464
}
466
465
467
466
/// <inheritdoc />
468
- public async Task RemoveFromToManyRelationshipAsync ( TId primaryId , string relationshipName , ISet < IIdentifiable > secondaryResourceIds ,
467
+ public virtual async Task RemoveFromToManyRelationshipAsync ( TId primaryId , string relationshipName , ISet < IIdentifiable > secondaryResourceIds ,
469
468
CancellationToken cancellationToken )
470
469
{
471
470
_traceWriter . LogMethodStart ( new
@@ -481,14 +480,22 @@ public async Task RemoveFromToManyRelationshipAsync(TId primaryId, string relati
481
480
AssertHasRelationship ( _request . Relationship , relationshipName ) ;
482
481
483
482
TResource resourceFromDatabase = await GetPrimaryResourceForUpdateAsync ( primaryId , cancellationToken ) ;
484
- await AssertResourcesExistAsync ( secondaryResourceIds , cancellationToken ) ;
483
+ await AssertRightResourcesExistAsync ( secondaryResourceIds , cancellationToken ) ;
485
484
486
485
if ( secondaryResourceIds . Any ( ) )
487
486
{
488
487
await _repositoryAccessor . RemoveFromToManyRelationshipAsync ( resourceFromDatabase , secondaryResourceIds , cancellationToken ) ;
489
488
}
490
489
}
491
490
491
+ protected async Task < TResource > GetPrimaryResourceByIdAsync ( TId id , TopFieldSelection fieldSelection , CancellationToken cancellationToken )
492
+ {
493
+ TResource primaryResource = await TryGetPrimaryResourceByIdAsync ( id , fieldSelection , cancellationToken ) ;
494
+ AssertPrimaryResourceExists ( primaryResource ) ;
495
+
496
+ return primaryResource ;
497
+ }
498
+
492
499
private async Task < TResource > TryGetPrimaryResourceByIdAsync ( TId id , TopFieldSelection fieldSelection , CancellationToken cancellationToken )
493
500
{
494
501
QueryLayer primaryLayer = _queryLayerComposer . ComposeForGetById ( id , _request . PrimaryResource , fieldSelection ) ;
@@ -497,7 +504,7 @@ private async Task<TResource> TryGetPrimaryResourceByIdAsync(TId id, TopFieldSel
497
504
return primaryResources . SingleOrDefault ( ) ;
498
505
}
499
506
500
- private async Task < TResource > GetPrimaryResourceForUpdateAsync ( TId id , CancellationToken cancellationToken )
507
+ protected async Task < TResource > GetPrimaryResourceForUpdateAsync ( TId id , CancellationToken cancellationToken )
501
508
{
502
509
QueryLayer queryLayer = _queryLayerComposer . ComposeForUpdate ( id , _request . PrimaryResource ) ;
503
510
var resource = await _repositoryAccessor . GetForUpdateAsync < TResource > ( queryLayer , cancellationToken ) ;
0 commit comments