Skip to content

Commit

Permalink
Plug in the EF Core Repositories and Test and Troubleshoot the App
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-liu-toronto committed Jun 7, 2024
1 parent 04d688e commit 38ce1c2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

namespace IMS.Plugins.EFCoreSqlServer
{
public class ProductEFCoreRespostiory : IProductRepository
public class ProductEFCoreRepository : IProductRepository
{
private readonly IDbContextFactory<IMSContext> contextFactory;

public ProductEFCoreRespostiory(IDbContextFactory<IMSContext> contextFactory)
public ProductEFCoreRepository(IDbContextFactory<IMSContext> contextFactory)
{
this.contextFactory = contextFactory;
}
Expand All @@ -40,7 +40,9 @@ public async Task DeleteProductByIdAsync(int productId)
public async Task<Product?> 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();
Expand All @@ -55,7 +57,9 @@ public async Task<IEnumerable<Product>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
[Parameter]
public EventCallback<Inventory> OnInventorySelected { get; set; }

private string _searchFilter;
private string _searchFilter = string.Empty;
private string searchFilter
{
get => _searchFilter;
Expand Down
21 changes: 17 additions & 4 deletions IMS.WebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -24,10 +25,22 @@
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

builder.Services.AddSingleton<IInventoryRepository, InventoryRepository>();
builder.Services.AddSingleton<IProductRepository, ProductRepository>();
builder.Services.AddSingleton<IInventoryTransactionRepository, InventoryTransactionRepository>();
builder.Services.AddSingleton<IProductTransactionRepository, ProductTransactionRepository>();
if (builder.Environment.IsEnvironment("Testing"))
{
StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);

builder.Services.AddSingleton<IInventoryRepository, InventoryRepository>();
builder.Services.AddSingleton<IProductRepository, ProductRepository>();
builder.Services.AddSingleton<IInventoryTransactionRepository, InventoryTransactionRepository>();
builder.Services.AddSingleton<IProductTransactionRepository, ProductTransactionRepository>();
}
else
{
builder.Services.AddTransient<IInventoryRepository, InventoryEFCoreRepository>();
builder.Services.AddTransient<IProductRepository, ProductEFCoreRepository>();
builder.Services.AddTransient<IInventoryTransactionRepository, InventoryTransactionEFCoreRepository>();
builder.Services.AddTransient<IProductTransactionRepository, ProductTransactionEFCoreRepository>();
}

builder.Services.AddTransient<IViewInventoriesByNameUseCase, ViewInventoriesByNameUseCase>();
builder.Services.AddTransient<IAddInventoryUseCase, AddInventoryUseCase>();
Expand Down
1 change: 1 addition & 0 deletions IMS.WebApp/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"DetailedErrors": true,
"ConnectionStrings": {
"InventoryManagement": "Data Source=(local);Initial Catalog=InventoryManagement;User ID=sa;Password=Password1;Trust Server Certificate=True"
},
Expand Down

0 comments on commit 38ce1c2

Please sign in to comment.