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

Commit

Permalink
better enum handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DefGh committed Aug 1, 2023
1 parent 99ed61d commit 9fb6558
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
6 changes: 6 additions & 0 deletions IEnumerableExtenders.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestFilters", "TestFilters\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileldMapper", "FileldMapper\FileldMapper.csproj", "{2A25805E-BFDB-437C-9AC2-4EEC09A31E12}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{596D162E-EBD9-492E-AD32-84C461C160C4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -24,5 +26,9 @@ Global
{2A25805E-BFDB-437C-9AC2-4EEC09A31E12}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A25805E-BFDB-437C-9AC2-4EEC09A31E12}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A25805E-BFDB-437C-9AC2-4EEC09A31E12}.Release|Any CPU.Build.0 = Release|Any CPU
{596D162E-EBD9-492E-AD32-84C461C160C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{596D162E-EBD9-492E-AD32-84C461C160C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{596D162E-EBD9-492E-AD32-84C461C160C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{596D162E-EBD9-492E-AD32-84C461C160C4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
6 changes: 4 additions & 2 deletions IEnumerableExtenders/FilterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Add<TSource, TTarget>()
var filter = new Filter
{
SourcePropertyName = sourceProperty.Name,
SourcePropertyType = sourceProperty.PropertyType,
SourcePropertyType = sourceProperty.PropertyType.IsGenericType ? sourceProperty.PropertyType.GenericTypeArguments[0] : sourceProperty.PropertyType,
TargetPropertyName = targetProperty.Name,
TargetPropertyType = targetProperty.PropertyType,
ComparisonTypes = comparisonTypes,
Expand All @@ -83,14 +83,16 @@ public void Add<TSource, TTarget>()

foreach (var comparisonType in Enum.GetValues<ComparisonType>())
{


var key = new FilterKey
{
SourceType = sourceType,
TargetType = targetType,
SourcePropertyName = sourceProperty.Name,
TargetPropertyName = targetProperty.Name,
ComparisonType = comparisonType,
SourcePropertyType = sourceProperty.PropertyType,
SourcePropertyType = sourceProperty.PropertyType.IsGenericType ? sourceProperty.PropertyType.GenericTypeArguments[0] : sourceProperty.PropertyType,
TargetPropertyType = targetProperty.PropertyType
};

Expand Down
13 changes: 12 additions & 1 deletion IEnumerableExtenders/IEnumerableExtenders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ public static List<object> DistinctColumnValues<T>(this IEnumerable<T> dbSet, Li
{
var filter = filterProvider.GetFilter(columnName, typeof(T));

if (filter.TargetPropertyType.IsEnum)
return Enum.GetValues(filter.TargetPropertyType).Cast<object>().ToList();
// same for list
if (filter.TargetPropertyType.IsGenericType && filter.TargetPropertyType.GetGenericTypeDefinition() == typeof(List<>))
{
if (filter.TargetPropertyType.GetGenericArguments()[0].IsEnum)
return Enum.GetValues(filter.TargetPropertyType.GetGenericArguments()[0]).Cast<object>().ToList();
}



var propertyType = filter.TargetPropertyType;

var query = dbSet.ApplyFilters(filters, filterProvider);
Expand All @@ -291,7 +302,7 @@ public static List<object> DistinctColumnValues<T>(this IEnumerable<T> dbSet, Li
catch
{
query3 = query2;
return query3.Skip(pageSize * (page - 1)).Take(pageSize).Distinct().AsEnumerable().Select(filter.DtoConverter).ToList();
return query3.Skip(pageSize * (page - 1)).Take(pageSize * 10).Distinct().AsEnumerable().Select(filter.DtoConverter).ToList();
}
}
}
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>2.0.5</Version>
<Version>2.0.6</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
13 changes: 12 additions & 1 deletion TestFilters/Controllers/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Person
public string Phone { get; set; } = null!;

public List<int> Ints { get; set; } = null!;

public List<MyEnum> Enums { get; set; } = null!;
public double Money { get; set; }
public DateTime BirthDate { get; set; }
public bool IsMarried { get; set; }
Expand All @@ -45,6 +45,14 @@ public class Person
public List<Cat>? Cats { get; set; } = null!;
}

public enum MyEnum
{
One,
Two,
Three,
Four
}

public class PersonDto
{
public List<CatDto>? Cats { get; set; } = null!;
Expand All @@ -66,6 +74,9 @@ public class PersonDto

public DateOnly BirthDate => DateOnly.FromDateTime(DateTime.Now).AddYears(-Age);
public DateTime Now => DateTime.UtcNow;

public List<MyEnum> Enums { get; set; } = null!;

}

public class PersonDtoMapper : IMapping<Person, PersonDto>
Expand Down
9 changes: 3 additions & 6 deletions TestFilters/Controllers/SomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,9 @@ public IActionResult Distinct([FromBody] GetDataRequest getDataRequest, [FromRou


[HttpGet("[action]")]
public List<PersonDto> test1()
public List<object> test1()
{
Expression<Func<Person, bool>> ex;


return _context.Persons.Where("Name.StartsWith(@0)", "D").Take(10).AsEnumerable().Select(_personDtoMapper.Map)
.ToList();
return _context.Persons.Select(x => x.Enums).AsEnumerable().SelectMany(x => x).Distinct().Select(x => x.ToString() as object).ToList();
}

[HttpGet("[action]")]
Expand Down Expand Up @@ -188,6 +184,7 @@ public IActionResult PopulateDb()
IsMarried = Random.Shared.Next(0, 3) == 0,
IsWorking = Random.Shared.Next(0, 5) != 1,
Ints = new List<int> { Random.Shared.Next(0, 50), Random.Shared.Next(0, 50), Random.Shared.Next(0, 50) },
Enums = new List<MyEnum>() { MyEnum.One, MyEnum.Two, MyEnum.Three },
};

for (var j = 0; j < catCount; j++)
Expand Down

0 comments on commit 9fb6558

Please sign in to comment.