using CandyHouseBase.Infrastructure; using CandyHouseDataBase.Models; using Microsoft.EntityFrameworkCore; namespace CandyHouseDataBase; public class CandyHouseDbContext(IConfigurationDatabase configurationDatabase) : DbContext { private readonly IConfigurationDatabase? _configurationDatabase = configurationDatabase; protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString); base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // Uniqueness for Ingredient modelBuilder.Entity() .HasIndex(x => x.Name) .IsUnique(); // Uniqueness for Product (only non-deleted) modelBuilder.Entity() .HasIndex(x => new { x.Name, x.IsDeleted }) .IsUnique() .HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE"); // Composite key for Recipe modelBuilder.Entity() .HasKey(x => new { x.ProductId, x.IngredientId }); // Uniqueness for Position (Title for active records) modelBuilder.Entity() .HasIndex(x => new { x.Title, x.IsActual }) .IsUnique() .HasFilter($"\"{nameof(Position.IsActual)}\" = TRUE"); modelBuilder.Entity() .HasIndex(x => new { x.PositionId, x.IsActual }) .IsUnique() .HasFilter($"\"{nameof(Position.IsActual)}\" = TRUE"); // Uniqueness for Order modelBuilder.Entity() .HasIndex(x => new { x.PekarId, x.OrderDate }) .IsUnique(); } public DbSet Ingredients { get; set; } public DbSet Products { get; set; } public DbSet Recipes { get; set; } public DbSet Positions { get; set; } public DbSet Pekars { get; set; } public DbSet PekarHistories { get; set; } public DbSet Salaries { get; set; } public DbSet Orders { get; set; } }