8
8
9
9
namespace JsonApiDotNetCore . Builders
10
10
{
11
- public class DocumentBuilder
11
+ public class DocumentBuilder : IDocumentBuilder
12
12
{
13
13
private IJsonApiContext _jsonApiContext ;
14
14
private IContextGraph _contextGraph ;
15
+ private readonly IRequestMeta _requestMeta ;
15
16
16
17
public DocumentBuilder ( IJsonApiContext jsonApiContext )
17
18
{
18
19
_jsonApiContext = jsonApiContext ;
19
20
_contextGraph = jsonApiContext . ContextGraph ;
20
21
}
21
22
23
+ public DocumentBuilder ( IJsonApiContext jsonApiContext , IRequestMeta requestMeta )
24
+ {
25
+ _jsonApiContext = jsonApiContext ;
26
+ _contextGraph = jsonApiContext . ContextGraph ;
27
+ _requestMeta = requestMeta ;
28
+ }
29
+
22
30
public Document Build ( IIdentifiable entity )
23
31
{
24
32
var contextEntity = _contextGraph . GetContextEntity ( entity . GetType ( ) ) ;
25
33
26
34
var document = new Document
27
35
{
28
- Data = _getData ( contextEntity , entity ) ,
29
- Meta = _getMeta ( entity ) ,
36
+ Data = GetData ( contextEntity , entity ) ,
37
+ Meta = GetMeta ( entity ) ,
30
38
Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
31
39
} ;
32
40
33
- document . Included = _appendIncludedObject ( document . Included , contextEntity , entity ) ;
41
+ document . Included = AppendIncludedObject ( document . Included , contextEntity , entity ) ;
34
42
35
43
return document ;
36
44
}
@@ -46,39 +54,42 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
46
54
var documents = new Documents
47
55
{
48
56
Data = new List < DocumentData > ( ) ,
49
- Meta = _getMeta ( entities . FirstOrDefault ( ) ) ,
57
+ Meta = GetMeta ( entities . FirstOrDefault ( ) ) ,
50
58
Links = _jsonApiContext . PageManager . GetPageLinks ( new LinkBuilder ( _jsonApiContext ) )
51
59
} ;
52
60
53
61
foreach ( var entity in entities )
54
62
{
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 ) ;
57
65
}
58
66
59
67
return documents ;
60
68
}
61
69
62
- private Dictionary < string , object > _getMeta ( IIdentifiable entity )
70
+ private Dictionary < string , object > GetMeta ( IIdentifiable entity )
63
71
{
64
72
if ( entity == null ) return null ;
65
-
66
- var meta = new Dictionary < string , object > ( ) ;
67
- var metaEntity = entity as IHasMeta ;
68
73
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 ) ) ;
71
78
72
79
if ( _jsonApiContext . Options . IncludeTotalRecordCount )
73
- meta [ "total-records" ] = _jsonApiContext . PageManager . TotalRecords ;
80
+ builder . Add ( "total-records" , _jsonApiContext . PageManager . TotalRecords ) ;
74
81
82
+ if ( _requestMeta != null )
83
+ builder . Add ( _requestMeta . GetMeta ( ) ) ;
84
+
85
+ var meta = builder . Build ( ) ;
75
86
if ( meta . Count > 0 ) return meta ;
76
87
return null ;
77
88
}
78
89
79
- private List < DocumentData > _appendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
90
+ private List < DocumentData > AppendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
80
91
{
81
- var includedEntities = _getIncludedEntities ( contextEntity , entity ) ;
92
+ var includedEntities = GetIncludedEntities ( contextEntity , entity ) ;
82
93
if ( includedEntities . Count > 0 )
83
94
{
84
95
if ( includedObject == null )
@@ -89,7 +100,7 @@ private List<DocumentData> _appendIncludedObject(List<DocumentData> includedObje
89
100
return includedObject ;
90
101
}
91
102
92
- private DocumentData _getData ( ContextEntity contextEntity , IIdentifiable entity )
103
+ private DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity )
93
104
{
94
105
var data = new DocumentData
95
106
{
@@ -104,16 +115,24 @@ private DocumentData _getData(ContextEntity contextEntity, IIdentifiable entity)
104
115
105
116
contextEntity . Attributes . ForEach ( attr =>
106
117
{
107
- data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
118
+ if ( ShouldIncludeAttribute ( attr ) )
119
+ data . Attributes . Add ( attr . PublicAttributeName , attr . GetValue ( entity ) ) ;
108
120
} ) ;
109
121
110
122
if ( contextEntity . Relationships . Count > 0 )
111
- _addRelationships ( data , contextEntity , entity ) ;
123
+ AddRelationships ( data , contextEntity , entity ) ;
112
124
113
125
return data ;
114
126
}
115
127
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 )
117
136
{
118
137
data . Relationships = new Dictionary < string , RelationshipData > ( ) ;
119
138
var linkBuilder = new LinkBuilder ( _jsonApiContext ) ;
@@ -129,54 +148,57 @@ private void _addRelationships(DocumentData data, ContextEntity contextEntity, I
129
148
}
130
149
} ;
131
150
132
- if ( _relationshipIsIncluded ( r . InternalRelationshipName ) )
151
+ if ( RelationshipIsIncluded ( r . InternalRelationshipName ) )
133
152
{
134
153
var navigationEntity = _jsonApiContext . ContextGraph
135
154
. GetRelationship ( entity , r . InternalRelationshipName ) ;
136
155
137
156
if ( navigationEntity == null )
138
157
relationshipData . SingleData = null ;
139
158
else if ( navigationEntity is IEnumerable )
140
- relationshipData . ManyData = _getRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
159
+ relationshipData . ManyData = GetRelationships ( ( IEnumerable < object > ) navigationEntity , r . InternalRelationshipName ) ;
141
160
else
142
- relationshipData . SingleData = _getRelationship ( navigationEntity , r . InternalRelationshipName ) ;
161
+ relationshipData . SingleData = GetRelationship ( navigationEntity , r . InternalRelationshipName ) ;
143
162
}
144
163
145
164
data . Relationships . Add ( r . InternalRelationshipName . Dasherize ( ) , relationshipData ) ;
146
165
} ) ;
147
166
}
148
167
149
- private List < DocumentData > _getIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
168
+ private List < DocumentData > GetIncludedEntities ( ContextEntity contextEntity , IIdentifiable entity )
150
169
{
151
170
var included = new List < DocumentData > ( ) ;
152
171
153
172
contextEntity . Relationships . ForEach ( r =>
154
173
{
155
- if ( ! _relationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
174
+ if ( ! RelationshipIsIncluded ( r . InternalRelationshipName ) ) return ;
156
175
157
176
var navigationEntity = _jsonApiContext . ContextGraph . GetRelationship ( entity , r . InternalRelationshipName ) ;
158
177
159
178
if ( navigationEntity is IEnumerable )
160
179
foreach ( var includedEntity in ( IEnumerable ) navigationEntity )
161
- included . Add ( _getIncludedEntity ( ( IIdentifiable ) includedEntity ) ) ;
180
+ AddIncludedEntity ( included , ( IIdentifiable ) includedEntity ) ;
162
181
else
163
- included . Add ( _getIncludedEntity ( ( IIdentifiable ) navigationEntity ) ) ;
182
+ AddIncludedEntity ( included , ( IIdentifiable ) navigationEntity ) ;
164
183
} ) ;
165
184
166
185
return included ;
167
186
}
168
187
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 )
170
196
{
171
197
if ( entity == null ) return null ;
172
198
173
199
var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) ;
174
200
175
- var data = new DocumentData
176
- {
177
- Type = contextEntity . EntityName ,
178
- Id = entity . StringId
179
- } ;
201
+ var data = GetData ( contextEntity , entity ) ;
180
202
181
203
data . Attributes = new Dictionary < string , object > ( ) ;
182
204
@@ -188,13 +210,13 @@ private DocumentData _getIncludedEntity(IIdentifiable entity)
188
210
return data ;
189
211
}
190
212
191
- private bool _relationshipIsIncluded ( string relationshipName )
213
+ private bool RelationshipIsIncluded ( string relationshipName )
192
214
{
193
215
return _jsonApiContext . IncludedRelationships != null &&
194
216
_jsonApiContext . IncludedRelationships . Contains ( relationshipName . ToProperCase ( ) ) ;
195
217
}
196
218
197
- private List < Dictionary < string , string > > _getRelationships ( IEnumerable < object > entities , string relationshipName )
219
+ private List < Dictionary < string , string > > GetRelationships ( IEnumerable < object > entities , string relationshipName )
198
220
{
199
221
var objType = entities . GetType ( ) . GenericTypeArguments [ 0 ] ;
200
222
@@ -210,7 +232,7 @@ private List<Dictionary<string, string>> _getRelationships(IEnumerable<object> e
210
232
}
211
233
return relationships ;
212
234
}
213
- private Dictionary < string , string > _getRelationship ( object entity , string relationshipName )
235
+ private Dictionary < string , string > GetRelationship ( object entity , string relationshipName )
214
236
{
215
237
var objType = entity . GetType ( ) ;
216
238
0 commit comments