-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathCarCompositeKeyAwareRepository.cs
More file actions
59 lines (50 loc) · 2.32 KB
/
CarCompositeKeyAwareRepository.cs
File metadata and controls
59 lines (50 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using JetBrains.Annotations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Queries;
using JsonApiDotNetCore.Queries.Expressions;
using JsonApiDotNetCore.Repositories;
using JsonApiDotNetCore.Resources;
using Microsoft.Extensions.Logging;
namespace JsonApiDotNetCoreTests.IntegrationTests.CompositeKeys;
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public class CarCompositeKeyAwareRepository<TResource, TId>(
ITargetedFields targetedFields, IDbContextResolver dbContextResolver, IResourceGraph resourceGraph, IResourceFactory resourceFactory,
IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory, IResourceDefinitionAccessor resourceDefinitionAccessor)
: EntityFrameworkCoreRepository<TResource, TId>(targetedFields, dbContextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory,
resourceDefinitionAccessor)
where TResource : class, IIdentifiable<TId>
{
private readonly CarExpressionRewriter _writer = new(resourceGraph);
protected override IQueryable<TResource> ApplyQueryLayer(QueryLayer queryLayer)
{
RecursiveRewriteFilterInLayer(queryLayer);
return base.ApplyQueryLayer(queryLayer);
}
private void RecursiveRewriteFilterInLayer(QueryLayer queryLayer)
{
if (queryLayer.Filter != null)
{
queryLayer.Filter = (FilterExpression?)_writer.Visit(queryLayer.Filter, null);
}
if (queryLayer.Sort != null)
{
queryLayer.Sort = (SortExpression?)_writer.Visit(queryLayer.Sort, null);
}
if (queryLayer.Selection is { IsEmpty: false })
{
// @formatter:wrap_chained_method_calls chop_always
// @formatter:keep_existing_linebreaks true
foreach (QueryLayer? nextLayer in queryLayer.Selection
.GetResourceTypes()
.Select(queryLayer.Selection.GetOrCreateSelectors)
.SelectMany(selectors => selectors
.Select(selector => selector.Value)
.Where(layer => layer != null)))
{
RecursiveRewriteFilterInLayer(nextLayer!);
}
// @formatter:keep_existing_linebreaks restore
// @formatter:wrap_chained_method_calls restore
}
}
}