|
| 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 | +} |
0 commit comments