-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from wemogy/108-sorting-is-missing
Introduced SortingParameters
- Loading branch information
Showing
21 changed files
with
4,704 additions
and
4,519 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Sorting & Pagination | ||
|
||
The DatabaseRepository methods `IterateAsync` and `QueryAsync` support sorting and pagination to allow the user to retrieve a subset of the results. | ||
|
||
## Sorting | ||
|
||
The generic `Sorting<>` objects allows you to define a sorting on a given properties (multiple properties helps you e.g. to order first on firstname and for all entities which have the same firstname, you can define that they should be ordered by the id property). The sorting can be ascending or descending. | ||
|
||
```csharp | ||
var sorting = new Sorting<User>() | ||
.OrderBy(x => x.FirstName) | ||
.OrderByDescending(x => x.Id); | ||
|
||
var sortedUsers = _userRepository.QueryAsync( | ||
x => true, | ||
sorting); | ||
``` | ||
|
||
## Pagination | ||
|
||
The `Pagination` objects allows you to define skip and take values to retrieve a subset of the results. | ||
|
||
```csharp | ||
var pagination = new Pagination() | ||
.Skip(10) | ||
.Take(10); | ||
|
||
var pagedUsers = _userRepository.QueryAsync( | ||
x => true, | ||
pagination); | ||
``` | ||
|
||
## Sorting & Pagination | ||
|
||
The `IterateAsync` and `QueryAsync` methods support sorting and pagination. You can combine both to retrieve a subset of the results in a sorted order. | ||
|
||
```csharp | ||
var sorting = new Sorting<User>() | ||
.OrderBy(x => x.FirstName) | ||
.OrderByDescending(x => x.Id); | ||
|
||
var pagination = new Pagination() | ||
.Skip(10) | ||
.Take(10); | ||
|
||
var sortedPagedUsers = _userRepository.QueryAsync( | ||
x => true, | ||
sorting, | ||
pagination); | ||
``` |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...core/Wemogy.Infrastructure.Database.Core.UnitTests/ValueObjects/SortingParametersTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Linq; | ||
using FluentAssertions; | ||
using Wemogy.Infrastructure.Database.Core.Enums; | ||
using Wemogy.Infrastructure.Database.Core.UnitTests.Fakes.Entities; | ||
using Wemogy.Infrastructure.Database.Core.ValueObjects; | ||
using Xunit; | ||
|
||
namespace Wemogy.Infrastructure.Database.Core.UnitTests.ValueObjects; | ||
|
||
public class SortingParametersTests | ||
{ | ||
[Fact] | ||
public void SortingParameters_OrderBy_HappyPath() | ||
{ | ||
// Arrange | ||
var sorting = new Sorting<Animal>(); | ||
|
||
// Act | ||
sorting | ||
.OrderBy(x => x.Firstname); | ||
sorting | ||
.OrderBy(x => x.BestFriend!.Firstname); | ||
|
||
// Assert | ||
sorting.Parameters.First().Property.Should().Be("Firstname"); | ||
sorting.Parameters.First().Direction.Should().Be(SortDirection.Ascending); | ||
sorting.Parameters[1].Property.Should().Be("BestFriend.Firstname"); | ||
sorting.Parameters[1].Direction.Should().Be(SortDirection.Ascending); | ||
} | ||
|
||
[Fact] | ||
public void SortingParameters_ApplyTo_HappyPath() | ||
{ | ||
// Arrange | ||
var animals = Animal.Faker.Generate(10); | ||
var sortingParameters = new Sorting<Animal>() | ||
.OrderBy(x => x.Firstname); | ||
|
||
// Act | ||
var sortedAnimals = sortingParameters | ||
.ApplyTo(animals) | ||
.ToList(); | ||
|
||
// Assert | ||
sortedAnimals.Should().NotBeNull(); | ||
sortedAnimals.Should().HaveCount(10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.