Skip to content

Commit

Permalink
Update model to fit the AgGrid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed Aug 1, 2024
1 parent 2e33550 commit 4b86a35
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 27 deletions.
9 changes: 3 additions & 6 deletions Ctoss/Builders/Filters/FilterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ public class FilterBuilder
private readonly IPropertyFilterBuilder<NumberCondition> _numberFilterBuilder = new NumberFilterBuilder();
private readonly IPropertyFilterBuilder<SetCondition> _setFilterBuilder = new SetFilterBuilder();

public Expression<Func<T, bool>>? GetExpression<T>(AgGridFilter? filterSet)
public Expression<Func<T, bool>>? GetExpression<T>(Dictionary<string, FilterModel>? filterSet)
{
if (filterSet == null)
return null;

var expressions = new List<Expression<Func<T, bool>>>();

expressions.AddRange(filterSet.Filters
expressions.AddRange(filterSet
.Select(filter => GetExpressionInternal<T>(filter.Key, filter.Value)));
return expressions.Aggregate((acc, expr) => acc.AndAlso(expr));
}

public Expression<Func<T, bool>>? GetExpression<T>(string property, FilterModel filter)
=> GetExpression<T>(new AgGridFilter
{
Filters = new Dictionary<string, FilterModel> { { property, filter } }
});
=> GetExpression<T>(new Dictionary<string, FilterModel> { { property, filter } });

private Expression<Func<T, bool>> GetExpressionInternal<T>(string property, FilterModel? filter)
{
Expand Down
4 changes: 2 additions & 2 deletions Ctoss/Extensions/AgGridExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class AgGridExtensions
{
public static AgGridQueryResult<T> Apply<T>(this IEnumerable<T> all, AgGridQuery query)
{
var applyFilter = query.FilterModel!.Filters is { Count: > 0 };
var applyFilter = query.FilterModel is { Count: > 0 };
if (applyFilter)
all = all.WithFilter(query.FilterModel!);

Expand All @@ -26,7 +26,7 @@ public static AgGridQueryResult<T> Apply<T>(this IEnumerable<T> all, AgGridQuery

public static AgGridQueryResult<T> Apply<T>(this IQueryable<T> all, AgGridQuery query)
{
var applyFilter = query.FilterModel!.Filters is { Count: > 0 };
var applyFilter = query.FilterModel! is { Count: > 0 };
if (applyFilter)
all = all.WithFilter(query.FilterModel!);

Expand Down
13 changes: 4 additions & 9 deletions Ctoss/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,20 @@ public static IEnumerable<T> WithSorting<T>(this IEnumerable<T> query, List<Sort
public static IEnumerable<T> WithFilter<T>(
this IEnumerable<T> query, string jsonFilter) =>
query.WithFilter(
JsonSerializer.Deserialize<AgGridFilter>(
JsonSerializer.Deserialize<Dictionary<string, FilterModel>?>(
jsonFilter, CtossJsonDefaults.DefaultJsonOptions)
);

public static IEnumerable<T> WithFilter<T>(
this IEnumerable<T> query, string propertyName, FilterModel? filter) =>
filter is null
? query
: WithFilter(
query,
new AgGridFilter
{
Filters = new Dictionary<string, FilterModel> { { propertyName, filter } }
});
: WithFilter(query, new Dictionary<string, FilterModel> { { propertyName, filter } });

public static IEnumerable<T> WithFilter<T>(
this IEnumerable<T> query, AgGridFilter? filterSet)
this IEnumerable<T> query, Dictionary<string, FilterModel>? filterSet)
{
if (filterSet is null || !filterSet.Filters.Any())
if (filterSet is null || !filterSet.Any())
return query;

var filterBuilder = new FilterBuilder();
Expand Down
13 changes: 4 additions & 9 deletions Ctoss/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,20 @@ public static IQueryable<T> WithSorting<T>(this IQueryable<T> query, List<Sortin
public static IQueryable<T> WithFilter<T>(
this IQueryable<T> query, string jsonFilter) =>
query.WithFilter(
JsonSerializer.Deserialize<AgGridFilter>(
JsonSerializer.Deserialize<Dictionary<string, FilterModel>?>(
jsonFilter, CtossJsonDefaults.DefaultJsonOptions)
);

public static IQueryable<T> WithFilter<T>(
this IQueryable<T> query, string propertyName, FilterModel? filter) =>
filter is null
? query
: WithFilter(
query,
new AgGridFilter
{
Filters = new Dictionary<string, FilterModel> { { propertyName, filter } }
});
: WithFilter(query, new Dictionary<string, FilterModel> { { propertyName, filter } });

public static IQueryable<T> WithFilter<T>(
this IQueryable<T> query, AgGridFilter? filtersSet)
this IQueryable<T> query, Dictionary<string, FilterModel>? filtersSet)
{
if (filtersSet is null || !filtersSet.Filters.Any())
if (filtersSet is null || !filtersSet.Any())
return query;

var filterBuilder = new FilterBuilder();
Expand Down
2 changes: 1 addition & 1 deletion Ctoss/Models/AgGrid/AgGridQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public record AgGridQuery(
int StartRow,
int EndRow,
List<Sorting>? SortModel,
AgGridFilter? FilterModel
Dictionary<string, FilterModel>? FilterModel
);

0 comments on commit 4b86a35

Please sign in to comment.