@using Groceries.Data
@using Microsoft.EntityFrameworkCore
@layout Layout
@inject AppDbContext DbContext
Groceries – Stores
@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 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);
}
}