Skip to content

Commit

Permalink
Added support for between both cursors (#7800)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunghwan2789 authored and michaelstaib committed Dec 3, 2024
1 parent 6824099 commit 7cafe32
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ public static async ValueTask<Page<T>> ToPageAsync<T>(
if (arguments.After is not null)
{
var cursor = CursorParser.Parse(arguments.After, keys);
source = source.Where(BuildWhereExpression<T>(keys, cursor, forward));
source = source.Where(BuildWhereExpression<T>(keys, cursor, true));
}

if (arguments.Before is not null)
{
var cursor = CursorParser.Parse(arguments.Before, keys);
source = source.Where(BuildWhereExpression<T>(keys, cursor, forward));
source = source.Where(BuildWhereExpression<T>(keys, cursor, false));
}

if (arguments.First is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ public async Task Fetch_First_2_Items_Third_Page()
page.MatchMarkdownSnapshot();
}

[Fact]
public async Task Fetch_First_2_Items_Between()
{
// Arrange
var connectionString = CreateConnectionString();
await SeedAsync(connectionString);

// .. get first page
var arguments = new PagingArguments(4);
await using var context = new CatalogContext(connectionString);
var page = await context.Products.OrderBy(t => t.Name).ThenBy(t => t.Id)
.ToPageAsync(arguments);

// Act
arguments = new PagingArguments(2, after: page.CreateCursor(page.First!), before: page.CreateCursor(page.Last!));
page = await context.Products.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(arguments);

// Assert
page.MatchMarkdownSnapshot();
}

[Fact]
public async Task Fetch_Last_2_Items()
{
Expand Down Expand Up @@ -118,6 +139,29 @@ public async Task Fetch_Last_2_Items_Before_Last_Page()
page.MatchMarkdownSnapshot();
}

[Fact]
public async Task Fetch_Last_2_Items_Between()
{
// Arrange
var connectionString = CreateConnectionString();
await SeedAsync(connectionString);

// .. get last page
var arguments = new PagingArguments(last: 4);
await using var context = new CatalogContext(connectionString);
var page = await context.Products
.OrderBy(t => t.Name)
.ThenBy(t => t.Id)
.ToPageAsync(arguments);

// Act
arguments = new PagingArguments(after: page.CreateCursor(page.First!), last: 2, before: page.CreateCursor(page.Last!));
page = await context.Products.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(arguments);

// Assert
page.MatchMarkdownSnapshot();
}

[Fact]
public async Task Batch_Fetch_First_2_Items()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Fetch_First_2_Items_Between

```json
[
{
"Id": 2,
"Name": "Product 0-1",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 1,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 11,
"Name": "Product 0-10",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 1,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Fetch_Last_2_Items_Between

```json
[
{
"Id": 9998,
"Name": "Product 99-97",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 100,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 9999,
"Name": "Product 99-98",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 100,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
```

0 comments on commit 7cafe32

Please sign in to comment.