|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | 2 | using System.Linq.Expressions; |
5 | 3 | using JsonApiDotNetCore.Configuration; |
6 | 4 | using JsonApiDotNetCore.Queries; |
7 | 5 | using JsonApiDotNetCore.Queries.Expressions; |
8 | 6 | using JsonApiDotNetCore.Queries.Internal.QueryableBuilding; |
9 | 7 | using JsonApiDotNetCore.Resources; |
10 | | -using JsonApiDotNetCore.Resources.Annotations; |
11 | 8 | using Microsoft.EntityFrameworkCore.Metadata; |
12 | 9 |
|
13 | 10 | namespace JsonApiDotNetCore.MongoDb.Queries.Internal.QueryableBuilding |
14 | 11 | { |
15 | 12 | /// <summary> |
16 | 13 | /// Drives conversion from <see cref="QueryLayer"/> into system <see cref="Expression"/> trees. |
17 | 14 | /// </summary> |
18 | | - /// <remarks> |
19 | | - /// This class was copied from JsonApiDotNetCore, so it can use <see cref="MongoDbWhereClauseBuilder"/> instead. |
20 | | - /// </remarks> |
21 | | - public sealed class MongoDbQueryableBuilder |
| 15 | + public sealed class MongoDbQueryableBuilder : QueryableBuilder |
22 | 16 | { |
23 | | - private readonly Expression _source; |
24 | 17 | private readonly Type _elementType; |
25 | 18 | private readonly Type _extensionType; |
26 | | - private readonly LambdaParameterNameFactory _nameFactory; |
27 | | - private readonly IResourceFactory _resourceFactory; |
28 | | - private readonly IResourceContextProvider _resourceContextProvider; |
29 | | - private readonly IModel _entityModel; |
30 | 19 | private readonly LambdaScopeFactory _lambdaScopeFactory; |
31 | 20 |
|
32 | | - public MongoDbQueryableBuilder(Expression source, Type elementType, Type extensionType, LambdaParameterNameFactory nameFactory, |
33 | | - IResourceFactory resourceFactory, IResourceContextProvider resourceContextProvider, IModel entityModel, |
| 21 | + public MongoDbQueryableBuilder(Expression source, Type elementType, Type extensionType, |
| 22 | + LambdaParameterNameFactory nameFactory, IResourceFactory resourceFactory, |
| 23 | + IResourceContextProvider resourceContextProvider, IModel entityModel, |
34 | 24 | LambdaScopeFactory lambdaScopeFactory = null) |
| 25 | + : base(source, elementType, extensionType, nameFactory, resourceFactory, resourceContextProvider, |
| 26 | + entityModel, lambdaScopeFactory) |
35 | 27 | { |
36 | | - _source = source ?? throw new ArgumentNullException(nameof(source)); |
37 | 28 | _elementType = elementType ?? throw new ArgumentNullException(nameof(elementType)); |
38 | 29 | _extensionType = extensionType ?? throw new ArgumentNullException(nameof(extensionType)); |
39 | | - _nameFactory = nameFactory ?? throw new ArgumentNullException(nameof(nameFactory)); |
40 | | - _resourceFactory = resourceFactory ?? throw new ArgumentNullException(nameof(resourceFactory)); |
41 | | - _resourceContextProvider = resourceContextProvider ?? throw new ArgumentNullException(nameof(resourceContextProvider)); |
42 | | - _entityModel = entityModel ?? throw new ArgumentNullException(nameof(entityModel)); |
43 | | - _lambdaScopeFactory = lambdaScopeFactory ?? new LambdaScopeFactory(_nameFactory); |
| 30 | + _lambdaScopeFactory = lambdaScopeFactory ?? new LambdaScopeFactory(nameFactory); |
44 | 31 | } |
45 | 32 |
|
46 | | - public Expression ApplyQuery(QueryLayer layer) |
47 | | - { |
48 | | - if (layer == null) throw new ArgumentNullException(nameof(layer)); |
49 | | - |
50 | | - Expression expression = _source; |
51 | | - |
52 | | - if (layer.Include != null) |
53 | | - { |
54 | | - expression = ApplyInclude(expression, layer.Include, layer.ResourceContext); |
55 | | - } |
56 | | - |
57 | | - if (layer.Filter != null) |
58 | | - { |
59 | | - expression = ApplyFilter(expression, layer.Filter); |
60 | | - } |
61 | | - |
62 | | - if (layer.Sort != null) |
63 | | - { |
64 | | - expression = ApplySort(expression, layer.Sort); |
65 | | - } |
66 | | - |
67 | | - if (layer.Pagination != null) |
68 | | - { |
69 | | - expression = ApplyPagination(expression, layer.Pagination); |
70 | | - } |
71 | | - |
72 | | - if (layer.Projection != null && layer.Projection.Any()) |
73 | | - { |
74 | | - expression = ApplyProjection(expression, layer.Projection, layer.ResourceContext); |
75 | | - } |
76 | | - |
77 | | - return expression; |
78 | | - } |
79 | | - |
80 | | - private Expression ApplyInclude(Expression source, IncludeExpression include, ResourceContext resourceContext) |
81 | | - { |
82 | | - using var lambdaScope = _lambdaScopeFactory.CreateScope(_elementType); |
83 | | - |
84 | | - var builder = new IncludeClauseBuilder(source, lambdaScope, resourceContext, _resourceContextProvider); |
85 | | - return builder.ApplyInclude(include); |
86 | | - } |
87 | | - |
88 | | - private Expression ApplyFilter(Expression source, FilterExpression filter) |
| 33 | + protected override Expression ApplyFilter(Expression source, FilterExpression filter) |
89 | 34 | { |
90 | 35 | using var lambdaScope = _lambdaScopeFactory.CreateScope(_elementType); |
91 | 36 |
|
92 | 37 | var builder = new MongoDbWhereClauseBuilder(source, lambdaScope, _extensionType); |
93 | 38 | return builder.ApplyWhere(filter); |
94 | 39 | } |
95 | | - |
96 | | - private Expression ApplySort(Expression source, SortExpression sort) |
97 | | - { |
98 | | - using var lambdaScope = _lambdaScopeFactory.CreateScope(_elementType); |
99 | | - |
100 | | - var builder = new OrderClauseBuilder(source, lambdaScope, _extensionType); |
101 | | - return builder.ApplyOrderBy(sort); |
102 | | - } |
103 | | - |
104 | | - private Expression ApplyPagination(Expression source, PaginationExpression pagination) |
105 | | - { |
106 | | - using var lambdaScope = _lambdaScopeFactory.CreateScope(_elementType); |
107 | | - |
108 | | - var builder = new SkipTakeClauseBuilder(source, lambdaScope, _extensionType); |
109 | | - return builder.ApplySkipTake(pagination); |
110 | | - } |
111 | | - |
112 | | - private Expression ApplyProjection(Expression source, IDictionary<ResourceFieldAttribute, QueryLayer> projection, ResourceContext resourceContext) |
113 | | - { |
114 | | - using var lambdaScope = _lambdaScopeFactory.CreateScope(_elementType); |
115 | | - |
116 | | - var builder = new SelectClauseBuilder(source, lambdaScope, _entityModel, _extensionType, _nameFactory, _resourceFactory, _resourceContextProvider); |
117 | | - return builder.ApplySelect(projection, resourceContext); |
118 | | - } |
119 | 40 | } |
120 | 41 | } |
0 commit comments