@@ -27,15 +27,17 @@ public Document Build(IIdentifiable entity)
27
27
Data = _getData ( contextEntity , entity )
28
28
} ;
29
29
30
+ document . Included = _appendIncludedObject ( document . Included , contextEntity , entity ) ;
31
+
30
32
return document ;
31
33
}
32
34
33
35
public Documents Build ( IEnumerable < IIdentifiable > entities )
34
36
{
35
37
var entityType = entities
36
- . GetType ( )
37
- . GenericTypeArguments [ 0 ] ;
38
-
38
+ . GetType ( )
39
+ . GenericTypeArguments [ 0 ] ;
40
+
39
41
var contextEntity = _contextGraph . GetContextEntity ( entityType ) ;
40
42
41
43
var documents = new Documents
@@ -44,9 +46,25 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
44
46
} ;
45
47
46
48
foreach ( var entity in entities )
49
+ {
47
50
documents . Data . Add ( _getData ( contextEntity , entity ) ) ;
51
+ documents . Included = _appendIncludedObject ( documents . Included , contextEntity , entity ) ;
52
+ }
48
53
49
- return documents ;
54
+ return documents ;
55
+ }
56
+
57
+ private List < DocumentData > _appendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
58
+ {
59
+ var includedEntities = _getIncludedEntities ( contextEntity , entity ) ;
60
+ if ( includedEntities . Count > 0 )
61
+ {
62
+ if ( includedObject == null )
63
+ includedObject = new List < DocumentData > ( ) ;
64
+ includedObject . AddRange ( includedEntities ) ;
65
+ }
66
+
67
+ return includedObject ;
50
68
}
51
69
52
70
private DocumentData _getData ( ContextEntity contextEntity , IIdentifiable entity )
@@ -61,20 +79,21 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
61
79
return data ;
62
80
63
81
data . Attributes = new Dictionary < string , object > ( ) ;
64
- data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
65
82
66
83
contextEntity . Attributes . ForEach ( attr =>
67
84
{
68
85
data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
69
86
} ) ;
70
87
71
- _addRelationships ( data , contextEntity , entity ) ;
88
+ if ( contextEntity . Relationships . Count > 0 )
89
+ _addRelationships ( data , contextEntity , entity ) ;
72
90
73
91
return data ;
74
92
}
75
93
76
94
private void _addRelationships ( DocumentData data , ContextEntity contextEntity , IIdentifiable entity )
77
95
{
96
+ data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
78
97
var linkBuilder = new LinkBuilder ( _jsonApiContext ) ;
79
98
80
99
contextEntity . Relationships . ForEach ( r =>
@@ -88,12 +107,12 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
88
107
}
89
108
} ;
90
109
91
- if ( _hasRelationship ( r . RelationshipName ) )
110
+ if ( _relationshipIsIncluded ( r . RelationshipName ) )
92
111
{
93
112
var navigationEntity = _jsonApiContext . ContextGraph
94
113
. GetRelationship ( entity , r . RelationshipName ) ;
95
114
96
- if ( navigationEntity is IEnumerable )
115
+ if ( navigationEntity is IEnumerable )
97
116
relationshipData . ManyData = _getRelationships ( ( IEnumerable < object > ) navigationEntity , r . RelationshipName ) ;
98
117
else
99
118
relationshipData . SingleData = _getRelationship ( navigationEntity , r . RelationshipName ) ;
@@ -103,20 +122,60 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
103
122
} ) ;
104
123
}
105
124
106
- private bool _hasRelationship ( string relationshipName )
125
+ private List < DocumentData > _getIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
126
+ {
127
+ var included = new List < DocumentData > ( ) ;
128
+
129
+ contextEntity . Relationships . ForEach ( r =>
130
+ {
131
+ if ( ! _relationshipIsIncluded ( r . RelationshipName ) ) return ;
132
+
133
+ var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . RelationshipName ) ;
134
+
135
+ if ( navigationEntity is IEnumerable )
136
+ foreach ( var includedEntity in ( IEnumerable ) navigationEntity )
137
+ included . Add ( _getIncludedEntity ( ( IIdentifiable ) includedEntity ) ) ;
138
+ else
139
+ included . Add ( _getIncludedEntity ( ( IIdentifiable ) navigationEntity ) ) ;
140
+ } ) ;
141
+
142
+ return included ;
143
+ }
144
+
145
+ private DocumentData _getIncludedEntity ( IIdentifiable entity )
107
146
{
108
- return _jsonApiContext . IncludedRelationships != null &&
147
+ var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) ;
148
+
149
+ var data = new DocumentData
150
+ {
151
+ Type = contextEntity . EntityName ,
152
+ Id = entity . Id . ToString ( )
153
+ } ;
154
+
155
+ data . Attributes = new Dictionary < string , object > ( ) ;
156
+
157
+ contextEntity . Attributes . ForEach ( attr =>
158
+ {
159
+ data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
160
+ } ) ;
161
+
162
+ return data ;
163
+ }
164
+
165
+ private bool _relationshipIsIncluded ( string relationshipName )
166
+ {
167
+ return _jsonApiContext . IncludedRelationships != null &&
109
168
_jsonApiContext . IncludedRelationships . Contains ( relationshipName . ToProperCase ( ) ) ;
110
169
}
111
170
112
171
private List < Dictionary < string , string > > _getRelationships ( IEnumerable < object > entities , string relationshipName )
113
172
{
114
173
var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
115
-
174
+
116
175
var typeName = _jsonApiContext . ContextGraph . GetContextEntity ( objType ) ;
117
176
118
177
var relationships = new List < Dictionary < string , string > > ( ) ;
119
- foreach ( var entity in entities )
178
+ foreach ( var entity in entities )
120
179
{
121
180
relationships . Add ( new Dictionary < string , string > {
122
181
{ "type" , typeName . EntityName . Dasherize ( ) } ,
@@ -128,7 +187,7 @@ private List<Dictionary<string, string>> _getRelationships(IEnumerable<object> e
128
187
private Dictionary < string , string > _getRelationship ( object entity , string relationshipName )
129
188
{
130
189
var objType = entity . GetType ( ) ;
131
-
190
+
132
191
var typeName = _jsonApiContext . ContextGraph . GetContextEntity ( objType ) ;
133
192
134
193
return new Dictionary < string , string > {
0 commit comments