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

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Bisharyan committed Apr 8, 2024
1 parent db555cf commit 51fd6ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
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
11 changes: 2 additions & 9 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 @@ -33,14 +32,8 @@ public static IQueryable<TModel> ApplyFilters<TModel>(this IQueryable<TModel> db
$"Property {filter.PropertyName} not mapped in {typeof(TModel).Name}");

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>);
// }
if (targetType.IsIEnumerable() && !mappedToPropertyAttribute.Encrypted)
targetType = targetType.GetCollectionType();

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

Expand Down
1 change: 0 additions & 1 deletion 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

0 comments on commit 51fd6ba

Please sign in to comment.