@@ -27,15 +27,17 @@ public Document Build(IIdentifiable entity)
2727 Data = _getData ( contextEntity , entity )
2828 } ;
2929
30+ document . Included = _appendIncludedObject ( document . Included , contextEntity , entity ) ;
31+
3032 return document ;
3133 }
3234
3335 public Documents Build ( IEnumerable < IIdentifiable > entities )
3436 {
3537 var entityType = entities
36- . GetType ( )
37- . GenericTypeArguments [ 0 ] ;
38-
38+ . GetType ( )
39+ . GenericTypeArguments [ 0 ] ;
40+
3941 var contextEntity = _contextGraph . GetContextEntity ( entityType ) ;
4042
4143 var documents = new Documents
@@ -44,9 +46,25 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
4446 } ;
4547
4648 foreach ( var entity in entities )
49+ {
4750 documents . Data . Add ( _getData ( contextEntity , entity ) ) ;
51+ documents . Included = _appendIncludedObject ( documents . Included , contextEntity , entity ) ;
52+ }
4853
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 ;
5068 }
5169
5270 private DocumentData _getData ( ContextEntity contextEntity , IIdentifiable entity )
@@ -61,20 +79,21 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
6179 return data ;
6280
6381 data . Attributes = new Dictionary < string , object > ( ) ;
64- data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
6582
6683 contextEntity . Attributes . ForEach ( attr =>
6784 {
6885 data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
6986 } ) ;
7087
71- _addRelationships ( data , contextEntity , entity ) ;
88+ if ( contextEntity . Relationships . Count > 0 )
89+ _addRelationships ( data , contextEntity , entity ) ;
7290
7391 return data ;
7492 }
7593
7694 private void _addRelationships ( DocumentData data , ContextEntity contextEntity , IIdentifiable entity )
7795 {
96+ data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
7897 var linkBuilder = new LinkBuilder ( _jsonApiContext ) ;
7998
8099 contextEntity . Relationships . ForEach ( r =>
@@ -88,12 +107,12 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
88107 }
89108 } ;
90109
91- if ( _hasRelationship ( r . RelationshipName ) )
110+ if ( _relationshipIsIncluded ( r . RelationshipName ) )
92111 {
93112 var navigationEntity = _jsonApiContext . ContextGraph
94113 . GetRelationship ( entity , r . RelationshipName ) ;
95114
96- if ( navigationEntity is IEnumerable )
115+ if ( navigationEntity is IEnumerable )
97116 relationshipData . ManyData = _getRelationships ( ( IEnumerable < object > ) navigationEntity , r . RelationshipName ) ;
98117 else
99118 relationshipData . SingleData = _getRelationship ( navigationEntity , r . RelationshipName ) ;
@@ -103,20 +122,60 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
103122 } ) ;
104123 }
105124
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 )
107146 {
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 &&
109168 _jsonApiContext . IncludedRelationships . Contains ( relationshipName . ToProperCase ( ) ) ;
110169 }
111170
112171 private List < Dictionary < string , string > > _getRelationships ( IEnumerable < object > entities , string relationshipName )
113172 {
114173 var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
115-
174+
116175 var typeName = _jsonApiContext . ContextGraph . GetContextEntity ( objType ) ;
117176
118177 var relationships = new List < Dictionary < string , string > > ( ) ;
119- foreach ( var entity in entities )
178+ foreach ( var entity in entities )
120179 {
121180 relationships . Add ( new Dictionary < string , string > {
122181 { "type" , typeName . EntityName . Dasherize ( ) } ,
@@ -128,7 +187,7 @@ private List<Dictionary<string, string>> _getRelationships(IEnumerable<object> e
128187 private Dictionary < string , string > _getRelationship ( object entity , string relationshipName )
129188 {
130189 var objType = entity . GetType ( ) ;
131-
190+
132191 var typeName = _jsonApiContext . ContextGraph . GetContextEntity ( objType ) ;
133192
134193 return new Dictionary < string , string > {
0 commit comments