Fix error loading Add/Edit Transaction Item page
All checks were successful
Docker Image CI / build (push) Successful in 3m8s
All checks were successful
Docker Image CI / build (push) Successful in 3m8s
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
@using Groceries.Data
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
|
||||
@inject AppDbContext DbContext
|
||||
@inject IDbContextFactory<AppDbContext> DbContextFactory
|
||||
|
||||
<form method="post" @attributes="AdditionalAttributes">
|
||||
<div class="form-field">
|
||||
@ -45,7 +45,8 @@
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
retailers = await DbContext.Retailers
|
||||
using var dbContext = DbContextFactory.CreateDbContext();
|
||||
retailers = await dbContext.Retailers
|
||||
.OrderBy(retailer => retailer.Name)
|
||||
.ToArrayAsync();
|
||||
}
|
||||
|
@ -8,11 +8,11 @@ using Microsoft.EntityFrameworkCore;
|
||||
[Route("/stores")]
|
||||
public class StoresController : Controller
|
||||
{
|
||||
private readonly AppDbContext dbContext;
|
||||
private readonly IDbContextFactory<AppDbContext> dbContextFactory;
|
||||
|
||||
public StoresController(AppDbContext dbContext)
|
||||
public StoresController(IDbContextFactory<AppDbContext> dbContextFactory)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@ -32,6 +32,8 @@ public class StoresController : Controller
|
||||
[HttpPost("new")]
|
||||
public async Task<IResult> NewStore(Guid retailerId, string name, string? address)
|
||||
{
|
||||
using var dbContext = dbContextFactory.CreateDbContext();
|
||||
|
||||
var store = new Store(retailerId, name, address);
|
||||
dbContext.Stores.Add(store);
|
||||
|
||||
@ -45,6 +47,8 @@ public class StoresController : Controller
|
||||
[HttpGet("edit/{id}")]
|
||||
public async Task<IResult> EditStore(Guid id)
|
||||
{
|
||||
using var dbContext = dbContextFactory.CreateDbContext();
|
||||
|
||||
var store = await dbContext.Stores
|
||||
.SingleOrDefaultAsync(store => store.Id == id, HttpContext.RequestAborted);
|
||||
|
||||
@ -61,6 +65,8 @@ public class StoresController : Controller
|
||||
[HttpPost("edit/{id}")]
|
||||
public async Task<IResult> EditStore(Guid id, Guid retailerId, string name, string? address, string? returnUrl)
|
||||
{
|
||||
using var dbContext = dbContextFactory.CreateDbContext();
|
||||
|
||||
var store = new Store(id, retailerId, name, address);
|
||||
dbContext.Stores.Update(store);
|
||||
|
||||
|
@ -2,7 +2,9 @@
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
|
||||
@layout Layout
|
||||
@inject AppDbContext DbContext
|
||||
|
||||
@implements IDisposable
|
||||
@inject IDbContextFactory<AppDbContext> DbContextFactory
|
||||
|
||||
<PageTitle>Groceries – Stores</PageTitle>
|
||||
|
||||
@ -39,6 +41,7 @@
|
||||
public int TransactionsCount { get; init; }
|
||||
}
|
||||
|
||||
private AppDbContext? dbContext;
|
||||
private IQueryable<StoreModel> stores = null!;
|
||||
private PaginationState pagination = new();
|
||||
|
||||
@ -47,7 +50,9 @@
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
var storesQuery = DbContext.Stores.AsQueryable();
|
||||
dbContext ??= DbContextFactory.CreateDbContext();
|
||||
|
||||
var storesQuery = dbContext.Stores.AsQueryable();
|
||||
if (!string.IsNullOrEmpty(Search))
|
||||
{
|
||||
var searchPattern = $"%{Search}%";
|
||||
@ -66,4 +71,9 @@
|
||||
.OrderBy(store => store.Retailer)
|
||||
.ThenBy(store => store.Name);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
dbContext?.Dispose();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user