1515using JsonApiDotNetCore . Extensions ;
1616using JsonApiDotNetCore . JsonApi ;
1717using JsonApiDotNetCore . Routing ;
18+ using Microsoft . EntityFrameworkCore ;
1819
1920namespace JsonApiDotNetCore . Services
2021{
@@ -121,7 +122,8 @@ private JsonApiDatum ResourceToJsonApiDatum(JsonApiContext context, IJsonApiReso
121122 Type = context . Route . RouteDefinition . ContextPropertyName . ToCamelCase ( ) ,
122123 Id = resource . Id ,
123124 Attributes = GetAttributesFromResource ( resource ) ,
124- Links = GetJsonApiDatumLinks ( context , resource )
125+ Links = GetJsonApiDatumLinks ( context , resource ) ,
126+ Relationships = BuildRelationshipsObject ( context , resource )
125127 } ;
126128 }
127129
@@ -147,28 +149,33 @@ private Dictionary<string, string> GetJsonApiDocumentLinks(JsonApiContext jsonAp
147149 var request = jsonApiContext . HttpContext . Request ;
148150 var route = jsonApiContext . Route ;
149151
150- var links = new Dictionary < string , string >
151- {
152- {
153- "self" ,
154- RouteBuilder . BuildRoute ( request . Scheme , request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
155- route . RouteDefinition . ContextPropertyName . ToCamelCase ( ) , route . ResourceId )
156- }
157- } ;
158- return links ;
152+ return DocumentBuilder . BuildSelfLink ( request . Scheme , request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
153+ route . RouteDefinition . ContextPropertyName . ToCamelCase ( ) , route . ResourceId ) ;
159154 }
160155
161156 private Dictionary < string , string > GetJsonApiDatumLinks ( JsonApiContext jsonApiContext , IJsonApiResource resource )
162157 {
163- var links = new Dictionary < string , string >
164- {
158+ return DocumentBuilder . BuildSelfLink ( jsonApiContext . HttpContext . Request . Scheme ,
159+ jsonApiContext . HttpContext . Request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
160+ jsonApiContext . Route . RouteDefinition . ContextPropertyName . ToCamelCase ( ) , resource . Id ) ;
161+ }
162+
163+ private Dictionary < string , object > BuildRelationshipsObject ( JsonApiContext jsonApiContext , IJsonApiResource resource )
164+ {
165+ var relationships = new Dictionary < string , object > ( ) ;
166+ jsonApiContext . Route . Model . GetProperties ( ) . Where ( propertyInfo => propertyInfo . GetMethod . IsVirtual ) . ToList ( ) . ForEach (
167+ virtualProperty =>
165168 {
166- "self" ,
167- RouteBuilder . BuildRoute ( jsonApiContext . HttpContext . Request . Scheme , jsonApiContext . HttpContext . Request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
168- jsonApiContext . Route . RouteDefinition . ContextPropertyName . ToCamelCase ( ) , resource . Id )
169- }
170- } ;
171- return links ;
169+ relationships . Add ( virtualProperty . Name , GetRelationshipLinks ( jsonApiContext , resource , virtualProperty . Name ) ) ;
170+ } ) ;
171+ return relationships ;
172+ }
173+
174+ private Dictionary < string , string > GetRelationshipLinks ( JsonApiContext jsonApiContext , IJsonApiResource resource , string relationshipName )
175+ {
176+ return DocumentBuilder . BuildRelationshipLinks ( jsonApiContext . HttpContext . Request . Scheme ,
177+ jsonApiContext . HttpContext . Request . Host . ToString ( ) , _jsonApiModelConfiguration . Namespace ,
178+ jsonApiContext . Route . RouteDefinition . ContextPropertyName . ToCamelCase ( ) , resource . Id , relationshipName ) ;
172179 }
173180 }
174181}
0 commit comments