using DrinkRateAPI.DbEntities; using Microsoft.EntityFrameworkCore; namespace DrinkRateAPI.Contexts; public class ApplicationDbContext : DbContext { 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 Users { get; set; } public DbSet UserCompanyTableStats { get; set; } public DbSet UserProductTableStats { 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) { // 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.UserId, cr.CompanyId }); entity.HasOne(cr => cr.User) .WithMany(u => u.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.Users) .WithMany(u => u.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.UserId, pr.ProductId }); entity.HasOne(pr => pr.User) .WithMany(u => u.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.Users) .WithMany(u => u.ProductTableViews); }); // User modelBuilder.Entity(entity => { entity.HasKey(u => u.Id); entity.HasMany(u => u.CompanyTableViews) .WithMany(ctv => ctv.Users); entity.HasMany(u => u.ProductTableViews) .WithMany(ptv => ptv.Users); entity.HasMany(u => u.CompanyRatings) .WithOne(cr => cr.User) .HasForeignKey(cr => cr.UserId); entity.HasMany(u => u.ProductRatings) .WithOne(pr => pr.User) .HasForeignKey(pr => pr.UserId); entity.HasMany(u => u.UserCompanyTableStats) .WithOne(ucts => ucts.User) .HasForeignKey(ucts => ucts.UserId); entity.HasMany(u => u.UserProductTableStats) .WithOne(upts => upts.User) .HasForeignKey(upts => upts.UserId); entity.HasIndex(u => u.UserName) .IsUnique(); }); // User Company Table Stat modelBuilder.Entity(entity => { entity.HasKey(ucts => new { ucts.UserId, ucts.CompanyTableId }); entity.HasOne(ucts => ucts.User) .WithMany(u => u.UserCompanyTableStats); entity.HasOne(ucts => ucts.CompanyTable) .WithMany(); }); // User Product Table Stat modelBuilder.Entity(entity => { entity.HasKey(upts => new { upts.UserId, upts.ProductTableId }); entity.HasOne(upts => upts.User) .WithMany(u => u.UserProductTableStats); entity.HasOne(upts => upts.ProductTable) .WithMany(); }); } }