From 38ce1c2ea003ec3ebab9c35407deb91bedbe6ccb Mon Sep 17 00:00:00 2001 From: Frank Liu Date: Fri, 7 Jun 2024 12:15:48 -0400 Subject: [PATCH] Plug in the EF Core Repositories and Test and Troubleshoot the App --- ...postiory.cs => ProductEFCoreRepository.cs} | 12 +++++++---- .../SearchProductInventoriesComponent.razor | 2 +- IMS.WebApp/Program.cs | 21 +++++++++++++++---- IMS.WebApp/appsettings.Development.json | 1 + 4 files changed, 27 insertions(+), 9 deletions(-) rename IMS.Plugins/IMS.Plugins.EFCoreSqlServer/{ProductEFCoreRespostiory.cs => ProductEFCoreRepository.cs} (83%) diff --git a/IMS.Plugins/IMS.Plugins.EFCoreSqlServer/ProductEFCoreRespostiory.cs b/IMS.Plugins/IMS.Plugins.EFCoreSqlServer/ProductEFCoreRepository.cs similarity index 83% rename from IMS.Plugins/IMS.Plugins.EFCoreSqlServer/ProductEFCoreRespostiory.cs rename to IMS.Plugins/IMS.Plugins.EFCoreSqlServer/ProductEFCoreRepository.cs index 476ff62..5f899d2 100644 --- a/IMS.Plugins/IMS.Plugins.EFCoreSqlServer/ProductEFCoreRespostiory.cs +++ b/IMS.Plugins/IMS.Plugins.EFCoreSqlServer/ProductEFCoreRepository.cs @@ -9,11 +9,11 @@ namespace IMS.Plugins.EFCoreSqlServer { - public class ProductEFCoreRespostiory : IProductRepository + public class ProductEFCoreRepository : IProductRepository { private readonly IDbContextFactory contextFactory; - public ProductEFCoreRespostiory(IDbContextFactory contextFactory) + public ProductEFCoreRepository(IDbContextFactory contextFactory) { this.contextFactory = contextFactory; } @@ -40,7 +40,9 @@ public async Task DeleteProductByIdAsync(int productId) public async Task GetProductByIdAsync(int productId) { using var db = this.contextFactory.CreateDbContext(); - var product = await db.Products.FindAsync(productId); + var product = await db.Products.Include(x => x.ProductInventories) + .ThenInclude(x => x.Inventory) + .FirstOrDefaultAsync(x => x.ProductId == productId); if (product is not null) return product; return new Product(); @@ -55,7 +57,9 @@ public async Task> GetProductsByNameAsync(string name) public async Task UpdateProductAsync(Product product) { using var db = this.contextFactory.CreateDbContext(); - var prod = await db.Products.FindAsync(product.ProductId); + var prod = await db.Products + .Include(x => x.ProductInventories) + .FirstOrDefaultAsync(x => x.ProductId == product.ProductId); if (prod is not null) { prod.ProductName = product.ProductName; diff --git a/IMS.WebApp/Components/Controls/SearchProductInventoriesComponent.razor b/IMS.WebApp/Components/Controls/SearchProductInventoriesComponent.razor index 0a51e04..c210505 100644 --- a/IMS.WebApp/Components/Controls/SearchProductInventoriesComponent.razor +++ b/IMS.WebApp/Components/Controls/SearchProductInventoriesComponent.razor @@ -29,7 +29,7 @@ [Parameter] public EventCallback OnInventorySelected { get; set; } - private string _searchFilter; + private string _searchFilter = string.Empty; private string searchFilter { get => _searchFilter; diff --git a/IMS.WebApp/Program.cs b/IMS.WebApp/Program.cs index ed76aec..c1b7db6 100644 --- a/IMS.WebApp/Program.cs +++ b/IMS.WebApp/Program.cs @@ -10,6 +10,7 @@ using IMS.UseCases.Reports; using IMS.UseCases.Reports.interfaces; using IMS.WebApp.Components; +using Microsoft.AspNetCore.Hosting.StaticWebAssets; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); @@ -24,10 +25,22 @@ builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); +if (builder.Environment.IsEnvironment("Testing")) +{ + StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration); + + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); +} +else +{ + builder.Services.AddTransient(); + builder.Services.AddTransient(); + builder.Services.AddTransient(); + builder.Services.AddTransient(); +} builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/IMS.WebApp/appsettings.Development.json b/IMS.WebApp/appsettings.Development.json index fec8678..33146cc 100644 --- a/IMS.WebApp/appsettings.Development.json +++ b/IMS.WebApp/appsettings.Development.json @@ -1,4 +1,5 @@ { + "DetailedErrors": true, "ConnectionStrings": { "InventoryManagement": "Data Source=(local);Initial Catalog=InventoryManagement;User ID=sa;Password=Password1;Trust Server Certificate=True" },