|
| 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 = filteredQuery.CountAsync(cancellationToken); |
| 47 | + metadata.MetaObject.Add("total-pages", (int)Math.Ceiling((decimal) await count / paginationResult.PageSize)); |
| 48 | + metadata.MetaObject.Add("total-count", await count); |
| 49 | + } |
| 50 | + if (metadata.MetaObject.HasValues) |
| 51 | + return metadata; |
| 52 | + return null; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments