|
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()); |
@@ -62,16 +70,19 @@ public Documents Build(IEnumerable<IIdentifiable> entities) |
62 | 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 | } |
|
0 commit comments