All checks were successful
Docker Image CI / build (push) Successful in 8m8s
89 lines
3.0 KiB
Plaintext
89 lines
3.0 KiB
Plaintext
@using Groceries.Data
|
|
@using Microsoft.EntityFrameworkCore
|
|
|
|
@layout Layout
|
|
|
|
@implements IDisposable
|
|
@inject IDbContextFactory<AppDbContext> DbContextFactory
|
|
|
|
<PageTitle>Groceries – Items</PageTitle>
|
|
|
|
<header class="row">
|
|
<h1 class="row__fill">Items</h1>
|
|
<search title="Items">
|
|
<SearchForm data-turbo-frame="table" data-turbo-action="advance">
|
|
<input type="hidden" name="page" value="1" />
|
|
</SearchForm>
|
|
</search>
|
|
</header>
|
|
|
|
<turbo-frame id="table" target="top">
|
|
<section class="table">
|
|
<Table Items="items" Pagination="pagination" HeaderClass="table__header--shaded">
|
|
<PropertyTableColumn Property="i => i.Brand" Sortable="true" />
|
|
<PropertyTableColumn Property="i => i.Name" Fill="true" Sortable="true" />
|
|
<PropertyTableColumn Property="i => i.LastPurchasedAt" Title="Last Purchased" Sortable="true">
|
|
<time datetime="@context?.ToString("o")">@context?.ToLongDateString()</time>
|
|
</PropertyTableColumn>
|
|
<PropertyTableColumn Property="i => i.HasBarcode" Title="Barcode" Align="Align.Center" Sortable="true">
|
|
<span class="icon icon--sm">@(context ? "✓" : "")</span>
|
|
</PropertyTableColumn>
|
|
@* <TemplateTableColumn>
|
|
<a class="link" href="/items/edit/@context.Id">Edit</a>
|
|
</TemplateTableColumn> *@
|
|
</Table>
|
|
<TablePaginator State="pagination" />
|
|
</section>
|
|
</turbo-frame>
|
|
|
|
@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<ItemModel> 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();
|
|
}
|
|
}
|