|
1 |
| -using System; |
2 |
| -using System.Data.Entity; |
3 |
| -using System.Linq; |
4 |
| -using System.Threading; |
5 |
| -using System.Threading.Tasks; |
6 |
| -using JSONAPI.Documents; |
7 |
| -using JSONAPI.Documents.Builders; |
8 |
| -using JSONAPI.Http; |
9 |
| -using JSONAPI.QueryableTransformers; |
10 |
| - |
11 |
| -namespace JSONAPI.EntityFramework.Documents.Builders |
12 |
| -{ |
13 |
| - /// <summary> |
14 |
| - /// Provides a entity framework implementation of an IQueryableResourceCollectionDocumentBuilder |
15 |
| - /// </summary> |
16 |
| - public class EntityFrameworkQueryableResourceCollectionDocumentBuilder: DefaultQueryableResourceCollectionDocumentBuilder |
17 |
| - { |
18 |
| - /// <summary> |
19 |
| - /// Creates a new EntityFrameworkQueryableResourceCollectionDocumentBuilder |
20 |
| - /// </summary> |
21 |
| - public EntityFrameworkQueryableResourceCollectionDocumentBuilder( |
22 |
| - IResourceCollectionDocumentBuilder resourceCollectionDocumentBuilder, |
23 |
| - IQueryableEnumerationTransformer enumerationTransformer, |
24 |
| - IQueryableFilteringTransformer filteringTransformer, |
25 |
| - IQueryableSortingTransformer sortingTransformer, |
26 |
| - IQueryablePaginationTransformer paginationTransformer, |
27 |
| - IBaseUrlService baseUrlService) : |
28 |
| - base(resourceCollectionDocumentBuilder, |
29 |
| - enumerationTransformer, |
30 |
| - filteringTransformer, |
31 |
| - sortingTransformer, |
32 |
| - paginationTransformer, |
33 |
| - baseUrlService) |
34 |
| - { |
35 |
| - } |
36 |
| - |
37 |
| - /// <summary> |
38 |
| - /// Returns the metadata that should be sent with this document. |
39 |
| - /// </summary> |
40 |
| - protected override async Task<IMetadata> GetDocumentMetadata<T>(IQueryable<T> originalQuery, IQueryable<T> filteredQuery, IOrderedQueryable<T> sortedQuery, |
41 |
| - IPaginationTransformResult<T> paginationResult, CancellationToken cancellationToken) |
42 |
| - { |
43 |
| - var metadata = new Metadata(); |
44 |
| - if (paginationResult.PaginationWasApplied) |
45 |
| - { |
46 |
| - var count = await filteredQuery.CountAsync(cancellationToken); |
47 |
| - metadata.MetaObject.Add("total-pages", (int)Math.Ceiling((decimal) count / paginationResult.PageSize)); |
48 |
| - metadata.MetaObject.Add("total-count", count); |
49 |
| - } |
50 |
| - if (metadata.MetaObject.HasValues) |
51 |
| - return metadata; |
52 |
| - return null; |
53 |
| - } |
54 |
| - } |
55 |
| -} |
| 1 | +using System; |
| 2 | +using System.Data.Entity; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using JSONAPI.Documents; |
| 7 | +using JSONAPI.Documents.Builders; |
| 8 | +using JSONAPI.Http; |
| 9 | +using JSONAPI.QueryableTransformers; |
| 10 | + |
| 11 | +namespace JSONAPI.EntityFramework.Documents.Builders |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides a entity framework implementation of an IQueryableResourceCollectionDocumentBuilder |
| 15 | + /// </summary> |
| 16 | + public class EntityFrameworkQueryableResourceCollectionDocumentBuilder: DefaultQueryableResourceCollectionDocumentBuilder |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Creates a new EntityFrameworkQueryableResourceCollectionDocumentBuilder |
| 20 | + /// </summary> |
| 21 | + public EntityFrameworkQueryableResourceCollectionDocumentBuilder( |
| 22 | + IResourceCollectionDocumentBuilder resourceCollectionDocumentBuilder, |
| 23 | + IQueryableEnumerationTransformer enumerationTransformer, |
| 24 | + IQueryableFilteringTransformer filteringTransformer, |
| 25 | + IQueryableSortingTransformer sortingTransformer, |
| 26 | + IQueryablePaginationTransformer paginationTransformer, |
| 27 | + IBaseUrlService baseUrlService) : |
| 28 | + base(resourceCollectionDocumentBuilder, |
| 29 | + enumerationTransformer, |
| 30 | + filteringTransformer, |
| 31 | + sortingTransformer, |
| 32 | + paginationTransformer, |
| 33 | + baseUrlService) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Returns the metadata that should be sent with this document. |
| 39 | + /// </summary> |
| 40 | + protected override async Task<IMetadata> GetDocumentMetadata<T>(IQueryable<T> originalQuery, IQueryable<T> filteredQuery, IOrderedQueryable<T> sortedQuery, |
| 41 | + IPaginationTransformResult<T> paginationResult, CancellationToken cancellationToken) |
| 42 | + { |
| 43 | + var metadata = new Metadata(); |
| 44 | + if (paginationResult.PaginationWasApplied) |
| 45 | + { |
| 46 | + var count = await filteredQuery.CountAsync(cancellationToken); |
| 47 | + metadata.MetaObject.Add("total-pages", (int)Math.Ceiling((decimal) count / paginationResult.PageSize)); |
| 48 | + metadata.MetaObject.Add("total-count", count); |
| 49 | + metadata.MetaObject.Add("total_resources", count); |
| 50 | + } |
| 51 | + if (metadata.MetaObject.HasValues) |
| 52 | + return metadata; |
| 53 | + return null; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments