Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from PandaTechAM/enum-filter-number-or-string
Browse files Browse the repository at this point in the history
Enum filter both via number and via string
  • Loading branch information
DefGh authored Apr 8, 2024
2 parents b5a7a35 + 51fd6ba commit ee74cdf
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/EFCoreQueryMagic/EFCoreQueryMagic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<Authors>PandaTech</Authors>
<Copyright>MIT</Copyright>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<PackageId>Pandatech.EFCoreQueryMagic</PackageId>
<Title>Pandatech.EFCoreQueryMagic: Dynamic Query and Filter Generator for EF Core</Title>
<Description>Unlock the full potential of your Entity Framework Core applications with Pandatech.EFCoreQueryMagic. This innovative package empowers developers to seamlessly create dynamic, complex queries and filters for SQL tables without diving deep into the intricacies of LINQ or manual query construction. Designed to enhance productivity and maintainability, EFCoreQueryMagic automates the translation of front-end filter requests into optimized, ready-to-execute EF Core queries. Embrace the magic of streamlined data retrieval and manipulation, and elevate your applications to new heights of efficiency and performance.</Description>
Expand Down
58 changes: 26 additions & 32 deletions src/EFCoreQueryMagic/Extensions/DistinctColumnValuesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,9 @@ public static DistinctColumnValues DistinctColumnValues<TModel>(this IQueryable<

var propertyType = PropertyHelper.GetPropertyType(typeof(TModel), mappedToPropertyAttribute);

if (propertyType.EnumCheck())
{
var values = Enum.GetValues(GetEnumerableType(propertyType)).Cast<object>()
.Where(x => !(x as Enum)!.HasAttributeOfType<HideEnumValueAttribute>());

var list = values.ToList();
result.Values = list.Paginate(pageSize, page);
result.TotalCount = list.Count;
return result;
}

var query = GenerateBaseQueryable(dbSet, filters, context);
IQueryable<object> query2;

// check for ICollection<>

var property = PropertyHelper.GetPropertyLambda(mappedToPropertyAttribute);

if (propertyType.IsIEnumerable() && !mappedToPropertyAttribute.Encrypted)
Expand All @@ -96,16 +83,26 @@ public static DistinctColumnValues DistinctColumnValues<TModel>(this IQueryable<
: Activator.CreateInstance(mappedToPropertyAttribute.ConverterType ?? typeof(DirectConverter))) as
IConverter;

converter.Context = context;
converter!.Context = context;

var method = converter.GetType().GetMethods().First(x => x.Name == "ConvertFrom");

var query3 = query2.Distinct();


if (propertyType.EnumCheck())
{
var excludedValues = Enum.GetValues(GetEnumerableType(propertyType)).Cast<object>()
.Where(x => (x as Enum)!.HasAttributeOfType<HideEnumValueAttribute>())
.ToList();

if (excludedValues.Count != 0)
query3 = query3.Where(x => !excludedValues.Contains(x));
}

result.Values = query3.Skip(pageSize * (page - 1)).Take(pageSize)
.ToList()
.Select(x => method.Invoke(converter, [x])!).Distinct().OrderBy(x => x).ToList();

try
{
result.TotalCount = mappedToPropertyAttribute.Encrypted ? 1 : query3.LongCount();
Expand Down Expand Up @@ -139,22 +136,9 @@ public static async Task<DistinctColumnValues> DistinctColumnValuesAsync<TModel>

var propertyType = PropertyHelper.GetPropertyType(typeof(TModel), mappedToPropertyAttribute);

if (propertyType.EnumCheck())
{
var values = Enum.GetValues(GetEnumerableType(propertyType)).Cast<object>()
.Where(x => !(x as Enum)!.HasAttributeOfType<HideEnumValueAttribute>());

var list = values.ToList();
result.Values = list.Paginate(pageSize, page);
result.TotalCount = list.Count;
return result;
}

var query = GenerateBaseQueryable(dbSet, filters, context);
IQueryable<object> query2;

// check for ICollection<>

var property = PropertyHelper.GetPropertyLambda(mappedToPropertyAttribute);

if (propertyType.IsIEnumerable() && !mappedToPropertyAttribute.Encrypted)
Expand All @@ -171,16 +155,26 @@ public static async Task<DistinctColumnValues> DistinctColumnValuesAsync<TModel>
: Activator.CreateInstance(mappedToPropertyAttribute.ConverterType ?? typeof(DirectConverter))) as
IConverter;

converter.Context = context;
converter!.Context = context;

var method = converter.GetType().GetMethods().First(x => x.Name == "ConvertFrom");

var query3 = query2.Distinct();


if (propertyType.EnumCheck())
{
var excludedValues = Enum.GetValues(GetEnumerableType(propertyType)).Cast<object>()
.Where(x => (x as Enum)!.HasAttributeOfType<HideEnumValueAttribute>())
.ToList();

if (excludedValues.Count != 0)
query3 = query3.Where(x => !excludedValues.Contains(x));
}

result.Values = (await query3.Skip(pageSize * (page - 1)).Take(pageSize)
.ToListAsync(cancellationToken: cancellationToken))
.Select(x => method.Invoke(converter, [x])!).Distinct().OrderBy(x => x).ToList();

try
{
result.TotalCount = mappedToPropertyAttribute.Encrypted
Expand Down
7 changes: 0 additions & 7 deletions src/EFCoreQueryMagic/Extensions/FilterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace EFCoreQueryMagic.Extensions;

public static class FilterExtensions
{
//public static IQueryable<TModel> ApplyFilters<TModel, TDto>(this IQueryable<TModel> dbSet, List<FilterDto> filters)
public static IQueryable<TModel> ApplyFilters<TModel>(this IQueryable<TModel> dbSet, List<FilterDto> filters,
DbContext? context = null)
{
Expand All @@ -35,12 +34,6 @@ public static IQueryable<TModel> ApplyFilters<TModel>(this IQueryable<TModel> db
var targetType = PropertyHelper.GetPropertyType(typeof(TModel), mappedToPropertyAttribute);
if (targetType.IsIEnumerable() && !mappedToPropertyAttribute.Encrypted)
targetType = targetType.GetCollectionType();

// var nullabilityContext = new NullabilityInfoContext();
// if (nullabilityContext.Create(filterProperty).ReadState == NullabilityState.Nullable)
// {
// Convert.ChangeType(targetType, Nullable<string>);
// }

var method = typeof(PropertyHelper).GetMethod("GetValues")!.MakeGenericMethod(targetType);

Expand Down
8 changes: 6 additions & 2 deletions src/EFCoreQueryMagic/Helpers/PropertyHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using EFCoreQueryMagic.Attributes;
using EFCoreQueryMagic.Converters;
Expand Down Expand Up @@ -69,7 +68,12 @@ public static List<T> GetValues<T>(this FilterDto filter, MappedToPropertyAttrib
}

if (typeof(T).EnumCheck())
return (T)Enum.Parse(typeof(T).GetEnumType(), val.GetString()!, true);
{
if (val.ValueKind == JsonValueKind.String)
return (T)Enum.Parse(typeof(T).GetEnumType(), val.GetString()!, true);

return (T)Enum.ToObject(typeof(T), val.GetInt32());
}

var type = attribute.Encrypted ? typeof(string) : typeof(T);

Expand Down
126 changes: 120 additions & 6 deletions test/EFCoreQueryMagic.Test/DistinctTests/Enum/EnumNullableTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EFCoreQueryMagic.Dto;
using EFCoreQueryMagic.Enums;
using EFCoreQueryMagic.Extensions;
using EFCoreQueryMagic.Test.EntityFilters;
using EFCoreQueryMagic.Test.Enums;
Expand All @@ -17,9 +18,9 @@ public void TestDistinctColumnValuesAsync()
{
var set = _context.Orders;

var query = System.Enum.GetValues<CancellationStatus>()
.Select(x => x as object)
.OrderBy(x => x)
var query = set
.Select(x => x.CancellationStatus as object)
.OrderBy(x => (int)x)
.ToList();

var qString = new GetDataRequest();
Expand All @@ -29,14 +30,71 @@ public void TestDistinctColumnValuesAsync()
query.Should().Equal(result.Values);
}

[Fact]
public void TestDistinctColumnValuesAsync_String()
{
var set = _context.Orders;

var query = set
.Where(x => x.CancellationStatus == CancellationStatus.Yes)
.Select(x => x.CancellationStatus as object)
.ToList();

var qString = GetDataRequest.FromString(new GetDataRequest
{
Filters =
[
new FilterDto
{
Values = [CancellationStatus.Yes.ToString()],
ComparisonType = ComparisonType.Equal,
PropertyName = nameof(OrderFilter.CancellationStatus)
}
]
}.ToString());

var result = set.DistinctColumnValues(qString.Filters, nameof(OrderFilter.CancellationStatus), 20, 1);

query.Should().Equal(result.Values);
}

[Fact]
public void TestDistinctColumnValuesAsync_Number()
{
var set = _context.Orders;

var query = set
.Where(x => x.CancellationStatus == CancellationStatus.Yes)
.Select(x => x.CancellationStatus as object)
.ToList();

var qString = GetDataRequest.FromString(new GetDataRequest
{
Filters =
[
new FilterDto
{
Values = [(int)CancellationStatus.Yes],
ComparisonType = ComparisonType.Equal,
PropertyName = nameof(OrderFilter.CancellationStatus)
}
]
}.ToString());

var result = set.DistinctColumnValues(qString.Filters, nameof(OrderFilter.CancellationStatus), 20, 1);

query.Should().Equal(result.Values);
}


[Fact]
public void TestDistinctColumnValues()
{
var set = _context.Orders;

var query = System.Enum.GetValues<CancellationStatus>()
.Select(x => x as object)
.OrderBy(x => x)
var query = set
.Select(x => x.CancellationStatus as object)
.OrderBy(x => (int)x)
.ToList();

var qString = new GetDataRequest();
Expand All @@ -45,4 +103,60 @@ public void TestDistinctColumnValues()

query.Should().Equal(result.Values);
}

[Fact]
public void TestDistinctColumnValues_String()
{
var set = _context.Orders;

var query = set
.Where(x => x.CancellationStatus == CancellationStatus.Yes)
.Select(x => x.CancellationStatus as object)
.ToList();

var qString = GetDataRequest.FromString(new GetDataRequest
{
Filters =
[
new FilterDto
{
Values = [CancellationStatus.Yes.ToString()],
ComparisonType = ComparisonType.Equal,
PropertyName = nameof(OrderFilter.CancellationStatus)
}
]
}.ToString());

var result = set.DistinctColumnValues(qString.Filters, nameof(OrderFilter.CancellationStatus), 20, 1);

query.Should().Equal(result.Values);
}

[Fact]
public void TestDistinctColumnValues_Number()
{
var set = _context.Orders;

var query = set
.Where(x => x.CancellationStatus == CancellationStatus.Yes)
.Select(x => x.CancellationStatus as object)
.ToList();

var qString = GetDataRequest.FromString(new GetDataRequest
{
Filters =
[
new FilterDto
{
Values = [(int)CancellationStatus.Yes],
ComparisonType = ComparisonType.Equal,
PropertyName = nameof(OrderFilter.CancellationStatus)
}
]
}.ToString());

var result = set.DistinctColumnValues(qString.Filters, nameof(OrderFilter.CancellationStatus), 20, 1);

query.Should().Equal(result.Values);
}
}
Loading

0 comments on commit ee74cdf

Please sign in to comment.