| | 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="IGuidFilterExpression"/> |
| | 11 | | [SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Provided as library, can be used from outsid |
| | 12 | | public class GuidFilterExpression : DefaultFilterExpression, IGuidFilterExpression |
| | 13 | | { |
| | 14 | | /// <inheritdoc /> |
| | 15 | | public override ICollection<FilterOperator> SupportedFilterOperators |
| | 16 | | => |
| 936 | 17 | | [ |
| 936 | 18 | | FilterOperator.Default, |
| 936 | 19 | | FilterOperator.Contains, |
| 936 | 20 | | FilterOperator.StartsWith, |
| 936 | 21 | | FilterOperator.EndsWith, |
| 936 | 22 | | FilterOperator.EqualCaseSensitive, |
| 936 | 23 | | FilterOperator.EqualCaseInsensitive, |
| 936 | 24 | | FilterOperator.NotEqual, |
| 936 | 25 | | FilterOperator.IsNull, |
| 936 | 26 | | FilterOperator.NotNull |
| 936 | 27 | | ]; |
| | 28 | |
|
| | 29 | | /// <inheritdoc /> |
| | 30 | | public override bool CanCreateExpressionFor(Type type) |
| 20167 | 31 | | => type.GetUnderlyingType() == typeof(Guid); |
| | 32 | |
|
| | 33 | | /// <inheritdoc /> |
| | 34 | | protected internal override Expression? CreateExpressionForValue<TEntity, TProperty>(Expression<Func<TEntity, TPrope |
| | 35 | | { |
| 552 | 36 | | if (Guid.TryParse(value, out var guidValue)) |
| 360 | 37 | | return CreateGuidExpressionByFilterOperator(propertySelector, filterOperator, guidValue); |
| 192 | 38 | | if (filterOperator == FilterOperator.Contains) |
| 48 | 39 | | return CreateGuidContainsExpression(propertySelector, value); |
| 144 | 40 | | if (filterOperator == FilterOperator.StartsWith) |
| 24 | 41 | | return CreateGuidStartsWithExpression(propertySelector, value); |
| 120 | 42 | | if (filterOperator == FilterOperator.EndsWith) |
| 24 | 43 | | return CreateGuidEndsWithExpression(propertySelector, value); |
| | 44 | |
|
| 96 | 45 | | if (configuration.IgnoreParseExceptions) |
| 0 | 46 | | return null; |
| | 47 | |
|
| 96 | 48 | | throw CreateFilterExpressionCreationException("Unable to parse given filter value", propertySelector, filterOper |
| | 49 | | } |
| | 50 | |
|
| | 51 | | private Expression CreateGuidExpressionByFilterOperator<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro |
| | 52 | | { |
| | 53 | | switch (filterOperator) |
| | 54 | | { |
| | 55 | | case FilterOperator.Default: |
| | 56 | | case FilterOperator.StartsWith: |
| | 57 | | case FilterOperator.EndsWith: |
| | 58 | | case FilterOperator.EqualCaseSensitive: |
| | 59 | | case FilterOperator.EqualCaseInsensitive: |
| 264 | 60 | | return CreateEqualExpression(propertySelector, value); |
| | 61 | | case FilterOperator.NotEqual: |
| 72 | 62 | | return CreateNotEqualExpression(propertySelector, value); |
| | 63 | | case FilterOperator.Contains: |
| 24 | 64 | | return CreateGuidContainsExpression(propertySelector, value); |
| | 65 | | default: |
| 0 | 66 | | throw CreateFilterExpressionCreationException($"Filter operator '{filterOperator}' not allowed for prope |
| | 67 | | } |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Creates unique identifier contains expression. |
| | 72 | | /// </summary> |
| | 73 | | /// <typeparam name="TEntity">Type of the entity.</typeparam> |
| | 74 | | /// <typeparam name="TProperty">Type of the property.</typeparam> |
| | 75 | | /// <param name="propertySelector">The property selector.</param> |
| | 76 | | /// <param name="value">The value to check for.</param> |
| | 77 | | /// <returns> |
| | 78 | | /// The new unique identifier contains expression. |
| | 79 | | /// </returns> |
| | 80 | | public static Expression CreateGuidContainsExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> prope |
| | 81 | | { |
| 72 | 82 | | var valueToUpper = Expression.Constant(value?.ToString()?.ToUpper(CultureInfo.InvariantCulture), typeof(string)) |
| 72 | 83 | | var propertyToString = propertySelector.Body.ObjectToString(); |
| 72 | 84 | | var propertyToUpper = propertyToString.StringToUpper(); |
| 72 | 85 | | var propertyContainsValue = propertyToUpper.StringContains(valueToUpper); |
| 72 | 86 | | return propertyContainsValue; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Creates unique identifier starts with expression. |
| | 91 | | /// </summary> |
| | 92 | | /// <typeparam name="TEntity">Type of the entity.</typeparam> |
| | 93 | | /// <typeparam name="TProperty">Type of the property.</typeparam> |
| | 94 | | /// <param name="propertySelector">The property selector.</param> |
| | 95 | | /// <param name="value">The value to check for.</param> |
| | 96 | | public static Expression CreateGuidStartsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> pro |
| | 97 | | { |
| 24 | 98 | | var valueToUpper = Expression.Constant(value?.ToString()?.ToUpper(CultureInfo.InvariantCulture), typeof(string)) |
| 24 | 99 | | var propertyToString = propertySelector.Body.ObjectToString(); |
| 24 | 100 | | var propertyToUpper = propertyToString.StringToUpper(); |
| 24 | 101 | | var propertyStartsWithValue = propertyToUpper.StringStartsWith(valueToUpper); |
| 24 | 102 | | return propertyStartsWithValue; |
| | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Creates unique identifier ends with expression. |
| | 107 | | /// </summary> |
| | 108 | | /// <typeparam name="TEntity">Type of the entity.</typeparam> |
| | 109 | | /// <typeparam name="TProperty">Type of the property.</typeparam> |
| | 110 | | /// <param name="propertySelector">The property selector.</param> |
| | 111 | | /// <param name="value">The value to check for.</param> |
| | 112 | | public static Expression CreateGuidEndsWithExpression<TEntity, TProperty>(Expression<Func<TEntity, TProperty>> prope |
| | 113 | | { |
| 24 | 114 | | var valueToUpper = Expression.Constant(value?.ToString()?.ToUpper(CultureInfo.InvariantCulture), typeof(string)) |
| 24 | 115 | | var propertyToString = propertySelector.Body.ObjectToString(); |
| 24 | 116 | | var propertyToUpper = propertyToString.StringToUpper(); |
| 24 | 117 | | var propertyEndsWithValue = propertyToUpper.StringEndsWith(valueToUpper); |
| 24 | 118 | | return propertyEndsWithValue; |
| | 119 | | } |
| | 120 | | } |