Skip to content

Commit 6401afa

Browse files
committed
Add Nested example page to basic sample
1 parent c67bb83 commit 6401afa

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

samples/Basic/Models/User.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,15 @@ public class User
1010
public string Name { get; set; }
1111

1212
public DateTime Created { get; set; }
13+
14+
public UserDetails Details { get; set; }
15+
}
16+
17+
[Index(nameof(Created))]
18+
public class UserDetails
19+
{
20+
public int Id { get; set; }
21+
22+
public DateTime Created { get; set; }
1323
}
1424
}

samples/Basic/Pages/Nested.cshtml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@page
2+
@model NestedModel
3+
4+
<div class="text-center">
5+
@if (Model.Users.Any())
6+
{
7+
<div class="users">
8+
@foreach (var user in Model.Users)
9+
{
10+
<div class="user">
11+
@user.Name (@user.Details.Created.ToLongDateString())
12+
</div>
13+
}
14+
</div>
15+
16+
<div class="mt-3" style="text-align: center">
17+
(@Model.Count items) (@Model.Elapsed | total with prev/next: @Model.ElapsedTotal)
18+
</div>
19+
20+
<div class="pagination mt-3">
21+
<a asp-page="/Nested" asp-route-first="@true" disabled="@(!Model.HasPrevious ? "disabled" : null)">First</a>
22+
<a asp-page="/Nested" asp-route-before="@Model.Users.First().Id" disabled="@(!Model.HasPrevious ? "disabled" : null)">Previous</a>
23+
<a asp-page="/Nested" asp-route-after="@Model.Users.Last().Id" disabled="@(!Model.HasNext ? "disabled" : null)">Next</a>
24+
<a asp-page="/Nested" asp-route-last="@true" disabled="@(!Model.HasNext ? "disabled" : null)">Last</a>
25+
</div>
26+
}
27+
else
28+
{
29+
<div>Nothing</div>
30+
}
31+
</div>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Diagnostics;
2+
using Basic.Models;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
using Microsoft.EntityFrameworkCore;
5+
using MR.EntityFrameworkCore.KeysetPagination;
6+
7+
namespace Basic.Pages
8+
{
9+
public class NestedModel : PageModel
10+
{
11+
private readonly AppDbContext _dbContext;
12+
13+
public NestedModel(
14+
AppDbContext dbContext)
15+
{
16+
_dbContext = dbContext;
17+
}
18+
19+
public int Count { get; set; }
20+
21+
public List<User> Users { get; set; }
22+
23+
public bool HasPrevious { get; set; }
24+
25+
public bool HasNext { get; set; }
26+
27+
public string Elapsed { get; set; }
28+
29+
public string ElapsedTotal { get; set; }
30+
31+
public async Task OnGet(int? after, int? before, bool first = false, bool last = false)
32+
{
33+
var size = 20;
34+
35+
var keysetBuilderAction = (KeysetPaginationBuilder<User> b) =>
36+
{
37+
b.Descending(x => x.Details.Created);
38+
};
39+
40+
var sw = Stopwatch.StartNew();
41+
42+
var query = _dbContext.Users.AsQueryable();
43+
Count = await query.CountAsync();
44+
KeysetPaginationContext<User> keysetContext;
45+
if (first)
46+
{
47+
keysetContext = query.KeysetPaginate(keysetBuilderAction, KeysetPaginationDirection.Forward);
48+
Users = await keysetContext.Query
49+
.Include(x => x.Details)
50+
.Take(size)
51+
.ToListAsync();
52+
}
53+
else if (last)
54+
{
55+
keysetContext = query.KeysetPaginate(keysetBuilderAction, KeysetPaginationDirection.Backward);
56+
Users = await keysetContext.Query
57+
.Include(x => x.Details)
58+
.Take(size)
59+
.ToListAsync();
60+
}
61+
else if (after != null)
62+
{
63+
var reference = await _dbContext.Users.Include(x => x.Details).FirstOrDefaultAsync(x => x.Id == after.Value);
64+
keysetContext = query.KeysetPaginate(keysetBuilderAction, KeysetPaginationDirection.Forward, reference);
65+
Users = await keysetContext.Query
66+
.Include(x => x.Details)
67+
.Take(size)
68+
.ToListAsync();
69+
}
70+
else if (before != null)
71+
{
72+
var reference = await _dbContext.Users.Include(x => x.Details).FirstOrDefaultAsync(x => x.Id == before.Value);
73+
keysetContext = query.KeysetPaginate(keysetBuilderAction, KeysetPaginationDirection.Backward, reference);
74+
Users = await keysetContext.Query
75+
.Include(x => x.Details)
76+
.Take(size)
77+
.ToListAsync();
78+
}
79+
else
80+
{
81+
keysetContext = query.KeysetPaginate(keysetBuilderAction);
82+
Users = await keysetContext.Query
83+
.Include(x => x.Details)
84+
.Take(size)
85+
.ToListAsync();
86+
}
87+
88+
keysetContext.EnsureCorrectOrder(Users);
89+
90+
Elapsed = sw.ElapsedMilliseconds.ToString();
91+
92+
HasPrevious = await keysetContext.HasPreviousAsync(Users);
93+
HasNext = await keysetContext.HasNextAsync(Users);
94+
95+
ElapsedTotal = sw.ElapsedMilliseconds.ToString();
96+
}
97+
}
98+
}

samples/Basic/Pages/Shared/_Layout.cshtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<li class="nav-item">
2525
<a class="nav-link text-dark" asp-area="" asp-page="/Example2">Example 2</a>
2626
</li>
27+
<li class="nav-item">
28+
<a class="nav-link text-dark" asp-area="" asp-page="/Nested">Nested</a>
29+
</li>
2730
</ul>
2831
</div>
2932
</div>

samples/Basic/Startup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4141
Id = i,
4242
Name = i.ToString(),
4343
Created = created,
44+
Details = new UserDetails
45+
{
46+
Created = created,
47+
},
4448
});
4549
}
4650
_dbContext.AddRange(users);

0 commit comments

Comments
 (0)