| | 1 | | using Plainquire.Filter.Abstractions; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Diagnostics.CodeAnalysis; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Linq.Expressions; |
| | 7 | |
|
| | 8 | | namespace Plainquire.Filter.ValueFilterExpressions; |
| | 9 | |
|
| | 10 | | /// <inheritdoc cref="INumericFilterExpression"/> |
| | 11 | | [SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Provided as library, can be used from outsid |
| | 12 | | public class NumericFilterExpression : DefaultFilterExpression, INumericFilterExpression |
| | 13 | | { |
| 1 | 14 | | private static readonly List<Type> _primitiveNumberTypes = [typeof(byte), typeof(sbyte), typeof(short), typeof(ushor |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public override ICollection<FilterOperator> SupportedFilterOperators |
| | 18 | | => |
| 4941 | 19 | | [ |
| 4941 | 20 | | FilterOperator.Default, |
| 4941 | 21 | | FilterOperator.Contains, |
| 4941 | 22 | | FilterOperator.StartsWith, |
| 4941 | 23 | | FilterOperator.EndsWith, |
| 4941 | 24 | | FilterOperator.EqualCaseInsensitive, |
| 4941 | 25 | | FilterOperator.EqualCaseSensitive, |
| 4941 | 26 | | FilterOperator.NotEqual, |
| 4941 | 27 | | FilterOperator.LessThan, |
| 4941 | 28 | | FilterOperator.LessThanOrEqual, |
| 4941 | 29 | | FilterOperator.GreaterThan, |
| 4941 | 30 | | FilterOperator.GreaterThanOrEqual, |
| 4941 | 31 | | FilterOperator.IsNull, |
| 4941 | 32 | | FilterOperator.NotNull |
| 4941 | 33 | | ]; |
| | 34 | |
|
| | 35 | | /// <inheritdoc /> |
| | 36 | | public override bool CanCreateExpressionFor(Type type) |
| 9452 | 37 | | => _primitiveNumberTypes.Contains(type.GetUnderlyingType()); |
| | 38 | |
|
| | 39 | | /// <inheritdoc /> |
| | 40 | | protected internal override Expression? CreateExpressionForValue<TEntity, TProperty>(Expression<Func<TEntity, TPrope |
| | 41 | | { |
| 4315 | 42 | | if (decimal.TryParse(value, NumberStyles.Any, new CultureInfo(configuration.CultureName), out var decimalValue)) |
| 3883 | 43 | | return CreateNumberExpressionByFilterOperator(propertySelector, filterOperator, decimalValue); |
| | 44 | |
|
| 432 | 45 | | if (configuration.IgnoreParseExceptions) |
| 0 | 46 | | return null; |
| | 47 | |
|
| 432 | 48 | | throw CreateFilterExpressionCreationException("Unable to parse given filter value", propertySelector, filterOper |
| | 49 | | } |
| | 50 | |
|
| | 51 | | private Expression CreateNumberExpressionByFilterOperator<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> p |
| | 52 | | { |
| 3883 | 53 | | var underlyingFilterPropertyType = typeof(TProperty).GetUnderlyingType(); |
| 3883 | 54 | | var typedValue = (TProperty)Convert.ChangeType(value, underlyingFilterPropertyType, CultureInfo.InvariantCulture |
| | 55 | |
|
| | 56 | | switch (filterOperator) |
| | 57 | | { |
| | 58 | | case FilterOperator.Default: |
| | 59 | | case FilterOperator.EqualCaseInsensitive: |
| | 60 | | case FilterOperator.EqualCaseSensitive: |
| 1051 | 61 | | return CreateEqualExpression(propertySelector, typedValue); |
| | 62 | | case FilterOperator.NotEqual: |
| 288 | 63 | | return CreateNotEqualExpression(propertySelector, typedValue); |
| | 64 | | case FilterOperator.Contains: |
| 384 | 65 | | return CreateNumericContainsExpression(propertySelector, typedValue); |
| | 66 | | case FilterOperator.StartsWith: |
| 720 | 67 | | return CreateStringStartsWithExpression(propertySelector, typedValue); |
| | 68 | | case FilterOperator.EndsWith: |
| 288 | 69 | | return CreateStringEndsWithExpression(propertySelector, typedValue); |
| | 70 | | case FilterOperator.LessThan: |
| 288 | 71 | | return CreateLessThanExpression(propertySelector, typedValue); |
| | 72 | | case FilterOperator.LessThanOrEqual: |
| 288 | 73 | | return CreateLessThanOrEqualExpression(propertySelector, typedValue); |
| | 74 | | case FilterOperator.GreaterThan: |
| 288 | 75 | | return CreateGreaterThanExpression(propertySelector, typedValue); |
| | 76 | | case FilterOperator.GreaterThanOrEqual: |
| 288 | 77 | | return CreateGreaterThanOrEqualExpression(propertySelector, typedValue); |
| | 78 | | default: |
| 0 | 79 | | throw CreateFilterExpressionCreationException($"Filter operator '{filterOperator}' not allowed for prope |
| | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Creates numeric contains expression. |
| | 85 | | /// </summary> |
| | 86 | | /// <typeparam name="TEntity">Type of the entity.</typeparam> |
| | 87 | | /// <typeparam name="TProperty">Type of the property.</typeparam> |
| | 88 | | /// <param name="propertySelector">The property selector.</param> |
| | 89 | | /// <param name="value">The value to check for.</param> |
| | 90 | | /// <returns> |
| | 91 | | /// The new numeric contains expression. |
| | 92 | | /// </returns> |
| | 93 | | public static Expression CreateNumericContainsExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pr |
| | 94 | | { |
| 384 | 95 | | var valueToUpper = Expression.Constant(value.ToString()?.ToUpper(CultureInfo.InvariantCulture), typeof(string)); |
| 384 | 96 | | var propertyToString = propertySelector.Body.ObjectToString(); |
| 384 | 97 | | var propertyToUpper = propertyToString.StringToUpper(); |
| 384 | 98 | | var propertyContainsValue = propertyToUpper.StringContains(valueToUpper); |
| 384 | 99 | | return propertyContainsValue; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// Creates numeric starts-with expression. |
| | 104 | | /// </summary> |
| | 105 | | /// <typeparam name="TEntity">Type of the entity.</typeparam> |
| | 106 | | /// <typeparam name="TProperty">Type of the property.</typeparam> |
| | 107 | | /// <param name="propertySelector">The property selector.</param> |
| | 108 | | /// <param name="value">The value to check for.</param> |
| | 109 | | /// <returns></returns> |
| | 110 | | public static Expression CreateStringStartsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> p |
| | 111 | | { |
| 720 | 112 | | var valueToUpper = Expression.Constant(value.ToString()?.ToUpper(CultureInfo.InvariantCulture), typeof(string)); |
| 720 | 113 | | var propertyToString = propertySelector.Body.ObjectToString(); |
| 720 | 114 | | var propertyToUpper = propertyToString.StringToUpper(); |
| 720 | 115 | | var propertyStartsWithValue = propertyToUpper.StringStartsWith(valueToUpper); |
| 720 | 116 | | return propertyStartsWithValue; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Creates numeric ends-with expression. |
| | 121 | | /// </summary> |
| | 122 | | /// <typeparam name="TEntity">Type of the entity.</typeparam> |
| | 123 | | /// <typeparam name="TProperty">Type of the property.</typeparam> |
| | 124 | | /// <param name="propertySelector">The property selector.</param> |
| | 125 | | /// <param name="value">The value to check for.</param> |
| | 126 | | /// <returns></returns> |
| | 127 | | public static Expression CreateStringEndsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro |
| | 128 | | { |
| 288 | 129 | | var valueToUpper = Expression.Constant(value.ToString()?.ToUpper(CultureInfo.InvariantCulture), typeof(string)); |
| 288 | 130 | | var propertyToString = propertySelector.Body.ObjectToString(); |
| 288 | 131 | | var propertyToUpper = propertyToString.StringToUpper(); |
| 288 | 132 | | var propertyEndsWithValue = propertyToUpper.StringEndsWith(valueToUpper); |
| 288 | 133 | | return propertyEndsWithValue; |
| | 134 | | } |
| | 135 | | } |