labdb/Pages/Tests/List.razor
2022-11-15 18:07:10 +01:00

94 lines
2 KiB
Plaintext

@page "/tests"
@using Microsoft.EntityFrameworkCore
<PageTitle>Tests</PageTitle>
<h1>Tests</h1>
<a href="/tests/add" class="btn btn-primary">Add new</a>
<table class="table table-striped">
<thead>
<tr>
<td>Name</td>
<td>Abbreviation</td>
<td>Synonyms</td>
</tr>
</thead>
<tbody>
@foreach (var test in Tests)
{
<tr>
<td>
<a href="@($"/tests/{test.Id.Value}")">@test.Name</a>
</td>
<td>
@test.Abbreviation
</td>
<td>
@(string.Join(", ", test.Synonyms.Select(x => x.Name).ToArray()))
</td>
</tr>
}
</tbody>
</table>
<div>
<ul class="pagination">
@for (var i = 1; i <= TotalPages; i++)
{
var p = i;
<li class="page-item @(i == CurrentPage ? "active" : "")">
<a class="page-link" href="@($"/labs?page={p}")">@p</a>
</li>
}
</ul>
</div>
@code {
[Inject]
public IDbContextFactory<AppDbContext> DbContextFactory { get; set; } = null!;
[Parameter]
[SupplyParameterFromQuery(Name = "page")]
public int? CurrentPage { get; set; }
private int Count { get; set; }
private int PageSize { get; set; } = 10;
private int TotalPages => (int)Math.Ceiling(decimal.Divide(Count, PageSize));
private List<Test> Tests { get; set; } = new();
private void Load()
{
using var ctx = DbContextFactory.CreateDbContext();
Count = ctx.Set<Lab>().Count();
var page = CurrentPage ?? 1;
Tests = ctx.Set<Test>()
.OrderBy(x => x.Id)
.Skip((page - 1) * PageSize)
.Take(PageSize)
.Include(x => x.Synonyms)
.ToList();
}
protected override void OnInitialized()
{
base.OnInitialized();
Load();
}
protected override void OnParametersSet()
{
base.OnParametersSet();
Load();
}
}