(feature): paged request lower case comparison

This commit is contained in:
Maksym Sadovnychyy 2024-12-31 22:22:23 +01:00
parent 39d97500e9
commit 85f1d7c89f
3 changed files with 20 additions and 3 deletions

View File

@ -2,6 +2,8 @@
using Xunit;
using MaksIT.Core.Webapi.Models; // Ensure namespace matches the actual namespace of PagedRequest
namespace MaksIT.Core.Tests.Webapi.Models;
public class PagedRequestTests {
public class TestEntity {

View File

@ -8,7 +8,7 @@
<!-- NuGet package metadata -->
<PackageId>MaksIT.Core</PackageId>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
<Authors>Maksym Sadovnychyy</Authors>
<Company>MAKS-IT</Company>
<Product>MaksIT.Core</Product>

View File

@ -1,5 +1,6 @@
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using MaksIT.Core.Abstractions.Webapi;
public class PagedRequest : RequestModelBase {
@ -14,12 +15,26 @@ public class PagedRequest : RequestModelBase {
if (string.IsNullOrWhiteSpace(Filters))
return x => true; // Returns an expression that doesn't filter anything.
// Parse the filter string into a dynamic lambda expression.
// Adjust Filters to make Contains, StartsWith, EndsWith, ==, and != case-insensitive
string adjustedFilters = Filters
.Replace(".Contains(", ".ToLower().Contains(")
.Replace(".StartsWith(", ".ToLower().StartsWith(")
.Replace(".EndsWith(", ".ToLower().EndsWith(")
.Replace("==", ".ToLower() ==")
.Replace("!=", ".ToLower() !=");
// Ensure values are also transformed to lowercase
adjustedFilters = Regex.Replace(adjustedFilters, "\"([^\"]+)\"", m => $"\"{m.Groups[1].Value.ToLower()}\"");
// Parse the adjusted filter string into a dynamic lambda expression
var predicate = DynamicExpressionParser.ParseLambda<T, bool>(
new ParsingConfig(), false, Filters);
new ParsingConfig(), false, adjustedFilters);
return predicate;
}
public Func<IQueryable<T>, IOrderedQueryable<T>> BuildSortExpression<T>() {
if (string.IsNullOrWhiteSpace(SortBy))
return q => (IOrderedQueryable<T>)q; // Cast to IOrderedQueryable