| | 1 | | using Fluorite.Strainer.Exceptions; |
| | 2 | | using Fluorite.Strainer.Models.Filtering.Operators; |
| | 3 | |
|
| | 4 | | namespace Fluorite.Strainer.Services.Filtering.Steps; |
| | 5 | |
|
| | 6 | | public class ApplyFilterOperatorStep : IApplyFilterOperatorStep |
| | 7 | | { |
| | 8 | | private readonly IStrainerOptionsProvider _strainerOptionsProvider; |
| | 9 | |
|
| 3 | 10 | | public ApplyFilterOperatorStep(IStrainerOptionsProvider strainerOptionsProvider) |
| | 11 | | { |
| 3 | 12 | | _strainerOptionsProvider = Guard.Against.Null(strainerOptionsProvider); |
| 3 | 13 | | } |
| | 14 | |
|
| | 15 | | public void Execute(FilterExpressionWorkflowContext context) |
| | 16 | | { |
| 2 | 17 | | Guard.Against.Null(context); |
| 2 | 18 | | Guard.Against.Null(context.FinalExpression); |
| 2 | 19 | | Guard.Against.Null(context.PropertyValue); |
| 2 | 20 | | Guard.Against.Null(context.Term); |
| 2 | 21 | | Guard.Against.Null(context.Term.Operator); |
| 2 | 22 | | Guard.Against.Null(context.PropertyMetadata); |
| 2 | 23 | | Guard.Against.Null(context.PropertyMetadata.PropertyInfo); |
| 2 | 24 | | Guard.Against.Null(context.FilterTermValue); |
| | 25 | |
|
| 2 | 26 | | var strainerOptions = _strainerOptionsProvider.GetStrainerOptions(); |
| 2 | 27 | | var isStringBasedProperty = context.PropertyMetadata.PropertyInfo.PropertyType == typeof(string); |
| 2 | 28 | | var filterOperatorContext = new FilterExpressionContext( |
| 2 | 29 | | context.FinalExpression, |
| 2 | 30 | | context.PropertyValue, |
| 2 | 31 | | strainerOptions.IsCaseInsensitiveForValues, |
| 2 | 32 | | context.IsMaterializedQueryable, |
| 2 | 33 | | isStringBasedProperty); |
| | 34 | |
|
| | 35 | | try |
| | 36 | | { |
| 2 | 37 | | context.FinalExpression = context.Term.Operator.ExpressionProvider(filterOperatorContext); |
| 1 | 38 | | } |
| 1 | 39 | | catch (Exception ex) |
| | 40 | | { |
| 1 | 41 | | var metadata = context.PropertyMetadata; |
| | 42 | |
|
| 1 | 43 | | throw new StrainerOperatorException( |
| 1 | 44 | | $"Failed to use operator '{context.Term.Operator}' " + |
| 1 | 45 | | $"for filter value '{context.FilterTermValue}' on property " + |
| 1 | 46 | | $"'{metadata.PropertyInfo.DeclaringType.FullName}.{metadata.PropertyInfo.Name}' " + |
| 1 | 47 | | $"of type '{metadata.PropertyInfo.PropertyType.FullName}'\n." + |
| 1 | 48 | | "Please ensure that this operator is supported by type " + |
| 1 | 49 | | $"'{metadata.PropertyInfo.PropertyType.FullName}'.", |
| 1 | 50 | | ex, |
| 1 | 51 | | context.Term.Operator, |
| 1 | 52 | | metadata.PropertyInfo, |
| 1 | 53 | | context.FilterTermValue); |
| | 54 | | } |
| 1 | 55 | | } |
| | 56 | | } |