Skip to content

Commit

Permalink
fix: QueryBuilder ordering with custom mapper (#96)
Browse files Browse the repository at this point in the history
* Fix QueryBuilder ordering with custom mapper

* Fix all build methods of QueryBuilder
  • Loading branch information
TSrgy authored Mar 4, 2023
1 parent 208cd40 commit 8ad7884
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Gridify/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public IQueryable<T> Build(IQueryable<T> context)
query = query.Where(BuildFilteringExpression());

if (!string.IsNullOrEmpty(_orderBy))
query = query.ApplyOrdering(_orderBy);
query = query.ApplyOrdering(_orderBy, _mapper);

if (_paging.HasValue)
query = query.Skip(_paging.Value.page * _paging.Value.pageSize).Take(_paging.Value.pageSize);
Expand All @@ -235,7 +235,7 @@ public Func<IEnumerable<T>, IEnumerable<T>> BuildCompiled()
collection = collection.Where(compiled);

if (!string.IsNullOrEmpty(_orderBy)) // TODO: this also should be compiled
collection = collection.AsQueryable().ApplyOrdering(_orderBy);
collection = collection.AsQueryable().ApplyOrdering(_orderBy, _mapper);

if (_paging.HasValue)
collection = collection.Skip(_paging.Value.page * _paging.Value.pageSize).Take(_paging.Value.pageSize);
Expand All @@ -251,7 +251,7 @@ public IEnumerable<T> Build(IEnumerable<T> collection)
collection = collection.Where(BuildFilteringExpression().Compile());

if (!string.IsNullOrEmpty(_orderBy))
collection = collection.AsQueryable().ApplyOrdering(_orderBy);
collection = collection.AsQueryable().ApplyOrdering(_orderBy, _mapper);

if (_paging.HasValue)
collection = collection.Skip(_paging.Value.page * _paging.Value.pageSize).Take(_paging.Value.pageSize);
Expand Down Expand Up @@ -294,7 +294,7 @@ public Func<IEnumerable<T>, Paging<T>> BuildWithPagingCompiled()
collection = collection.Where(compiled);

if (!string.IsNullOrEmpty(_orderBy)) // TODO: this also should be compiled
collection = collection.AsQueryable().ApplyOrdering(_orderBy);
collection = collection.AsQueryable().ApplyOrdering(_orderBy, _mapper);

var result = collection.ToList();
var count = result.Count();
Expand All @@ -314,7 +314,7 @@ public QueryablePaging<T> BuildWithQueryablePaging(IQueryable<T> collection)
query = query.Where(BuildFilteringExpression());

if (!string.IsNullOrEmpty(_orderBy))
query = query.ApplyOrdering(_orderBy);
query = query.ApplyOrdering(_orderBy, _mapper);

var count = query.Count();

Expand Down
19 changes: 19 additions & 0 deletions test/Gridify.Tests/QueryBuilderShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ public void BuildFilteringExpression_ParameterName_ShouldHaveValidName()
Assert.Equal(expected, expression.Parameters.First().Name);
}

[Fact]
public void AddOrderBy_NotMappedColumn_WithCustomMapper_ShouldThrowException()
{
var builder = new QueryBuilder<TestClass>()
.UseCustomMapper(new GridifyMapper<TestClass>())
.AddOrderBy("name");

var buildIEnumerableAction = () => builder.Build(_fakeRepository);
var buildIQueryableAction = () => builder.Build(_fakeRepository.AsQueryable());
var buildWithQueryablePagingAction = () => builder.BuildWithQueryablePaging(_fakeRepository.AsQueryable());
var buildCompiledAction = () => builder.BuildCompiled()(_fakeRepository);
var BuildWithPagingCompiledAction = () => builder.BuildWithPagingCompiled()(_fakeRepository);

Assert.Throws<GridifyMapperException>(buildIEnumerableAction);
Assert.Throws<GridifyMapperException>(buildIQueryableAction);
Assert.Throws<GridifyMapperException>(buildWithQueryablePagingAction);
Assert.Throws<GridifyMapperException>(buildCompiledAction);
Assert.Throws<GridifyMapperException>(BuildWithPagingCompiledAction);
}

#region Validation

Expand Down

0 comments on commit 8ad7884

Please sign in to comment.