Skip to content

Commit

Permalink
Update filter model to better suit the AgGrid structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed Aug 1, 2024
1 parent ed98c4d commit d11bfa0
Show file tree
Hide file tree
Showing 33 changed files with 396 additions and 456 deletions.
51 changes: 51 additions & 0 deletions Ctoss.Example/JsonExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Ctoss.Example;

public static class JsonExamples
{
/// <summary>
/// This JSON snippet contains an example of the date range filter with
/// an illustration that field is case-insensitive.
/// </summary>
public const string PlainDateRangeFilter =
"""
{
"filters":{
"PrOpErTy":{
"filterType":"date",
"type":"inRange",
"dateFrom":"10/10/2002",
"dateTo":"10/12/2020"
}
}
}
""";

/// <summary>
/// This illustrates how to define multiple conditions, and use CTOSS configuration to define the virtual field ms
/// </summary>
public const string MultipleConditionsNumberFilter =
"""
{
"filters":{
"mc":{
"filterType":"number",
"operator": "and",
"conditions":[
{
"filterType":"number",
"type":"GreaterThan",
"filter":"10"
},
{
"filterType":"number",
"type":"LessThan",
"filter":"15"
}
]
}
}
}
""";


}
95 changes: 3 additions & 92 deletions Ctoss.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Ctoss.Configuration;
using Ctoss.Configuration.Builders;
using Ctoss.Configuration.Builders;
using Ctoss.Example;
using Ctoss.Extensions;
using Ctoss.Models;
Expand All @@ -18,83 +17,8 @@
.Property(x => x.TextField, settings => { settings.IgnoreCase = true; })
.Apply();

const string jsonFilter =
"""
{
"PrOpErTy": {
"filterType": "date",
"condition1": {
"filterType": "date",
"type": "inRange",
"dateFrom": "10/10/2002",
"dateTo": "10/12/2020"
},
"conditions": [
{
"filterType": "date",
"type": "inRange",
"date": "10/10/2002",
"dateTo": "10/12/2020"
}
]
}
}
""";

const string jsonNumericFilter =
"""
{
"mc": {
"filterType": "number",
"condition1": {
"filterType": "number",
"type": "inRange",
"filter": "1",
"filterTo": "20"
},
"conditions": [
{
"filterType": "number",
"type": "GreaterThan",
"filter": "10"
}
]
}
}
""";

const string jsonTextFilter =
"""
{
"TextField": {
"filterType": "text",
"condition1": {
"filterType": "text",
"type": "contains",
"filter": "a"
},
"conditions": [
{
"filterType": "text",
"type": "contains",
"filter": "a"
}
]
}
}
""";

/*
* The CTOSS gives you three overloads of the method WithFilter which evaluates a given filter and provides you with
* a filtering Expression<Func<T, bool>> fully compatible with IQueryable and EF.
*
* Overloads:
* - WithFilter<T>(this IQueryable<T> query, string jsonFilter)
* - WithFilter<T>(this IQueryable<T> query, string propertyName, Filter filter)
* - WithFilter<T>(this IQueryable<T> query, Dictionary<string, Filter> filters)
*/
var entities = ExampleEntityFaker.GetN(100).AsQueryable()
.WithFilter(jsonFilter) // <-- This is the extension method from the ctoss library
.WithFilter(JsonExamples.PlainDateRangeFilter)
.ToList();

Console.WriteLine("Filtered entities:");
Expand All @@ -112,23 +36,10 @@
};

var numericEntities = ExampleNumericEntityFaker.GetN(100).AsQueryable()
.WithFilter(jsonNumericFilter) // <-- This is the extension method from the ctoss library
.WithFilter(JsonExamples.MultipleConditionsNumberFilter)
.WithSorting(sortings)
.WithPagination(1, 10)
.ToList();

foreach (var entity in numericEntities)
Console.WriteLine($"A: {entity.A}, B: {entity.B}, SubEntity = ({entity.SubEntity.A + entity.SubEntity.B})");

Check warning on line 45 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 45 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 45 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 45 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Console.WriteLine("\nText entities:");

var textEntity = new ExampleTextEntity()
{
TextField = "abc"
};

var textEntities = new List<ExampleTextEntity> { textEntity }.AsQueryable()
.WithFilter(jsonTextFilter) // <-- This is the extension method from the ctoss library
.ToList();

foreach (var entity in textEntities) Console.WriteLine(entity.TextField);
116 changes: 30 additions & 86 deletions Ctoss.Tests/DateFilterTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Ctoss.Builders.Filters;
using Ctoss.Models;
using Ctoss.Models.Conditions;
using Ctoss.Models.Enums;
using Ctoss.Models.V2;
using Ctoss.Tests.Models;

namespace Ctoss.Tests;
Expand All @@ -12,35 +11,19 @@ public class DateFilterTests

