Skip to content

Commit

Permalink
added dynamic query extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi Oueldi committed Feb 6, 2023
1 parent ad5abaa commit c114240
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Azure.EntityServices.Queries
{
Expand All @@ -23,5 +24,43 @@ public static IFilterOperator<T> WithEach<T, U>(this IFilterOperator<T> query, I
}
return dynamicQuery;
}

/// <summary>
/// Build a filter to check if current field value was present in given list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="P"></typeparam>
/// <param name="query"></param>
/// <param name="values"></param>
/// <returns></returns>
public static IFilterOperator<T> In<T, P>(this IQueryFilter<T, P> query, params P[] values)
{
IQuery<T> nextQuery = (IQuery<T>)query;
foreach (var item in values.SkipLast(1))
{
nextQuery = (IQuery<T>)(nextQuery as IQueryFilter<T>).Equal(item).Or(query.PropertyName);
}
(nextQuery as IQueryFilter<T>).Equal(values.Last());
return nextQuery as IFilterOperator<T>;
}

/// <summary>
/// Build a filter to check if current field value was not present in given list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="P"></typeparam>
/// <param name="query"></param>
/// <param name="values"></param>
/// <returns></returns>
public static IFilterOperator<T> NotIn<T, P>(this IQueryFilter<T, P> query, params P[] values)
{
IQuery<T> nextQuery = (IQuery<T>)query;
foreach (var item in values.SkipLast(1))
{
nextQuery = (IQuery<T>)(nextQuery as IQueryFilter<T>).NotEqual(item).And(query.PropertyName);
}
(nextQuery as IQueryFilter<T>).NotEqual(values.Last());
return nextQuery as IFilterOperator<T>;
}
}
}
5 changes: 4 additions & 1 deletion src/Azure.EntityServices.Queries/IQueryFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ public interface IQueryFilter<T> : IQueryFilter<T, object>

public interface IQueryFilter<T, P>
{
string PropertyName { get; }

IFilterOperator<T> AddFilterCondition(string comparison, P value);

IFilterOperator<T> AddFilterCondition(string comparison, object value, Type type);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersonEntity>();

Expand All @@ -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<PersonEntity>();

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<PersonEntity>();

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'");
}
}
}

0 comments on commit c114240

Please sign in to comment.