| | 1 | | using System.Linq.Expressions; |
| | 2 | |
|
| | 3 | | namespace Fluorite.Strainer.Services.Filtering.Steps; |
| | 4 | |
|
| | 5 | | public class MitigateCaseInsensitivityStep : IMitigateCaseInsensitivityStep |
| | 6 | | { |
| | 7 | | private readonly IStrainerOptionsProvider _strainerOptionsProvider; |
| | 8 | |
|
| 5 | 9 | | public MitigateCaseInsensitivityStep(IStrainerOptionsProvider strainerOptionsProvider) |
| | 10 | | { |
| 5 | 11 | | _strainerOptionsProvider = Guard.Against.Null(strainerOptionsProvider); |
| 5 | 12 | | } |
| | 13 | |
|
| | 14 | | public void Execute(FilterExpressionWorkflowContext context) |
| | 15 | | { |
| 4 | 16 | | Guard.Against.Null(context); |
| 4 | 17 | | Guard.Against.Null(context.Term); |
| 4 | 18 | | Guard.Against.Null(context.Term.Operator); |
| 4 | 19 | | Guard.Against.Null(context.PropertyMetadata); |
| 4 | 20 | | Guard.Against.Null(context.PropertyMetadata.PropertyInfo); |
| | 21 | |
|
| 4 | 22 | | if (context.IsMaterializedQueryable) |
| | 23 | | { |
| 1 | 24 | | return; |
| | 25 | | } |
| | 26 | |
|
| 3 | 27 | | var options = _strainerOptionsProvider.GetStrainerOptions(); |
| | 28 | |
|
| 3 | 29 | | if ((context.Term.Operator.IsCaseInsensitive |
| 3 | 30 | | || (!context.Term.Operator.IsCaseInsensitive && options.IsCaseInsensitiveForValues)) |
| 3 | 31 | | && context.PropertyMetadata.PropertyInfo.PropertyType == typeof(string)) |
| | 32 | | { |
| 2 | 33 | | context.PropertyValue = Expression.Call( |
| 2 | 34 | | context.PropertyValue, |
| 2 | 35 | | typeof(string).GetMethod(nameof(string.ToUpper), Array.Empty<Type>())); |
| | 36 | |
|
| 2 | 37 | | context.FinalExpression = Expression.Call( |
| 2 | 38 | | context.FinalExpression, |
| 2 | 39 | | typeof(string).GetMethod(nameof(string.ToUpper), Array.Empty<Type>())); |
| | 40 | | } |
| 3 | 41 | | } |
| | 42 | | } |