@@ -196,24 +196,18 @@ public virtual async Task<TResource> GetRelationshipAsync(TId id, string relatio
196
196
_hookExecutor ? . BeforeRead < TResource > ( ResourcePipeline . GetRelationship , id . ToString ( ) ) ;
197
197
198
198
var secondaryLayer = _queryLayerComposer . Compose ( _request . SecondaryResource ) ;
199
-
200
- var secondaryIdAttribute = _request . SecondaryResource . Attributes . Single ( a => a . Property . Name == nameof ( Identifiable . Id ) ) ;
201
-
199
+ secondaryLayer . Projection = _queryLayerComposer . GetSecondaryProjectionForRelationshipEndpoint ( _request . SecondaryResource ) ;
202
200
secondaryLayer . Include = null ;
203
- secondaryLayer . Projection = new Dictionary < ResourceFieldAttribute , QueryLayer >
204
- {
205
- [ secondaryIdAttribute ] = null
206
- } ;
207
-
208
- var primaryLayer = GetPrimaryLayerForSecondaryEndpoint ( secondaryLayer , id ) ;
201
+
202
+ var primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResource , id , _request . Relationship ) ;
209
203
210
204
var primaryResources = await _repository . GetAsync ( primaryLayer ) ;
211
-
205
+
212
206
var primaryResource = primaryResources . SingleOrDefault ( ) ;
213
207
AssertPrimaryResourceExists ( primaryResource ) ;
214
208
215
209
if ( _hookExecutor != null )
216
- {
210
+ {
217
211
_hookExecutor . AfterRead ( AsList ( primaryResource ) , ResourcePipeline . GetRelationship ) ;
218
212
primaryResource = _hookExecutor . OnReturn ( AsList ( primaryResource ) , ResourcePipeline . GetRelationship ) . Single ( ) ;
219
213
}
@@ -233,7 +227,7 @@ public virtual async Task<object> GetSecondaryAsync(TId id, string relationshipN
233
227
_hookExecutor ? . BeforeRead < TResource > ( ResourcePipeline . GetRelationship , id . ToString ( ) ) ;
234
228
235
229
var secondaryLayer = _queryLayerComposer . Compose ( _request . SecondaryResource ) ;
236
- var primaryLayer = GetPrimaryLayerForSecondaryEndpoint ( secondaryLayer , id ) ;
230
+ var primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResource , id , _request . Relationship ) ;
237
231
238
232
var primaryResources = await _repository . GetAsync ( primaryLayer ) ;
239
233
@@ -249,35 +243,6 @@ public virtual async Task<object> GetSecondaryAsync(TId id, string relationshipN
249
243
return _request . Relationship . GetValue ( primaryResource ) ;
250
244
}
251
245
252
- private QueryLayer GetPrimaryLayerForSecondaryEndpoint ( QueryLayer secondaryLayer , TId primaryId )
253
- {
254
- var innerInclude = secondaryLayer . Include ;
255
- secondaryLayer . Include = null ;
256
-
257
- var primaryIdAttribute =
258
- _request . PrimaryResource . Attributes . Single ( a => a . Property . Name == nameof ( Identifiable . Id ) ) ;
259
-
260
- return new QueryLayer ( _request . PrimaryResource )
261
- {
262
- Include = RewriteIncludeForSecondaryEndpoint ( innerInclude ) ,
263
- Filter = CreateFilterById ( primaryId ) ,
264
- Projection = new Dictionary < ResourceFieldAttribute , QueryLayer >
265
- {
266
- [ primaryIdAttribute ] = null ,
267
- [ _request . Relationship ] = secondaryLayer
268
- }
269
- } ;
270
- }
271
-
272
- private IncludeExpression RewriteIncludeForSecondaryEndpoint ( IncludeExpression relativeInclude )
273
- {
274
- var parentElement = relativeInclude != null
275
- ? new IncludeElementExpression ( _request . Relationship , relativeInclude . Elements )
276
- : new IncludeElementExpression ( _request . Relationship ) ;
277
-
278
- return new IncludeExpression ( new [ ] { parentElement } ) ;
279
- }
280
-
281
246
/// <inheritdoc />
282
247
public virtual async Task < TResource > UpdateAsync ( TId id , TResource requestResource )
283
248
{
@@ -320,7 +285,7 @@ public virtual async Task UpdateRelationshipAsync(TId id, string relationshipNam
320
285
AssertRelationshipExists ( relationshipName ) ;
321
286
322
287
var secondaryLayer = _queryLayerComposer . Compose ( _request . SecondaryResource ) ;
323
- var primaryLayer = GetPrimaryLayerForSecondaryEndpoint ( secondaryLayer , id ) ;
288
+ var primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResource , id , _request . Relationship ) ;
324
289
primaryLayer . Projection = null ;
325
290
326
291
var primaryResources = await _repository . GetAsync ( primaryLayer ) ;
0 commit comments