groceries/Groceries/Stores/StoresPage.razor

70 lines
2.3 KiB
Plaintext

@using Groceries.Data
@using Microsoft.EntityFrameworkCore
@layout Layout
@inject AppDbContext DbContext
<PageTitle>Groceries &ndash; Stores</PageTitle>
<header class="row">
<h1 class="row__fill">Stores</h1>
<search title="Stores">
<SearchForm data-turbo-frame="table" data-turbo-action="advance">
<input type="hidden" name="page" value="1" />
</SearchForm>
</search>
<a class="button button--primary" href="/stores/new" data-turbo-frame="modal">New store</a>
</header>
<turbo-frame id="table" target="_top">
<section class="table">
<Table Items="stores" Pagination="pagination" HeaderClass="table__header--shaded">
<PropertyTableColumn Property="s => s.Retailer" Sortable="true" />
<PropertyTableColumn Property="s => s.Name" Fill="true" Sortable="true" />
<PropertyTableColumn Property="s => s.TransactionsCount" Title="Transactions" Sortable="true" />
<TemplateTableColumn Context="store">
<a class="link" href="/stores/edit/@store.Id" data-turbo-frame="modal">Edit</a>
</TemplateTableColumn>
</Table>
<TablePaginator State="pagination" />
</section>
</turbo-frame>
@code {
private record StoreModel
{
public Guid Id { get; init; }
public required string Retailer { get; init; }
public required string Name { get; init; }
public int TransactionsCount { get; init; }
}
private IQueryable<StoreModel> stores = null!;
private PaginationState pagination = new();
[SupplyParameterFromQuery]
public string? Search { get; set; }
protected override void OnParametersSet()
{
var storesQuery = DbContext.Stores.AsQueryable();
if (!string.IsNullOrEmpty(Search))
{
var searchPattern = $"%{Search}%";
storesQuery = storesQuery
.Where(store => EF.Functions.ILike(store.Retailer!.Name + ' ' + store.Name, searchPattern));
}
stores = storesQuery
.Select(store => new StoreModel
{
Id = store.Id,
Retailer = store.Retailer!.Name,
Name = store.Name,
TransactionsCount = store.Transactions!.Count(),
})
.OrderBy(store => store.Retailer)
.ThenBy(store => store.Name);
}
}