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

Commit

Permalink
3.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
DefGh committed Oct 27, 2023
1 parent 23da976 commit f118b5b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
88 changes: 64 additions & 24 deletions IEnumerableExtenders/EnumerableExtendersV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ public static IQueryable<TModel> ApplyFilters<TModel, TDto>(this IQueryable<TMod
filterDto.Values[index] = method.Invoke(converter, new[] { filterDto.Values[index] }) ??
throw new MappingException("Converter returned null");
}

var typedList = Activator.CreateInstance(typeof(List<>).MakeGenericType(filterType));

var addMethod = typedList!.GetType().GetMethod("AddRange");
addMethod!.Invoke(typedList, new[] { filterDto.Values });


var addMethod = typedList!.GetType().GetMethod("Add");
foreach (var value in filterDto.Values)
{
addMethod!.Invoke(typedList, new[] { value });
}

q = filterDto.ComparisonType switch
{
Expand Down Expand Up @@ -154,10 +156,6 @@ public static IQueryable<TModel> ApplyOrdering<TModel, TDto>(this IEnumerable<TM
: dbSet.AsQueryable().OrderBy(filter.TargetPropertyName + " DESC");
}

private struct FilteringInfo
{
}

public static List<object> DistinctColumnValues<TModel, TDto>(this IQueryable<TModel> dbSet,
List<FilterDto> filters,
string columnName, int pageSize, int page, out long totalCount) where TModel : class
Expand All @@ -168,7 +166,6 @@ public static List<object> DistinctColumnValues<TModel, TDto>(this IQueryable<TM
return result.Values;
}


public static DistinctColumnValuesResult DistinctColumnValues<T, TDto>(this IQueryable<T> dbSet,
List<FilterDto> filters,
string columnName, int pageSize, int page) where T : class
Expand Down Expand Up @@ -312,6 +309,49 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
}
}

public static List<string> GetTables(Assembly assembly)
{
return assembly.GetTypes()
.Where(x => x.CustomAttributes.Any(xx => xx.AttributeType == typeof(MappedToClassAttribute)))
.Select(x => x.Name)
.ToList();
}

public static List<string> GetTables() => GetTables(Assembly.GetCallingAssembly());

public static List<FilterInfo> GetFilters(Assembly assembly, string tableName)
{
var type = assembly.GetTypes()
.FirstOrDefault(x => x.Name == tableName &&
x.CustomAttributes.Any(attr =>
attr.AttributeType ==
typeof(MappedToClassAttribute)));

if (type is null)
throw new MappingException("Table not found");

var properties = type.GetProperties().Where(x => x.CustomAttributes.Any(attr =>
attr.AttributeType == typeof(MappedToPropertyAttribute))).ToList();
var infos = properties.Select(
x => new FilterInfo()
{
PropertyName = x.Name,
Table = tableName,
ComparisonTypes = (x.GetCustomAttribute<MappedToPropertyAttribute>()!.ComparisonTypes ?? new[]
{
ComparisonType.Equal,
ComparisonType.NotEqual,
ComparisonType.In,
ComparisonType.NotIn
}).ToList()
}
);

return infos.ToList();
}

public static List<FilterInfo> GetFilters(string tableName) => GetFilters(Assembly.GetCallingAssembly(), tableName);

public static async Task<Dictionary<string, object?>> GetAggregatesAsync<TModel, TDto>(
this IQueryable<TModel> dbSet,
List<AggregateDto> aggregates, CancellationToken cancellationToken = default) where TModel : class
Expand Down Expand Up @@ -397,7 +437,8 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(new KeyTask<long>()
{
Key = key,
Task =await dbSet.Select(lambda).Distinct().LongCountAsync(cancellationToken: cancellationToken),
Task = await dbSet.Select(lambda).Distinct()
.LongCountAsync(cancellationToken: cancellationToken),
});
break;
}
Expand All @@ -406,7 +447,7 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(new KeyTask<int>()
{
Key = key,
Task =await dbSet.Select(lambda).SumAsync(cancellationToken: cancellationToken),
Task = await dbSet.Select(lambda).SumAsync(cancellationToken: cancellationToken),
});
break;
}
Expand All @@ -424,7 +465,7 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(new KeyTask<int>()
{
Key = key,
Task =await dbSet.Select(lambda).MinAsync(cancellationToken: cancellationToken),
Task = await dbSet.Select(lambda).MinAsync(cancellationToken: cancellationToken),
});
break;
}
Expand Down Expand Up @@ -454,7 +495,8 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(new KeyTask<long>()
{
Key = key,
Task = await dbSet.Select(lambda).Distinct().LongCountAsync(cancellationToken: cancellationToken),
Task = await dbSet.Select(lambda).Distinct()
.LongCountAsync(cancellationToken: cancellationToken),
});
break;
}
Expand Down Expand Up @@ -514,7 +556,7 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(new KeyTask<DateTime?>()
{
Key = key,
Task =await Task.FromResult<DateTime?>(null)
Task = await Task.FromResult<DateTime?>(null)
});
}
else
Expand Down Expand Up @@ -547,23 +589,23 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(
new KeyTask<decimal>()
{
Task =await dbSet.Select(lambda).SumAsync(cancellationToken),
Task = await dbSet.Select(lambda).SumAsync(cancellationToken),
Key = key
});
break;
case AggregateType.Average:
tasks.Add(
new KeyTask<decimal>()
{
Task =await dbSet.Select(lambda).AverageAsync(cancellationToken),
Task = await dbSet.Select(lambda).AverageAsync(cancellationToken),
Key = key
});
break;
case AggregateType.Min:
tasks.Add(
new KeyTask<decimal>()
{
Task =await dbSet.Select(lambda).MinAsync(cancellationToken),
Task = await dbSet.Select(lambda).MinAsync(cancellationToken),
Key = key
});
break;
Expand All @@ -579,7 +621,7 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(
new KeyTask<decimal?>()
{
Task =await Task.FromResult<decimal?>(null),
Task = await Task.FromResult<decimal?>(null),
Key = key
});
break;
Expand All @@ -606,7 +648,7 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(
new KeyTask<double>()
{
Task =await dbSet.Select(lambda).SumAsync(cancellationToken),
Task = await dbSet.Select(lambda).SumAsync(cancellationToken),
Key = key
});
break;
Expand All @@ -622,7 +664,7 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
tasks.Add(
new KeyTask<double>()
{
Task =await dbSet.Select(lambda).MinAsync(cancellationToken),
Task = await dbSet.Select(lambda).MinAsync(cancellationToken),
Key = key
});
break;
Expand Down Expand Up @@ -665,7 +707,7 @@ public static async Task<DistinctColumnValuesResult> DistinctColumnValuesAsync<T
Task = Task.FromResult<object?>(null)
});
}


return tasks.ToDictionary(task => task.Key, task => task switch
{
Expand All @@ -685,8 +727,6 @@ private abstract class ImTask
public string Key = null!;
}



private class KeyTask<T> : ImTask
{
public T Task = default!;
Expand Down
2 changes: 1 addition & 1 deletion IEnumerableExtenders/IEnumerableExtenders.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>PandaTech.IEnumerableFilters</RootNamespace>
<Version>3.0.8</Version>
<Version>3.0.9</Version>
<Authors>PandaTech</Authors>
<Description>This NuGet helps with filtering tables.</Description>
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-ienumerable-extenders-filters.git</RepositoryUrl>
Expand Down

0 comments on commit f118b5b

Please sign in to comment.