groceries/Groceries/Transactions/TransactionsPage.razor
James Chapman 47d13ba922
All checks were successful
Docker Image CI / build (push) Successful in 3m45s
Add support for adding 'loose' items to transactions
2024-10-12 02:36:38 +01:00

59 lines
2.3 KiB
Plaintext

@using Groceries.Data
@layout Layout
@inject AppDbContext DbContext
<PageTitle>Groceries &ndash; Transactions</PageTitle>
<header class="row">
<h1 class="row__fill">Transactions</h1>
<a class="button button--primary form-field" href="/transactions/new">New transaction</a>
</header>
<section class="table">
<Table Items="transactions" Pagination="pagination" HeaderClass="table__header--shaded">
<PropertyTableColumn Property="t => t.CreatedAt" Title="Date" Sortable="true" Context="createdAt">
<time datetime="@createdAt.ToString("o")">@createdAt.ToLongDateString()</time>
</PropertyTableColumn>
<PropertyTableColumn Property="t => t.Store" Fill="true" Sortable="true" />
<PropertyTableColumn Property="t => t.TotalItems" Title="Items" Sortable="true" />
<PropertyTableColumn Property="t => t.TotalAmount" Title="Amount" Format="c" Sortable="true" />
@* <TemplateTableColumn>
<a class="link" href="/transactions/@context.Id">View</a>
</TemplateTableColumn> *@
</Table>
<TablePaginator State="pagination" />
</section>
@code {
private record TransactionModel
{
public Guid Id { get; init; }
public DateTime CreatedAt { get; init; }
public required string Store { get; init; }
public decimal TotalAmount { get; init; }
public int TotalItems { get; init; }
}
private IQueryable<TransactionModel> transactions = null!;
private PaginationState pagination = new();
protected override void OnParametersSet()
{
transactions = DbContext.Transactions
.Join(
DbContext.TransactionTotals,
transaction => transaction.Id,
transactionTotal => transactionTotal.TransactionId,
(transaction, transactionTotal) => new TransactionModel
{
Id = transaction.Id,
CreatedAt = transaction.CreatedAt,
Store = string.Concat(transaction.Store!.Retailer!.Name, " ", transaction.Store.Name),
TotalAmount = transactionTotal.Total,
TotalItems = transaction.Items.Sum(item => item.Unit == null ? (int)item.Quantity : 1),
})
.OrderByDescending(transaction => transaction.CreatedAt);
}
}