sort fix.

This commit is contained in:
David Lebee 2018-10-22 22:08:27 -05:00
parent 788078bc49
commit 7a39d70a3e
2 changed files with 64 additions and 0 deletions

View File

@ -32,5 +32,62 @@ namespace PoweredSoft.DynamicQuery.Test
Assert.Equal(shouldResult, result.Data);
});
}
[Fact]
public void MultiSort()
{
MockContextFactory.SeedAndTestContextFor("SortTests_MultiSort", TestSeeders.SimpleSeedScenario, ctx =>
{
var shouldResult = ctx.Orders.OrderBy(t => t.Customer.FirstName).ThenBy(t => t.OrderNum).ToList();
// query handler that is empty should be the same as running to list.
var criteria = new QueryCriteria()
{
Sorts = new List<ISort>()
{
new Sort("Customer.FirstName"),
new Sort("OrderNum")
}
};
var queryHandler = new QueryHandler();
var result = queryHandler.Execute(ctx.Orders, criteria);
Assert.Equal(shouldResult, result.Data);
});
}
private class MockSortInterceptor : ISortInterceptor
{
public IEnumerable<ISort> InterceptSort(IEnumerable<ISort> sort)
{
if (sort.Count() == 1 && sort.First().Path == "OrderNumStr")
return new ISort[] { new Sort("OrderNum", false) };
return sort;
}
}
[Fact]
public void SortInterceptor()
{
MockContextFactory.SeedAndTestContextFor("SortTests_SortInterceptor", TestSeeders.SimpleSeedScenario, ctx =>
{
var shouldResult = ctx.Orders.OrderByDescending(t => t.OrderNum).ToList();
// query handler that is empty should be the same as running to list.
var criteria = new QueryCriteria()
{
Sorts = new List<ISort>()
{
new Sort("OrderNumStr", false)
}
};
var queryHandler = new QueryHandler();
queryHandler.AddInterceptor(new MockSortInterceptor());
var result = queryHandler.Execute(ctx.Orders, criteria);
Assert.Equal(shouldResult, result.Data);
});
}
}
}

View File

@ -68,11 +68,18 @@ namespace PoweredSoft.DynamicQuery
return;
}
bool isAppending = false;
Criteria.Sorts.ForEach(sort =>
{
var transformedSort = InterceptSort<T>(sort);
if (transformedSort.Count == 0)
return;
transformedSort.ForEach(ts =>
{
CurrentQueryable = CurrentQueryable.OrderBy(ts.Path, ts.Ascending == false ? QueryOrderByDirection.Descending : QueryOrderByDirection.Ascending, isAppending);
isAppending = true;
});
});
}