88
99namespace JsonApiDotNetCore . Builders
1010{
11- public class DocumentBuilder
11+ public class DocumentBuilder : IDocumentBuilder
1212 {
1313 private IJsonApiContext _jsonApiContext ;
1414 private IContextGraph _contextGraph ;
15+ private readonly IRequestMeta _requestMeta ;
1516
1617 public DocumentBuilder ( IJsonApiContext jsonApiContext )
1718 {
1819 _jsonApiContext = jsonApiContext ;
1920 _contextGraph = jsonApiContext . ContextGraph ;
2021 }
2122
23+ public DocumentBuilder ( IJsonApiContext jsonApiContext , IRequestMeta requestMeta )
24+ {
25+ _jsonApiContext = jsonApiContext ;
26+ _contextGraph = jsonApiContext . ContextGraph ;
27+ _requestMeta = requestMeta ;
28+ }
29+
2230 public Document Build ( IIdentifiable entity )
2331 {
2432 var contextEntity = _contextGraph . GetContextEntity ( entity . GetType ( ) ) ;
2533
2634 var document = new Document
2735 {
28- Data = _getData ( contextEntity , entity ) ,
29- Meta = _getMeta ( entity ) ,
36+ Data = GetData ( contextEntity , entity ) ,
37+ Meta = GetMeta ( entity ) ,
3038 Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
3139 } ;
3240
33- document . Included = _appendIncludedObject ( document . Included , contextEntity , entity ) ;
41+ document . Included = AppendIncludedObject ( document . Included , contextEntity , entity ) ;
3442
3543 return document ;
3644 }
@@ -46,39 +54,42 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
4654 var documents = new Documents
4755 {
4856 Data = new List < DocumentData > ( ) ,
49- Meta = _getMeta ( entities . FirstOrDefault ( ) ) ,
57+ Meta = GetMeta ( entities . FirstOrDefault ( ) ) ,
5058 Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
5159 } ;
5260
5361 foreach ( var entity in entities )
5462 {
55- documents . Data . Add ( _getData ( contextEntity , entity ) ) ;
56- documents . Included = _appendIncludedObject ( documents . Included , contextEntity , entity ) ;
63+ documents . Data . Add ( GetData ( contextEntity , entity ) ) ;
64+ documents . Included = AppendIncludedObject ( documents . Included , contextEntity , entity ) ;
5765 }
5866
5967 return documents ;
6068 }
6169
62- private Dictionary < string , object > _getMeta ( IIdentifiable entity )
70+ private Dictionary < string , object > GetMeta ( IIdentifiable entity )
6371 {
6472 if ( entity == null ) return null ;
65-
66- var meta = new Dictionary < string , object > ( ) ;
67- var metaEntity = entity as IHasMeta ;
6873
69- if ( metaEntity != null )
70- meta = metaEntity . GetMeta ( _jsonApiContext ) ;
74+ var builder = _jsonApiContext . MetaBuilder ;
75+
76+ if ( entity is IHasMeta metaEntity )
77+ builder . Add ( metaEntity . GetMeta ( _jsonApiContext ) ) ;
7178
7279 if ( _jsonApiContext . Options . IncludeTotalRecordCount )
73- meta [ "total-records" ] = _jsonApiContext . PageManager . TotalRecords ;
80+ builder . Add ( "total-records" , _jsonApiContext . PageManager . TotalRecords ) ;
7481
82+ if ( _requestMeta != null )
83+ builder . Add ( _requestMeta . GetMeta ( ) ) ;
84+
85+ var meta = builder . Build ( ) ;
7586 if ( meta . Count > 0 ) return meta ;
7687 return null ;
7788 }
7889
79- private List < DocumentData > _appendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
90+ private List < DocumentData > AppendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
8091 {
81- var includedEntities = _getIncludedEntities ( contextEntity , entity ) ;
92+ var includedEntities = GetIncludedEntities ( contextEntity , entity ) ;
8293 if ( includedEntities . Count > 0 )
8394 {
8495 if ( includedObject == null )
@@ -89,7 +100,7 @@ private List<DocumentData> _appendIncludedObject(List<DocumentData> includedObje
89100 return includedObject ;
90101 }
91102
92- private DocumentData _getData ( ContextEntity contextEntity , IIdentifiable entity )
103+ private DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity )
93104 {
94105 var data = new DocumentData
95106 {
@@ -104,16 +115,24 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
104115
105116 contextEntity . Attributes . ForEach ( attr =>
106117 {
107- data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
118+ if ( ShouldIncludeAttribute ( attr ) )
119+ data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
108120 } ) ;
109121
110122 if ( contextEntity . Relationships . Count > 0 )
111- _addRelationships ( data , contextEntity , entity ) ;
123+ AddRelationships ( data , contextEntity , entity ) ;
112124
113125 return data ;
114126 }
115127
116- private void _addRelationships ( DocumentData data , ContextEntity contextEntity , IIdentifiable entity )
128+ private bool ShouldIncludeAttribute ( AttrAttribute attr )
129+ {
130+ return ( _jsonApiContext . QuerySet == null
131+ || _jsonApiContext . QuerySet . Fields . Count == 0
132+ || _jsonApiContext . QuerySet . Fields . Contains ( attr . InternalAttributeName ) ) ;
133+ }
134+
135+ private void AddRelationships ( DocumentData data , ContextEntity contextEntity , IIdentifiable entity )
117136 {
118137 data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
119138 var linkBuilder = new LinkBuilder ( _jsonApiContext ) ;
@@ -129,54 +148,57 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
129148 }
130149 } ;
131150
132- if ( _relationshipIsIncluded ( r . InternalRelationshipName ) )
151+ if ( RelationshipIsIncluded ( r . InternalRelationshipName ) )
133152 {
134153 var navigationEntity = _jsonApiContext . ContextGraph
135154 . GetRelationship ( entity , r . InternalRelationshipName ) ;
136155
137156 if ( navigationEntity == null )
138157 relationshipData . SingleData = null ;
139158 else if ( navigationEntity is IEnumerable )
140- relationshipData . ManyData = _getRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
159+ relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
141160 else
142- relationshipData . SingleData = _getRelationship ( navigationEntity , r . InternalRelationshipName ) ;
161+ relationshipData . SingleData = GetRelationship ( navigationEntity , r . InternalRelationshipName ) ;
143162 }
144163
145164 data . Relationships . Add ( r . InternalRelationshipName . Dasherize ( ) , relationshipData ) ;
146165 } ) ;
147166 }
148167
149- private List < DocumentData > _getIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
168+ private List < DocumentData > GetIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
150169 {
151170 var included = new List < DocumentData > ( ) ;
152171
153172 contextEntity . Relationships . ForEach ( r =>
154173 {
155- if ( ! _relationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
174+ if ( ! RelationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
156175
157176 var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . InternalRelationshipName ) ;
158177
159178 if ( navigationEntity is IEnumerable )
160179 foreach ( var includedEntity in ( IEnumerable ) navigationEntity )
161- included . Add ( _getIncludedEntity ( ( IIdentifiable ) includedEntity ) ) ;
180+ AddIncludedEntity ( included , ( IIdentifiable ) includedEntity ) ;
162181 else
163- included . Add ( _getIncludedEntity ( ( IIdentifiable ) navigationEntity ) ) ;
182+ AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
164183 } ) ;
165184
166185 return included ;
167186 }
168187
169- private DocumentData _getIncludedEntity ( IIdentifiable entity )
188+ private void AddIncludedEntity ( List < DocumentData > entities , IIdentifiable entity )
189+ {
190+ var includedEntity = GetIncludedEntity ( entity ) ;
191+ if ( includedEntity != null )
192+ entities . Add ( includedEntity ) ;
193+ }
194+
195+ private DocumentData GetIncludedEntity ( IIdentifiable entity )
170196 {
171197 if ( entity == null ) return null ;
172198
173199 var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) ;
174200
175- var data = new DocumentData
176- {
177- Type = contextEntity . EntityName ,
178- Id = entity . StringId
179- } ;
201+ var data = GetData ( contextEntity , entity ) ;
180202
181203 data . Attributes = new Dictionary < string , object > ( ) ;
182204
@@ -188,13 +210,13 @@ private DocumentData _getIncludedEntity(IIdentifiable entity)
188210 return data ;
189211 }
190212
191- private bool _relationshipIsIncluded ( string relationshipName )
213+ private bool RelationshipIsIncluded ( string relationshipName )
192214 {
193215 return _jsonApiContext . IncludedRelationships != null &&
194216 _jsonApiContext . IncludedRelationships . Contains ( relationshipName . ToProperCase ( ) ) ;
195217 }
196218
197- private List < Dictionary < string , string > > _getRelationships ( IEnumerable < object > entities , string relationshipName )
219+ private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities , string relationshipName )
198220 {
199221 var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
200222
@@ -210,7 +232,7 @@ private List<Dictionary<string, string>> _getRelationships(IEnumerable<object> e
210232 }
211233 return relationships ;
212234 }
213- private Dictionary < string , string > _getRelationship ( object entity , string relationshipName )
235+ private Dictionary < string , string > GetRelationship ( object entity , string relationshipName )
214236 {
215237 var objType = entity . GetType ( ) ;
216238
0 commit comments