private readonly List<TestEntity> _testEntities =
[
new TestEntity
{
NumericProperty = 10, StringProperty = "abc", DateTimeProperty = new DateOnly(2022, 1, 1)
},
new TestEntity
{
NumericProperty = 20, StringProperty = "def", DateTimeProperty = new DateOnly(2023, 2, 2)
},
new TestEntity
{
NumericProperty = 30, StringProperty = "ghi", DateTimeProperty = new DateOnly(2024, 3, 3)
}
new TestEntity(numericProperty: 10, stringProperty: "abc", dateTimeProperty: new DateOnly(2022, 1, 1)),
new TestEntity(numericProperty: 20, stringProperty: "def", dateTimeProperty: new DateOnly(2023, 2, 2)),
new TestEntity(numericProperty: 30, stringProperty: "ghi", dateTimeProperty: new DateOnly(2024, 3, 3))
];

[Fact]
public void DateFilter_Equals_Success()
{
var condition = new DateFilterCondition
{
DateFrom = "01/01/2022",
FilterType = "date",
Type = DateFilterOptions.Equals
};

var filter = new Filter
var filter = new FilterModel
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
DateFrom = "01/01/2022",
Type = "equals"
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
Expand All @@ -53,18 +36,11 @@ public void DateFilter_Equals_Success()
[Fact]
public void DateFilter_GreaterThen_Success()
{
var condition = new DateFilterCondition
var filter = new FilterModel
{
DateFrom = "02/02/2023",
FilterType = "date",
Type = DateFilterOptions.GreaterThen
};

var filter = new Filter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
DateFrom = "02/02/2023",
Type = "GreaterThen"
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
Expand All @@ -77,17 +53,10 @@ public void DateFilter_GreaterThen_Success()
[Fact]
public void DateFilter_Blank_Success()
{
var condition = new DateFilterCondition
{
FilterType = "date",
Type = DateFilterOptions.Blank
};

var filter = new Filter
var filter = new FilterModel
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
Type = "Blank"
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
Expand All @@ -99,18 +68,11 @@ public void DateFilter_Blank_Success()
[Fact]
public void DateFilter_LessThen_Success()
{
var condition = new DateFilterCondition
{
DateFrom = "01/01/2023",
FilterType = "date",
Type = DateFilterOptions.LessThen
};

var filter = new Filter
var filter = new FilterModel
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
DateFrom = "01/01/2023",
Type = "LessThen"
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
Expand All @@ -123,17 +85,10 @@ public void DateFilter_LessThen_Success()
[Fact]
public void DateFilter_NotBlank_Success()
{
var condition = new DateFilterCondition
var filter = new FilterModel
{
FilterType = "date",
Type = DateFilterOptions.NotBlank
};

var filter = new Filter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
Type = "NotBlank"
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
Expand All @@ -145,19 +100,12 @@ public void DateFilter_NotBlank_Success()
[Fact]
public void DateFilter_InRange_Success()
{
var condition = new DateFilterCondition
var filter = new FilterModel
{
FilterType = "date",
DateFrom = "06/06/2021",
DateTo = "09/09/2024",
FilterType = "date",
Type = DateFilterOptions.InRange
};

var filter = new Filter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
Type = "InRange"
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
Expand All @@ -169,26 +117,24 @@ public void DateFilter_InRange_Success()
[Fact]
public void DateFilter_NotEquals_Success()
{
var condition1 = new DateFilterCondition
var condition1 = new DateCondition
{
DateFrom = "01/01/2022",
FilterType = "date",
Type = DateFilterOptions.NotEquals
};
var condition2 = new DateFilterCondition
var condition2 = new DateCondition
{
DateFrom = "03/03/2024",
FilterType = "date",
Type = DateFilterOptions.NotEquals
};

var filter = new Filter
var filter = new FilterModel
{
FilterType = "date",
Operator = Operator.And,
Condition1 = condition1,
Condition2 = condition2,
Conditions = new List<FilterCondition> { condition1, condition2 }
Conditions = new List<FilterConditionBase> { condition1, condition2 }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
Expand All @@ -201,27 +147,25 @@ public void DateFilter_NotEquals_Success()
[Fact]
public void DateFilter_Composed_Success()
{
var condition1 = new DateFilterCondition
var condition1 = new DateCondition
{
DateFrom = "01/01/2022",
FilterType = "date",
Type = DateFilterOptions.NotEquals
};

var condition2 = new DateFilterCondition
var condition2 = new DateCondition
{
DateFrom = "03/03/2024",
FilterType = "date",
Type = DateFilterOptions.LessThen
};

var filter = new Filter
var filter = new FilterModel
{
Operator = Operator.And,
FilterType = "date",
Condition1 = condition1,
Condition2 = condition2,
Conditions = new List<FilterCondition>
Operator = Operator.And,
Conditions = new List<FilterConditionBase>
{
condition1, condition2
}
Expand All @@ -233,4 +177,4 @@ public void DateFilter_Composed_Success()
Assert.Single(result);
Assert.Equal(new DateOnly(2023, 02, 02), result.First().DateTimeProperty);
}
}
}
Loading

0 comments on commit d11bfa0

Please sign in to comment.