1
1
using System . Collections ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using JsonApiDotNetCore . Extensions ;
4
5
using JsonApiDotNetCore . Internal ;
5
6
using JsonApiDotNetCore . Models ;
@@ -24,7 +25,8 @@ public Document Build(IIdentifiable entity)
24
25
25
26
var document = new Document
26
27
{
27
- Data = _getData ( contextEntity , entity )
28
+ Data = _getData ( contextEntity , entity ) ,
29
+ Meta = _getMeta ( entity )
28
30
} ;
29
31
30
32
document . Included = _appendIncludedObject ( document . Included , contextEntity , entity ) ;
@@ -42,7 +44,8 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
42
44
43
45
var documents = new Documents
44
46
{
45
- Data = new List < DocumentData > ( )
47
+ Data = new List < DocumentData > ( ) ,
48
+ Meta = _getMeta ( entities . FirstOrDefault ( ) )
46
49
} ;
47
50
48
51
foreach ( var entity in entities )
@@ -54,6 +57,23 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
54
57
return documents ;
55
58
}
56
59
60
+ private Dictionary < string , object > _getMeta ( IIdentifiable entity )
61
+ {
62
+ if ( entity == null ) return null ;
63
+
64
+ var meta = new Dictionary < string , object > ( ) ;
65
+ var metaEntity = ( IHasMeta ) entity ;
66
+
67
+ if ( metaEntity != null )
68
+ meta = metaEntity . GetMeta ( _jsonApiContext ) ;
69
+
70
+ if ( _jsonApiContext . Options . IncludeTotalRecordCount )
71
+ meta [ "total-records" ] = _jsonApiContext . TotalRecords ;
72
+
73
+ if ( meta . Count > 0 ) return meta ;
74
+ return null ;
75
+ }
76
+
57
77
private List < DocumentData > _appendIncludedObject ( List < DocumentData > includedObject , ContextEntity contextEntity , IIdentifiable entity )
58
78
{
59
79
var includedEntities = _getIncludedEntities ( contextEntity , entity ) ;
0 commit comments