From c11424098cb42fff8a3ef8b8325b6739521ca894 Mon Sep 17 00:00:00 2001 From: Mehdi Oueldi Date: Mon, 6 Feb 2023 22:53:05 +0100 Subject: [PATCH] added dynamic query extensions --- .../DynamicQueryExtensions.cs | 39 +++++++++++++ .../IQueryFilter.cs | 5 +- .../ExpressionBuilderTests.cs | 58 ++++++++++++++++++- 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/Azure.EntityServices.Queries/HelperExtensions/DynamicQueryExtensions.cs b/src/Azure.EntityServices.Queries/HelperExtensions/DynamicQueryExtensions.cs index 8148b70..fa48821 100644 --- a/src/Azure.EntityServices.Queries/HelperExtensions/DynamicQueryExtensions.cs +++ b/src/Azure.EntityServices.Queries/HelperExtensions/DynamicQueryExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Azure.EntityServices.Queries { @@ -23,5 +24,43 @@ public static IFilterOperator WithEach(this IFilterOperator query, I } return dynamicQuery; } + + /// + /// Build a filter to check if current field value was present in given list + /// + /// + /// + /// + /// + /// + public static IFilterOperator In(this IQueryFilter query, params P[] values) + { + IQuery nextQuery = (IQuery)query; + foreach (var item in values.SkipLast(1)) + { + nextQuery = (IQuery)(nextQuery as IQueryFilter).Equal(item).Or(query.PropertyName); + } + (nextQuery as IQueryFilter).Equal(values.Last()); + return nextQuery as IFilterOperator; + } + + /// + /// Build a filter to check if current field value was not present in given list + /// + /// + /// + /// + /// + /// + public static IFilterOperator NotIn(this IQueryFilter query, params P[] values) + { + IQuery nextQuery = (IQuery)query; + foreach (var item in values.SkipLast(1)) + { + nextQuery = (IQuery)(nextQuery as IQueryFilter).NotEqual(item).And(query.PropertyName); + } + (nextQuery as IQueryFilter).NotEqual(values.Last()); + return nextQuery as IFilterOperator; + } } } \ No newline at end of file diff --git a/src/Azure.EntityServices.Queries/IQueryFilter.cs b/src/Azure.EntityServices.Queries/IQueryFilter.cs index 6a25866..e7ca300 100644 --- a/src/Azure.EntityServices.Queries/IQueryFilter.cs +++ b/src/Azure.EntityServices.Queries/IQueryFilter.cs @@ -7,8 +7,11 @@ public interface IQueryFilter : IQueryFilter public interface IQueryFilter { + string PropertyName { get; } + IFilterOperator AddFilterCondition(string comparison, P value); + IFilterOperator AddFilterCondition(string comparison, object value, Type type); - + } } \ No newline at end of file diff --git a/tests/Azure.EntityServices.Tests/QueryExpressions/ExpressionBuilderTests.cs b/tests/Azure.EntityServices.Tests/QueryExpressions/ExpressionBuilderTests.cs index 0469e63..701a070 100644 --- a/tests/Azure.EntityServices.Tests/QueryExpressions/ExpressionBuilderTests.cs +++ b/tests/Azure.EntityServices.Tests/QueryExpressions/ExpressionBuilderTests.cs @@ -122,7 +122,7 @@ public void Should_BuildGroup_Dynamic_Query_Expression() } [TestMethod] - public void Should_BuildGroup_Dynamic_Query_Expression_With_Extension_Helper() + public void Should_BuildGroup_Dynamic_Query_Expression_WithEach_Extension_Helper() { var builder = new MockedExpressionBuilder(); @@ -138,5 +138,61 @@ public void Should_BuildGroup_Dynamic_Query_Expression_With_Extension_Helper() .Should() .Be("TenantId Equal '50' Or FirstName Equal 'do 0' Or FirstName Equal 'do 1' Or FirstName Equal 'do 2' Or FirstName Equal 'do 3' Or FirstName Equal 'do 4' Or FirstName Equal 'do 5' Or FirstName Equal 'do 6' Or FirstName Equal 'do 7' Or FirstName Equal 'do 8' Or FirstName Equal 'do 9'"); } + + [TestMethod] + public void Should_BuildGroup_Dynamic_Query_Expression_In_Extension_Helper() + { + var builder = new MockedExpressionBuilder(); + + var dynamicQuery = builder.Query + .Where(p => p.TenantId).Equal("50") + .And(p => p.LastName) + .In("Doe"); + + var queryStr = builder.Build(); + + queryStr.Trim() + .Should() + .Be("TenantId Equal '50' And LastName Equal 'Doe'"); + + dynamicQuery = builder.Query + .Where(p => p.TenantId).Equal("50") + .And("LastName") + .In("Doe", "Kent"); + + queryStr = builder.Build(); + + queryStr.Trim() + .Should() + .Be("TenantId Equal '50' And LastName Equal 'Doe' Or LastName Equal 'Kent'"); + } + + [TestMethod] + public void Should_BuildGroup_Dynamic_Query_Expression_NotIn_Extension_Helper() + { + var builder = new MockedExpressionBuilder(); + + var dynamicQuery = builder.Query + .Where(p => p.TenantId).Equal("50") + .And(p => p.LastName) + .NotIn("Doe"); + + var queryStr = builder.Build(); + + queryStr.Trim() + .Should() + .Be("TenantId Equal '50' And LastName NotEqual 'Doe'"); + + dynamicQuery = builder.Query + .Where(p => p.TenantId).Equal("50") + .And("LastName") + .NotIn("Doe", "Kent"); + + queryStr = builder.Build(); + + queryStr.Trim() + .Should() + .Be("TenantId Equal '50' And LastName NotEqual 'Doe' And LastName NotEqual 'Kent'"); + } } } \ No newline at end of file