Skip to content

Commit

Permalink
fix: Only apply .ToLower on properties that are strings (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamess-Lucass authored Feb 12, 2024
1 parent de5c810 commit 0b2ed17
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/controller/Dtos/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
3 changes: 3 additions & 0 deletions examples/controller/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
3 changes: 2 additions & 1 deletion examples/controller/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 10 additions & 4 deletions src/Extensions/QueryableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ public static (IQueryable, int?) Apply<T>(this IQueryable<T> 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());
}

Expand Down Expand Up @@ -117,4 +123,4 @@ public static (IQueryable, int?) Apply<T>(this IQueryable<T> queryable, Query qu

return (result, count);
}
}
}
1 change: 1 addition & 0 deletions tests/TestDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public record User

[Column("PersonSex", TypeName = "varchar(32)")]
public string Gender { get; set; } = string.Empty;
public int Age { get; set; }
}


Expand Down
26 changes: 26 additions & 0 deletions tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0b2ed17

Please sign in to comment.