1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using System . Threading . Tasks ;
4
5
using JsonApiDotNetCore . Data ;
5
6
using JsonApiDotNetCore . Internal ;
7
+ using JsonApiDotNetCore . Models ;
6
8
using JsonApiDotNetCore . Models . Operations ;
7
9
using JsonApiDotNetCore . Models . Pointers ;
8
10
using Microsoft . EntityFrameworkCore ;
@@ -61,7 +63,7 @@ private async Task ProcessOperation(Operation op, List<Operation> outputOps)
61
63
{
62
64
var operationsPointer = new OperationsPointer ( ) ;
63
65
64
- // ReplaceDataPointers(op.DataObject, outputOps);
66
+ ReplaceDataPointers ( op . DataObject , outputOps ) ;
65
67
// ReplaceRefPointers(op.Ref, outputOps);
66
68
67
69
var processor = GetOperationsProcessor ( op ) ;
@@ -71,6 +73,43 @@ private async Task ProcessOperation(Operation op, List<Operation> outputOps)
71
73
outputOps . Add ( resultOp ) ;
72
74
}
73
75
76
+ private void ReplaceDataPointers ( DocumentData data , List < Operation > outputOps )
77
+ {
78
+ if ( data == null ) return ;
79
+
80
+ bool HasLocalId ( ResourceIdentifierObject rio ) => string . IsNullOrEmpty ( rio . LocalId ) == false ;
81
+ string GetIdFromLocalId ( string localId ) {
82
+ var referencedOp = outputOps . FirstOrDefault ( o => o . DataObject . LocalId == localId ) ;
83
+ if ( referencedOp == null ) throw new JsonApiException ( 400 , $ "Could not locate lid '{ localId } ' in document.") ;
84
+ return referencedOp . DataObject . Id ;
85
+ } ;
86
+
87
+ // are there any circumstances where the primary data would contain an lid?
88
+ // if(HasLocalId(data))
89
+ // {
90
+ // data.Id = GetIdFromLocalId(data.LocalId);
91
+ // }
92
+
93
+ if ( data . Relationships != null )
94
+ {
95
+ foreach ( var relationshipDictionary in data . Relationships )
96
+ {
97
+ if ( relationshipDictionary . Value . IsHasMany )
98
+ {
99
+ foreach ( var relationship in relationshipDictionary . Value . ManyData )
100
+ if ( HasLocalId ( relationship ) )
101
+ relationship . Id = GetIdFromLocalId ( relationship . LocalId ) ;
102
+ }
103
+ else
104
+ {
105
+ var relationship = relationshipDictionary . Value . SingleData ;
106
+ if ( HasLocalId ( relationship ) )
107
+ relationship . Id = GetIdFromLocalId ( relationship . LocalId ) ;
108
+ }
109
+ }
110
+ }
111
+ }
112
+
74
113
private IOpProcessor GetOperationsProcessor ( Operation op )
75
114
{
76
115
switch ( op . Op )
0 commit comments