Skip to content

Commit 5f1ab9e

Browse files
authored
Merge pull request #16 from kodlamaio-projects/feat/persistence/in&between
feat(persistence): add in and between operator & remove case sensitivity
2 parents f7c1993 + 312468e commit 5f1ab9e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Core.Persistence/Dynamic/Filter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class Filter
66
public string Operator { get; set; }
77
public string? Value { get; set; }
88
public string? Logic { get; set; }
9+
public bool CaseSensitive { get; set; } = false;
910
public IEnumerable<Filter>? Filters { get; set; }
1011

1112
public Filter()

src/Core.Persistence/Dynamic/IQueryableDynamicFilterExtensions.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public static class IQueryableDynamicFilterExtensions
2121
{ "startswith", "StartsWith" },
2222
{ "endswith", "EndsWith" },
2323
{ "contains", "Contains" },
24-
{ "doesnotcontain", "Contains" }
24+
{ "doesnotcontain", "Contains" },
25+
{ "in", "In" },
26+
{ "between", "Between" }
2527
};
2628

2729
public static IQueryable<T> ToDynamic<T>(this IQueryable<T> query, DynamicQuery dynamicQuery)
@@ -93,7 +95,28 @@ public static string Transform(Filter filter, IList<Filter> filters)
9395
if (filter.Operator == "doesnotcontain")
9496
where.Append($"(!np({filter.Field}).{comparison}(@{index.ToString()}))");
9597
else if (comparison is "StartsWith" or "EndsWith" or "Contains")
96-
where.Append($"(np({filter.Field}).{comparison}(@{index.ToString()}))");
98+
{
99+
if (!filter.CaseSensitive)
100+
{
101+
where.Append($"(np({filter.Field}).ToLower().{comparison}(@{index.ToString()}.ToLower()))");
102+
}
103+
else
104+
{
105+
where.Append($"(np({filter.Field}).{comparison}(@{index.ToString()}))");
106+
}
107+
}
108+
else if (filter.Operator == "in")
109+
{
110+
where.Append($"np({filter.Field}) in ({filter.Value})");
111+
}
112+
else if (filter.Operator == "between")
113+
{
114+
var values = filter.Value.Split(',');
115+
if (values.Length != 2)
116+
throw new ArgumentException("Invalid Value for 'between' operator");
117+
118+
where.Append($"(np({filter.Field}) >= {values[0]} and np({filter.Field}) <= {values[1]})");
119+
}
97120
else
98121
where.Append($"np({filter.Field}) {comparison} @{index.ToString()}");
99122
else if (filter.Operator is "isnull" or "isnotnull")

0 commit comments

Comments
 (0)