| [dfe03b8] | 1 | using Microsoft.EntityFrameworkCore;
|
|---|
| 2 | using StockMaster.Models;
|
|---|
| 3 | using StockMaster.ViewModels;
|
|---|
| 4 | using Microsoft.AspNetCore.Http;
|
|---|
| 5 |
|
|---|
| 6 | namespace StockMaster.Data
|
|---|
| 7 | {
|
|---|
| 8 | public class StockDbContext : DbContext
|
|---|
| 9 | {
|
|---|
| 10 | private readonly IHttpContextAccessor _httpContextAccessor;
|
|---|
| 11 |
|
|---|
| 12 | public StockDbContext(DbContextOptions<StockDbContext> options, IHttpContextAccessor httpContextAccessor) : base(options)
|
|---|
| 13 | {
|
|---|
| 14 | _httpContextAccessor = httpContextAccessor;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | public DbSet<User> Users { get; set; }
|
|---|
| 18 | public DbSet<Customer> Customers { get; set; }
|
|---|
| 19 | public DbSet<Category> Categories { get; set; }
|
|---|
| 20 | public DbSet<Supplier> Suppliers { get; set; }
|
|---|
| 21 | public DbSet<Product> Products { get; set; }
|
|---|
| 22 | public DbSet<Warehouse> Warehouses { get; set; }
|
|---|
| 23 | public DbSet<Sale> Sales { get; set; }
|
|---|
| 24 | public DbSet<PurchaseOrder> PurchaseOrders { get; set; }
|
|---|
| 25 | public DbSet<SaleItem> SaleItems { get; set; }
|
|---|
| 26 | public DbSet<PurchaseOrderItem> PurchaseOrderItems { get; set; }
|
|---|
| 27 | public DbSet<WarehouseStock> WarehouseStocks { get; set; }
|
|---|
| 28 | public DbSet<ProductPriceLog> ProductPriceLogs { get; set; }
|
|---|
| 29 |
|
|---|
| 30 | public DbSet<VwSalesByDay> VwSalesByDays { get; set; }
|
|---|
| 31 | public DbSet<VwEmployeeRanking> VwEmployeeRankings { get; set; }
|
|---|
| 32 | public DbSet<VwTodaysSummary> VwTodaysSummaries { get; set; }
|
|---|
| 33 |
|
|---|
| 34 | protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|---|
| 35 | {
|
|---|
| 36 | base.OnModelCreating(modelBuilder);
|
|---|
| 37 |
|
|---|
| 38 | modelBuilder.Entity<SaleItem>().HasKey(si => new { si.SaleId, si.ProductId });
|
|---|
| 39 | modelBuilder.Entity<PurchaseOrderItem>().HasKey(poi => new { poi.PoId, poi.ProductId });
|
|---|
| 40 | modelBuilder.Entity<WarehouseStock>().HasKey(ws => new { ws.WarehouseId, ws.ProductId });
|
|---|
| 41 |
|
|---|
| 42 | modelBuilder.Entity<Product>().Property(p => p.UnitPrice).HasPrecision(12, 2);
|
|---|
| 43 | modelBuilder.Entity<Sale>().Property(s => s.TotalAmount).HasPrecision(15, 2);
|
|---|
| 44 | modelBuilder.Entity<SaleItem>().Property(si => si.UnitPriceAtSale).HasPrecision(12, 2);
|
|---|
| 45 | modelBuilder.Entity<PurchaseOrderItem>().Property(poi => poi.UnitCost).HasPrecision(12, 2);
|
|---|
| 46 |
|
|---|
| 47 | modelBuilder.Entity<ProductPriceLog>().ToTable("product_price_log", "stock_management");
|
|---|
| 48 |
|
|---|
| 49 | modelBuilder.Entity<VwSalesByDay>().HasNoKey().ToView("vw_sales_by_day_of_week", "stock_management");
|
|---|
| 50 | modelBuilder.Entity<VwEmployeeRanking>().HasNoKey().ToView("vw_employee_sales_ranking", "stock_management");
|
|---|
| 51 | modelBuilder.Entity<VwTodaysSummary>().HasNoKey().ToView("vw_todays_sales_summary", "stock_management");
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|---|
| 57 | {
|
|---|
| 58 | var username = _httpContextAccessor.HttpContext?.User?.Identity?.Name ?? "system";
|
|---|
| 59 |
|
|---|
| 60 | await Database.ExecuteSqlRawAsync("SELECT set_config('app.current_user', {0}, false)", new[] { username }, cancellationToken);
|
|---|
| 61 |
|
|---|
| 62 | return await base.SaveChangesAsync(cancellationToken);
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | } |
|---|