1+ using System ;
2+ using System . Collections ;
13using System . Collections . Generic ;
24using JsonApiDotNetCore . Extensions ;
35using JsonApiDotNetCore . Internal ;
@@ -9,7 +11,7 @@ namespace JsonApiDotNetCore.Builders
911 public class DocumentBuilder
1012 {
1113 private IJsonApiContext _jsonApiContext ;
12- private IContextGraph _contextGraph ;
14+ private IContextGraph _contextGraph ;
1315
1416 public DocumentBuilder ( IJsonApiContext jsonApiContext )
1517 {
@@ -24,7 +26,7 @@ public Document Build(IIdentifiable entity)
2426 var document = new Document
2527 {
2628 Data = _getData ( contextEntity , entity )
27- } ;
29+ } ;
2830
2931 return document ;
3032 }
@@ -44,7 +46,7 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
4446
4547 foreach ( var entity in entities )
4648 documents . Data . Add ( _getData ( contextEntity , entity ) ) ;
47-
49+
4850 return documents ;
4951 }
5052
@@ -56,7 +58,7 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
5658 Id = entity . Id . ToString ( )
5759 } ;
5860
59- if ( _jsonApiContext . IsRelationshipData )
61+ if ( _jsonApiContext . IsRelationshipData )
6062 return data ;
6163
6264 data . Attributes = new Dictionary < string , object > ( ) ;
@@ -67,7 +69,7 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
6769 data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
6870 } ) ;
6971
70- _addRelationships ( data , contextEntity , entity ) ;
72+ _addRelationships ( data , contextEntity , entity ) ;
7173
7274 return data ;
7375 }
@@ -76,15 +78,59 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
7678 {
7779 var linkBuilder = new LinkBuilder ( _jsonApiContext ) ;
7880
79- contextEntity . Relationships . ForEach ( r => {
80- var relationshipData = new RelationshipData {
81- Links = new Links {
81+ contextEntity . Relationships . ForEach ( r =>
82+ {
83+ var relationshipData = new RelationshipData
84+ {
85+ Links = new Links
86+ {
8287 Self = linkBuilder . GetSelfRelationLink ( contextEntity . EntityName , entity . Id . ToString ( ) , r . RelationshipName ) ,
8388 Related = linkBuilder . GetRelatedRelationLink ( contextEntity . EntityName , entity . Id . ToString ( ) , r . RelationshipName )
8489 }
8590 } ;
91+
92+ if ( _jsonApiContext . IncludedRelationships . Contains ( r . RelationshipName . ToProperCase ( ) ) )
93+ {
94+ var navigationEntity = _jsonApiContext . ContextGraph
95+ . GetRelationship ( entity , r . RelationshipName ) ;
96+
97+ if ( navigationEntity is IEnumerable )
98+ relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity , r . RelationshipName ) ;
99+ else
100+ relationshipData . SingleData = GetRelationship ( navigationEntity , r . RelationshipName ) ;
101+ }
102+
103+
104+
86105 data . Relationships . Add ( r . RelationshipName . Dasherize ( ) , relationshipData ) ;
87106 } ) ;
88107 }
108+ private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities , string relationshipName )
109+ {
110+ var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
111+
112+ var typeName = _jsonApiContext . ContextGraph . GetContextEntity ( objType ) ;
113+
114+ var relationships = new List < Dictionary < string , string > > ( ) ;
115+ foreach ( var entity in entities )
116+ {
117+ relationships . Add ( new Dictionary < string , string > {
118+ { "type" , typeName . EntityName } ,
119+ { "id" , ( ( IIdentifiable ) entity ) . Id . ToString ( ) }
120+ } ) ;
121+ }
122+ return relationships ;
123+ }
124+ private Dictionary < string , string > GetRelationship ( object entity , string relationshipName )
125+ {
126+ var objType = entity . GetType ( ) ;
127+
128+ var typeName = _jsonApiContext . ContextGraph . GetContextEntity ( objType ) ;
129+
130+ return new Dictionary < string , string > {
131+ { "type" , typeName . EntityName } ,
132+ { "id" , ( ( IIdentifiable ) entity ) . Id . ToString ( ) }
133+ } ;
134+ }
89135 }
90136}
0 commit comments