diff --git a/IEnumerableExtenders/Attributes/MappedToPropertyAttribute.cs b/IEnumerableExtenders/Attributes/MappedToPropertyAttribute.cs index 4585779..a3a5eb1 100644 --- a/IEnumerableExtenders/Attributes/MappedToPropertyAttribute.cs +++ b/IEnumerableExtenders/Attributes/MappedToPropertyAttribute.cs @@ -6,33 +6,15 @@ namespace PandaTech.IEnumerableFilters.Attributes; [AttributeUsage(AttributeTargets.Property)] public class MappedToPropertyAttribute : Attribute { - public Type? TargetConverterType; - public Type? BackwardConverterType; + public Type? ConverterType; public readonly string TargetPropertyName; public ComparisonType[]? ComparisonTypes; - public readonly bool Sortable = true; + public bool Encrypted = false; + public bool Sortable = true; public MappedToPropertyAttribute(string property) { TargetPropertyName = property; } - - public ComparisonTypesDefault ComparisonTypesDefault - { - get => throw new Exception("This property is only for serialization"); - set - { - ComparisonTypes = value switch - { - ComparisonTypesDefault.Numeric => DefaultComparisonTypes.Numeric, - ComparisonTypesDefault.String => DefaultComparisonTypes.String, - ComparisonTypesDefault.DateTime => DefaultComparisonTypes.DateTime, - ComparisonTypesDefault.Bool => DefaultComparisonTypes.Bool, - ComparisonTypesDefault.Guid => DefaultComparisonTypes.Guid, - ComparisonTypesDefault.Enum => DefaultComparisonTypes.Enum, - _ => throw new ArgumentOutOfRangeException(nameof(value), value, null) - }; - } - } } \ No newline at end of file diff --git a/IEnumerableExtenders/EnumerableExtendersV3.cs b/IEnumerableExtenders/EnumerableExtendersV3.cs index c0d8bc4..94ff903 100644 --- a/IEnumerableExtenders/EnumerableExtendersV3.cs +++ b/IEnumerableExtenders/EnumerableExtendersV3.cs @@ -12,6 +12,39 @@ namespace PandaTech.IEnumerableFilters; public static class EnumerableExtendersV3 { + private static ComparisonType[]? ComparisonTypes(ComparisonTypesDefault typesDefault) => + typesDefault switch + { + ComparisonTypesDefault.Numeric => DefaultComparisonTypes.Numeric, + ComparisonTypesDefault.String => DefaultComparisonTypes.String, + ComparisonTypesDefault.DateTime => DefaultComparisonTypes.DateTime, + ComparisonTypesDefault.Bool => DefaultComparisonTypes.Bool, + ComparisonTypesDefault.Guid => DefaultComparisonTypes.Guid, + ComparisonTypesDefault.Enum => DefaultComparisonTypes.Enum, + ComparisonTypesDefault.ByteArray => DefaultComparisonTypes.ByteArray, + _ => null + }; + + private static ComparisonTypesDefault GetComparisonTypesDefault(Type type) + { + if (type == typeof(int) || type == typeof(long) || type == typeof(decimal) || type == typeof(double) || + type == typeof(float)) + return ComparisonTypesDefault.Numeric; + if (type == typeof(string)) + return ComparisonTypesDefault.String; + if (type == typeof(DateTime)) + return ComparisonTypesDefault.DateTime; + if (type == typeof(bool)) + return ComparisonTypesDefault.Bool; + if (type == typeof(Guid)) + return ComparisonTypesDefault.Guid; + if (type.IsEnum) + return ComparisonTypesDefault.Enum; + if (type == typeof(byte[])) + return ComparisonTypesDefault.ByteArray; + throw new ArgumentOutOfRangeException(nameof(type), type, null); + } + public static IQueryable ApplyFilters(this IQueryable dbSet, List filters) { var q = dbSet; @@ -47,8 +80,8 @@ public static IQueryable ApplyFilters(this IQueryable ApplyFilters(this IQueryable ApplyFilters(this IQueryable x.Name == "Convert"); + var method = converter!.GetType().GetMethods().First(x => x.Name == "ConvertTo"); for (var index = 0; index < filterDto.Values.Count; index++) { @@ -212,8 +245,8 @@ public static DistinctColumnValuesResult DistinctColumnValues(this IQue query2 = query.Select(filter.Attribute.TargetPropertyName); } - var converter = Activator.CreateInstance(filter.Attribute.BackwardConverterType ?? typeof(DirectConverter)); - var method = converter!.GetType().GetMethods().First(x => x.Name == "Convert"); + var converter = Activator.CreateInstance(filter.Attribute.ConverterType ?? typeof(DirectConverter)); + var method = converter!.GetType().GetMethods().First(x => x.Name == "ConvertFrom"); IQueryable query3; try @@ -285,8 +318,8 @@ public static async Task DistinctColumnValuesAsync(filter.Attribute.TargetPropertyName); } - var converter = Activator.CreateInstance(filter.Attribute.BackwardConverterType ?? typeof(DirectConverter)); - var method = converter!.GetType().GetMethods().First(x => x.Name == "Convert"); + var converter = Activator.CreateInstance(filter.Attribute.ConverterType ?? typeof(DirectConverter)); + var method = converter!.GetType().GetMethods().First(x => x.Name == "ConvertFrom"); IQueryable query3; try @@ -337,7 +370,9 @@ public static List GetFilters(Assembly assembly, string tableName) { PropertyName = x.Name, Table = tableName, - ComparisonTypes = (x.GetCustomAttribute()!.ComparisonTypes ?? new[] + ComparisonTypes = (x.GetCustomAttribute()!.ComparisonTypes ?? + ComparisonTypes(GetComparisonTypesDefault(x.PropertyType)) + ?? new[] { ComparisonType.Equal, ComparisonType.NotEqual, @@ -426,7 +461,7 @@ public static List GetFilters(Assembly assembly, string tableName) continue; } - if (property.PropertyType == typeof(int)) + if (targetProperty.PropertyType == typeof(int)) { var lambda = Lambda>(propertyAccess, parameter); @@ -484,7 +519,7 @@ public static List GetFilters(Assembly assembly, string tableName) continue; } - if (property.PropertyType == typeof(long)) + if (targetProperty.PropertyType == typeof(long)) { var lambda = Lambda>(propertyAccess, parameter); @@ -542,7 +577,7 @@ public static List GetFilters(Assembly assembly, string tableName) continue; } - if (property.PropertyType == typeof(DateTime)) + if (targetProperty.PropertyType == typeof(DateTime)) { var lambda = Lambda>(propertyAccess, parameter); var res = aggregate.AggregateType switch @@ -571,7 +606,7 @@ public static List GetFilters(Assembly assembly, string tableName) continue; } - if (property.PropertyType == typeof(decimal)) + if (targetProperty.PropertyType == typeof(decimal)) { var lambda = Lambda>(propertyAccess, parameter); @@ -630,7 +665,7 @@ public static List GetFilters(Assembly assembly, string tableName) continue; } - if (property.PropertyType == typeof(double)) + if (targetProperty.PropertyType == typeof(double)) { var lambda = Lambda>(propertyAccess, parameter); @@ -689,12 +724,12 @@ public static List GetFilters(Assembly assembly, string tableName) continue; } - if (property.PropertyType == typeof(Guid)) + if (targetProperty.PropertyType == typeof(Guid)) { throw new NotImplementedException(); } - if (property.PropertyType.IsClass) + if (targetProperty.PropertyType.IsClass) { throw new NotImplementedException(); } diff --git a/IEnumerableExtenders/FilterProvider.cs b/IEnumerableExtenders/FilterProvider.cs index bd3c532..7cca9e9 100644 --- a/IEnumerableExtenders/FilterProvider.cs +++ b/IEnumerableExtenders/FilterProvider.cs @@ -6,10 +6,15 @@ namespace PandaTech.IEnumerableFilters; public class DirectConverter : IConverter { - public object Convert(object from) + public object ConvertTo(object from) { return from; } + + public object ConvertFrom(object to) + { + return to; + } } public static class FilterLambdaBuilder diff --git a/IEnumerableExtenders/Helpers/ComparisonTypeHelper.cs b/IEnumerableExtenders/Helpers/ComparisonTypeHelper.cs index 40ac114..7d7293d 100644 --- a/IEnumerableExtenders/Helpers/ComparisonTypeHelper.cs +++ b/IEnumerableExtenders/Helpers/ComparisonTypeHelper.cs @@ -9,11 +9,28 @@ public enum ComparisonTypesDefault DateTime, Bool, Guid, - Enum + Enum, + ByteArray } public static class DefaultComparisonTypes { + public static readonly ComparisonType[] Default = + { + ComparisonType.In, + ComparisonType.NotIn, + ComparisonType.Equal, + ComparisonType.NotEqual, + }; + + public static readonly ComparisonType[] ByteArray = + { + ComparisonType.In, + ComparisonType.NotIn, + ComparisonType.Equal, + ComparisonType.NotEqual, + }; + public static readonly ComparisonType[] Numeric = { ComparisonType.In, diff --git a/IEnumerableExtenders/IConverter.cs b/IEnumerableExtenders/IConverter.cs index c8112ff..f9d58a7 100644 --- a/IEnumerableExtenders/IConverter.cs +++ b/IEnumerableExtenders/IConverter.cs @@ -1,6 +1,8 @@ namespace PandaTech.IEnumerableFilters; -public interface IConverter +public interface IConverter { - public TTo Convert(TFrom from); + public TTo ConvertTo(TFrom from); + + public TFrom ConvertFrom(TTo to); } \ No newline at end of file diff --git a/IEnumerableExtenders/IEnumerableExtenders.csproj b/IEnumerableExtenders/IEnumerableExtenders.csproj index cdac94d..883ecba 100644 --- a/IEnumerableExtenders/IEnumerableExtenders.csproj +++ b/IEnumerableExtenders/IEnumerableExtenders.csproj @@ -5,7 +5,7 @@ enable enable PandaTech.IEnumerableFilters - 3.0.9 + 3.1.0 PandaTech This NuGet helps with filtering tables. https://github.com/PandaTechAM/be-lib-ienumerable-extenders-filters.git diff --git a/TestFilters/Controllers/Models/CatDto.cs b/TestFilters/Controllers/Models/CatDto.cs index c55923a..c76e1cb 100644 --- a/TestFilters/Controllers/Models/CatDto.cs +++ b/TestFilters/Controllers/Models/CatDto.cs @@ -11,13 +11,13 @@ public class CatDto { [JsonConverter(typeof(PandaJsonBaseConverterNotNullable))] [MappedToProperty(nameof(Cat.Id), - TargetConverterType = typeof(PandaFilterBaseConverter))] + ConverterType = typeof(PandaFilterBaseConverter))] public long Id { get; set; } [MappedToProperty(nameof(Cat.Name))] public string Name { get; set; } = null!; - [MappedToProperty(nameof(Cat.Age), ComparisonTypesDefault = ComparisonTypesDefault.Numeric)] + [MappedToProperty(nameof(Cat.Age))] public int Age { get; set; } } @@ -27,5 +27,15 @@ public long Convert(string from) { return PandaBaseConverter.Base36ToBase10(from)!.Value; } + + public long ConvertTo(string from) + { + return PandaBaseConverter.Base36ToBase10(from)!.Value; + } + + public string ConvertFrom(long to) + { + return PandaBaseConverter.Base10ToBase36(to)!; + } } diff --git a/Tests/CounterpartyDto.cs b/Tests/CounterpartyDto.cs index 3ae0051..58c53b9 100644 --- a/Tests/CounterpartyDto.cs +++ b/Tests/CounterpartyDto.cs @@ -5,7 +5,7 @@ namespace Tests; [MappedToClass(typeof(Counterparty))] public class CounterpartyDto { - [MappedToProperty(nameof(Counterparty.Id), TargetConverterType = typeof(SomeConverter))] + [MappedToProperty(nameof(Counterparty.Id), ConverterType = typeof(SomeConverter))] public string Id { get; set; } [MappedToProperty(nameof(Counterparty.Name))] diff --git a/Tests/SomeConverter.cs b/Tests/SomeConverter.cs index 748d754..21a672c 100644 --- a/Tests/SomeConverter.cs +++ b/Tests/SomeConverter.cs @@ -8,4 +8,14 @@ public int Convert(string from) { return int.Parse(from); } + + public int ConvertTo(string from) + { + return int.Parse(from); + } + + public string ConvertFrom(int to) + { + return to.ToString(); + } } \ No newline at end of file