diff --git a/examples/controller/Dtos/UserDto.cs b/examples/controller/Dtos/UserDto.cs index fa76287..e23fafb 100644 --- a/examples/controller/Dtos/UserDto.cs +++ b/examples/controller/Dtos/UserDto.cs @@ -16,4 +16,6 @@ public record UserDto public string UserName { get; set; } = string.Empty; public string Gender { get; set; } = string.Empty; + + public int Age { get; set; } } \ No newline at end of file diff --git a/examples/controller/Entities/User.cs b/examples/controller/Entities/User.cs index d7e4f17..4784eb5 100644 --- a/examples/controller/Entities/User.cs +++ b/examples/controller/Entities/User.cs @@ -23,4 +23,7 @@ public record User [Column("PersonSex", TypeName = "varchar(32)")] public string Gender { get; set; } = string.Empty; + + [Column("Age", TypeName = "integer")] + public int Age { get; set; } } \ No newline at end of file diff --git a/examples/controller/Program.cs b/examples/controller/Program.cs index 8ce4fd0..9dd91be 100644 --- a/examples/controller/Program.cs +++ b/examples/controller/Program.cs @@ -48,7 +48,8 @@ .RuleFor(x => x.AvatarUrl, f => f.Internet.Avatar()) .RuleFor(x => x.UserName, f => f.Person.UserName) .RuleFor(x => x.Gender, f => f.Person.Gender.ToString()) - .RuleFor(x => x.IsDeleted, f => f.Random.Bool()); + .RuleFor(x => x.IsDeleted, f => f.Random.Bool()) + .RuleFor(x => x.Age, f => f.Random.Number(1, 10)); context.Users.AddRange(users.Generate(1_000)); context.SaveChanges(); diff --git a/src/Extensions/QueryableExtension.cs b/src/Extensions/QueryableExtension.cs index e4f8a2c..36d443c 100644 --- a/src/Extensions/QueryableExtension.cs +++ b/src/Extensions/QueryableExtension.cs @@ -59,14 +59,20 @@ public static (IQueryable, int?) Apply(this IQueryable queryable, Query qu { where.Append($"{property}.{_filterOperations[operand]}({value})"); } - else + else if (typeof(T).GetProperties().FirstOrDefault(x => x.Name.Equals(property, StringComparison.OrdinalIgnoreCase))?.PropertyType == typeof(string)) { where.Append($"{property}.ToLower() {_filterOperations[operand]} {value}.ToLower()"); } + else if (typeof(T).GetProperties().FirstOrDefault(x => x.Name.Equals(property, StringComparison.OrdinalIgnoreCase))?.PropertyType == typeof(Guid)) + { + where.Append($"{property} {_filterOperations[operand]} Guid({value})"); + } + else + { + where.Append($"{property} {_filterOperations[operand]} {value}"); + } } - Console.WriteLine(where.ToString()); - result = result.Where(where.ToString()); } @@ -117,4 +123,4 @@ public static (IQueryable, int?) Apply(this IQueryable queryable, Query qu return (result, count); } -} \ No newline at end of file +} diff --git a/tests/TestDbContext.cs b/tests/TestDbContext.cs index d438f79..826cff0 100644 --- a/tests/TestDbContext.cs +++ b/tests/TestDbContext.cs @@ -14,6 +14,7 @@ public record User [Column("PersonSex", TypeName = "varchar(32)")] public string Gender { get; set; } = string.Empty; + public int Age { get; set; } } diff --git a/tests/Tests.cs b/tests/Tests.cs index b4608de..2aeb4f7 100644 --- a/tests/Tests.cs +++ b/tests/Tests.cs @@ -380,4 +380,30 @@ public void Test_QueryWithFilterCustomColumnName() Assert.Equal(expectedSql, sql); } + + [Fact] + public void Test_QueryWithFilterGuidEquals() + { + var query = new Query() { Filter = "id eq '7ac156a2-f938-43cb-8652-8b14a8b471de'" }; + + var (result, _) = _context.Users.AsQueryable().Apply(query, null); + var sql = result.ToQueryString(); + + var expectedSql = _context.Users.AsQueryable().Where(x => x.Id == new Guid("7ac156a2-f938-43cb-8652-8b14a8b471de")).ToQueryString(); + + Assert.Equal(expectedSql, sql); + } + + [Fact] + public void Test_QueryWithFilterIntegerEquals() + { + var query = new Query() { Filter = "age eq 4" }; + + var (result, _) = _context.Users.AsQueryable().Apply(query, null); + var sql = result.ToQueryString(); + + var expectedSql = _context.Users.AsQueryable().Where(x => x.Age == 4).ToQueryString(); + + Assert.Equal(expectedSql, sql); + } } \ No newline at end of file