@using Groceries.Data @using Microsoft.EntityFrameworkCore @layout Layout @implements IDisposable @inject IDbContextFactory DbContextFactory Groceries – Items

Items

@(context ? "✓" : "") @* Edit *@
@code { private record ItemModel { public Guid Id { get; init; } public required string Brand { get; init; } public required string Name { get; init; } public bool HasBarcode { get; init; } public DateTime? LastPurchasedAt { get; init; } } private AppDbContext? dbContext; private IQueryable items = null!; private PaginationState pagination = new(); [SupplyParameterFromQuery] public string? Search { get; set; } protected override void OnParametersSet() { dbContext ??= DbContextFactory.CreateDbContext(); var itemsQuery = dbContext.Items.AsQueryable(); if (!string.IsNullOrEmpty(Search)) { var searchPattern = $"%{Search}%"; itemsQuery = itemsQuery.Where(item => EF.Functions.ILike(item.Brand + ' ' + item.Name, searchPattern)); } items = itemsQuery .LeftJoin( dbContext.ItemPurchases.Where(purchase => purchase.IsLastPurchase), item => item.Id, purchase => purchase.ItemId, (item, lastPurchase) => new ItemModel { Id = item.Id, Brand = item.Brand, Name = item.Name, HasBarcode = item.Barcodes.Count > 0, LastPurchasedAt = lastPurchase != null ? lastPurchase.CreatedAt : null, }) .OrderBy(item => item.Brand) .ThenBy(item => item.Name); } public void Dispose() { dbContext?.Dispose(); } }