Skip to content

Commit a90e828

Browse files
committed
feat(document-builder): build meta object
1 parent 097b601 commit a90e828

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using JsonApiDotNetCore.Extensions;
45
using JsonApiDotNetCore.Internal;
56
using 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 = (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+
5777
private List<DocumentData> _appendIncludedObject(List<DocumentData> includedObject, ContextEntity contextEntity, IIdentifiable entity)
5878
{
5979
var includedEntities = _getIncludedEntities(contextEntity, entity);

0 commit comments

Comments
 (0)