11using  System . Collections ; 
22using  System . Collections . Generic ; 
3+ using  System . Linq ; 
34using  JsonApiDotNetCore . Extensions ; 
45using  JsonApiDotNetCore . Internal ; 
56using  JsonApiDotNetCore . Models ; 
@@ -24,7 +25,8 @@ public Document Build(IIdentifiable entity)
2425
2526            var  document  =  new  Document 
2627            { 
27-                 Data  =  _getData ( contextEntity ,  entity ) 
28+                 Data  =  _getData ( contextEntity ,  entity ) , 
29+                 Meta  =  _getMeta ( entity ) 
2830            } ; 
2931
3032            document . Included  =  _appendIncludedObject ( document . Included ,  contextEntity ,  entity ) ; 
@@ -42,7 +44,8 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
4244
4345            var  documents  =  new  Documents 
4446            { 
45-                 Data  =  new  List < DocumentData > ( ) 
47+                 Data  =  new  List < DocumentData > ( ) , 
48+                 Meta  =  _getMeta ( entities . FirstOrDefault ( ) ) 
4649            } ; 
4750
4851            foreach  ( var  entity  in  entities ) 
@@ -54,6 +57,23 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
5457            return  documents ; 
5558        } 
5659
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  =  entity  as  IHasMeta ; 
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+ 
5777        private  List < DocumentData >  _appendIncludedObject ( List < DocumentData >  includedObject ,  ContextEntity  contextEntity ,  IIdentifiable  entity ) 
5878        { 
5979            var  includedEntities  =  _getIncludedEntities ( contextEntity ,  entity ) ; 
0 commit comments