using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Webshop.Domain.Entities; using Webshop.Domain.Identity; namespace Webshop.Infrastructure.Data { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } public DbSet Products { get; set; } = default!; public DbSet ProductVariants { get; set; } = default!; public DbSet categories { get; set; } = default!; public DbSet Customers { get; set; } = default!; public DbSet
Addresses { get; set; } = default!; public DbSet Orders { get; set; } = default!; public DbSet OrderItems { get; set; } = default!; public DbSet Reviews { get; set; } = default!; public DbSet Discounts { get; set; } = default!; public DbSet Suppliers { get; set; } = default!; public DbSet ShippingMethods { get; set; } = default!; public DbSet PaymentMethods { get; set; } = default!; public DbSet Settings { get; set; } = default!; // Verknüpfungstabellen und Details public DbSet Productcategories { get; set; } = default!; public DbSet ProductDiscounts { get; set; } = default!; public DbSet categorieDiscounts { get; set; } = default!; public DbSet ProductImages { get; set; } = default!; public DbSet ShopInfos { get; set; } = default!; // +++ NEU: Warenkorb Tabellen +++ public DbSet Carts { get; set; } = default!; public DbSet CartItems { get; set; } = default!; protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); foreach (var entity in modelBuilder.Model.GetEntityTypes()) { var tableName = entity.GetTableName(); if (tableName != null && tableName.StartsWith("AspNet")) { entity.SetTableName(tableName.Substring(6)); } } // +++ NEU: Warenkorb Konfiguration +++ modelBuilder.Entity(entity => { // Wenn ein Warenkorb gelöscht wird, lösche alle Items darin automatisch entity.HasMany(c => c.Items) .WithOne(i => i.Cart) .HasForeignKey(i => i.CartId) .OnDelete(DeleteBehavior.Cascade); }); // Bestehende Konfigurationen (bleiben unverändert) modelBuilder.Entity().HasKey(pc => new { pc.ProductId, pc.categorieId }); modelBuilder.Entity().HasKey(pd => new { pd.ProductId, pd.DiscountId }); modelBuilder.Entity().HasKey(cd => new { cd.categorieId, cd.DiscountId }); modelBuilder.Entity().HasIndex(p => p.SKU).IsUnique(); modelBuilder.Entity().HasIndex(p => p.Slug).IsUnique(); modelBuilder.Entity().HasIndex(c => c.Slug).IsUnique(); modelBuilder.Entity().HasIndex(d => d.CouponCode).IsUnique().HasFilter("\"CouponCode\" IS NOT NULL"); modelBuilder.Entity().HasIndex(s => s.Key).IsUnique(); modelBuilder.Entity().HasIndex(o => o.OrderNumber).IsUnique(); modelBuilder.Entity(e => { e.Property(p => p.Price).HasPrecision(18, 2); e.Property(p => p.OldPrice).HasPrecision(18, 2); e.Property(p => p.PurchasePrice).HasPrecision(18, 2); e.Property(p => p.Weight).HasPrecision(18, 3); e.Property(p => p.Width).HasPrecision(18, 2); e.Property(p => p.Height).HasPrecision(18, 2); e.Property(p => p.Length).HasPrecision(18, 2); }); modelBuilder.Entity(entity => { entity.ToTable("ProductImages"); entity.HasOne(pi => pi.Product) .WithMany(p => p.Images) .HasForeignKey(pi => pi.ProductId) .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity() .Property(pv => pv.PriceAdjustment).HasPrecision(18, 2); modelBuilder.Entity(e => { e.Property(o => o.OrderTotal).HasPrecision(18, 2); e.Property(o => o.ShippingCost).HasPrecision(18, 2); e.Property(o => o.TaxAmount).HasPrecision(18, 2); e.Property(o => o.DiscountAmount).HasPrecision(18, 2); }); modelBuilder.Entity(e => { e.Property(oi => oi.UnitPrice).HasPrecision(18, 2); e.Property(oi => oi.TotalPrice).HasPrecision(18, 2); }); modelBuilder.Entity(e => { e.Property(d => d.DiscountValue).HasPrecision(18, 2); e.Property(d => d.MinimumOrderAmount).HasPrecision(18, 2); }); modelBuilder.Entity(e => { e.Property(sm => sm.BaseCost).HasPrecision(18, 2); e.Property(sm => sm.MinimumOrderAmount).HasPrecision(18, 2); }); modelBuilder.Entity() .Property(pm => pm.ProcessingFee).HasPrecision(18, 2); modelBuilder.Entity() .HasOne(c => c.Parentcategorie) .WithMany(c => c.Subcategories) .HasForeignKey(c => c.ParentcategorieId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(oi => oi.Product) .WithMany() .HasForeignKey(oi => oi.ProductId) .OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity() .HasOne(oi => oi.ProductVariant) .WithMany() .HasForeignKey(oi => oi.ProductVariantId) .OnDelete(DeleteBehavior.SetNull); modelBuilder.Entity() .HasOne(o => o.BillingAddress) .WithMany() .HasForeignKey(o => o.BillingAddressId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(o => o.ShippingAddress) .WithMany() .HasForeignKey(o => o.ShippingAddressId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(a => a.Customer) .WithOne(c => c.User) .HasForeignKey(c => c.AspNetUserId); modelBuilder.Entity(entity => { entity.HasOne(pd => pd.Product) .WithMany(p => p.ProductDiscounts) .HasForeignKey(pd => pd.ProductId); entity.HasOne(pd => pd.Discount) .WithMany(d => d.ProductDiscounts) .HasForeignKey(pd => pd.DiscountId); }); modelBuilder.Entity(entity => { entity.HasOne(cd => cd.categorie) .WithMany(c => c.categorieDiscounts) .HasForeignKey(cd => cd.categorieId); entity.HasOne(cd => cd.Discount) .WithMany(d => d.categorieDiscounts) .HasForeignKey(cd => cd.DiscountId); }); modelBuilder.Entity(entity => { entity.HasOne(r => r.Product) .WithMany(p => p.Reviews) .HasForeignKey(r => r.ProductId) .OnDelete(DeleteBehavior.Cascade); entity.HasOne(r => r.Customer) .WithMany(c => c.Reviews) .HasForeignKey(r => r.CustomerId) .OnDelete(DeleteBehavior.SetNull); }); } } }