using DrinkRateAPI.DbEntities; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace DrinkRateAPI.Contexts; public class ApplicationDbContext : IdentityDbContext { public DbSet Companies { get; set; } public DbSet CompanyRatings { get; set; } public DbSet CompanyTables { get; set; } public DbSet CompanyTableViews { get; set; } public DbSet Product { get; set; } public DbSet ProductRating { get; set; } public DbSet ProductTable { get; set; } public DbSet ProductTableView { get; set; } public DbSet UserProfiles { get; set; } public DbSet UserProfileCompanyTableStats { get; set; } public DbSet UserProfileProductTableStats { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); optionsBuilder.UseNpgsql(configuration.GetConnectionString($"Db{environment}")); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // Company modelBuilder.Entity(entity => { entity.HasKey(c => c.Id); entity.HasMany(c => c.Products) .WithOne(p => p.Company) .HasForeignKey(p => p.CompanyId); entity.HasMany(c => c.CompanyRatings) .WithOne(cr => cr.Company) .HasForeignKey(cr => cr.CompanyId); entity.Property(c => c.CompanyName) .IsRequired() .HasMaxLength(50); entity.HasIndex(c => c.CompanyName) .IsUnique(); // to do: index by Sum / Count }); // Company Rating modelBuilder.Entity(entity => { entity.HasKey(cr => new { cr.UserProfileId, cr.CompanyId }); entity.HasOne(cr => cr.UserProfile) .WithMany(up => up.CompanyRatings); entity.HasOne(cr => cr.Company) .WithMany(c => c.CompanyRatings); }); // Company Table modelBuilder.Entity(entity => { entity.HasKey(ct => ct.Id); entity.HasMany(ct => ct.Companies) .WithOne(c => c.CompanyTable) .HasForeignKey(c => c.CompanyTableId); entity.HasIndex(ct => ct.CompanyTableName) .IsUnique(); }); // Company Table View modelBuilder.Entity(entity => { entity.HasKey(ctv => ctv.Id); entity.HasMany(ctv => ctv.CompanyTables) .WithMany(); entity.HasMany(ctv => ctv.UserProfiles) .WithMany(up => up.CompanyTableViews); }); // Product modelBuilder.Entity(entity => { entity.HasKey(p => p.Id); entity.HasOne(p => p.ProductTable) .WithMany(pt => pt.Products) .HasForeignKey(p => p.ProductTableId); entity.HasOne(p => p.Company) .WithMany(c => c.Products) .HasForeignKey(p => p.CompanyId); entity.HasMany(p => p.ProductRatings) .WithOne(pr => pr.Product) .HasForeignKey(pr => pr.ProductId); entity.Property(p => p.ProductName) .IsRequired() .HasMaxLength(50); entity.HasIndex(c => c.ProductName) .IsUnique(); // to do: index by Sum / Count }); // Product Rating modelBuilder.Entity(entity => { entity.HasKey(pr => new { pr.UserProfileId, pr.ProductId }); entity.HasOne(pr => pr.UserProfile) .WithMany(up => up.ProductRatings); entity.HasOne(pr => pr.Product) .WithMany(p => p.ProductRatings); }); // Product Table modelBuilder.Entity(entity => { entity.HasKey(pt => pt.Id); entity.HasMany(pt => pt.Products) .WithOne(p => p.ProductTable) .HasForeignKey(p => p.ProductTableId); entity.HasIndex(pt => pt.ProductTableName) .IsUnique(); }); // Product Table View modelBuilder.Entity(entity => { entity.HasKey(ptv => ptv.Id); entity.HasMany(ptv => ptv.ProductTables) .WithMany(); entity.HasMany(ptv => ptv.UserProfiles) .WithMany(u => u.ProductTableViews); }); // User modelBuilder.Entity(entity => { entity.HasKey(up => up.Id); entity.HasMany(up => up.CompanyTableViews) .WithMany(ctv => ctv.UserProfiles); entity.HasMany(up => up.ProductTableViews) .WithMany(ptv => ptv.UserProfiles); entity.HasMany(up => up.CompanyRatings) .WithOne(cr => cr.UserProfile) .HasForeignKey(cr => cr.UserProfileId); entity.HasMany(up => up.ProductRatings) .WithOne(pr => pr.UserProfile) .HasForeignKey(pr => pr.UserProfileId); entity.HasMany(up => up.UserProfileCompanyTableStats) .WithOne(upcts => upcts.UserProfile) .HasForeignKey(upcts => upcts.UserId); entity.HasMany(up => up.UserProfileProductTableStats) .WithOne(uppts => uppts.UserProfile) .HasForeignKey(uppts => uppts.UserId); entity.HasIndex(up => up.UserName) .IsUnique(); entity.HasOne(up => up.ApplicationUser) .WithOne(au => au.UserProfile) .HasForeignKey(up => up.ApplicationUserId); }); // User Company Table Stat modelBuilder.Entity(entity => { entity.HasKey(upcts => new { upcts.UserId, upcts.CompanyTableId }); entity.HasOne(upcts => upcts.UserProfile) .WithMany(up => up.UserProfileCompanyTableStats); entity.HasOne(upcts => upcts.CompanyTable) .WithMany(); }); // User Product Table Stat modelBuilder.Entity(entity => { entity.HasKey(uppts => new { uppts.UserId, uppts.ProductTableId }); entity.HasOne(uppts => uppts.UserProfile) .WithMany(up => up.UserProfileProductTableStats); entity.HasOne(uppts => uppts.ProductTable) .WithMany(); }); } }