214 lines
No EOL
6.5 KiB
C#
214 lines
No EOL
6.5 KiB
C#
using DrinkRateAPI.DbEntities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DrinkRateAPI.Contexts;
|
|
|
|
public class ApplicationDbContext : DbContext
|
|
{
|
|
public DbSet<DbCompany> Companies { get; set; }
|
|
public DbSet<DbCompanyRating> CompanyRatings { get; set; }
|
|
public DbSet<DbCompanyTable> CompanyTables { get; set; }
|
|
public DbSet<DbCompanyTableView> CompanyTableViews { get; set; }
|
|
|
|
public DbSet<DbProduct> Product { get; set; }
|
|
public DbSet<DbProductRating> ProductRating { get; set; }
|
|
public DbSet<DbProductTable> ProductTable { get; set; }
|
|
public DbSet<DbProductTableView> ProductTableView { get; set; }
|
|
|
|
public DbSet<DbUser> Users { get; set; }
|
|
public DbSet<DbUserCompanyTableStat> UserCompanyTableStats { get; set; }
|
|
public DbSet<DbUserProductTableStat> 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<DbCompany>(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<DbCompanyRating>(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<DbCompanyTable>(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<DbCompanyTableView>(entity =>
|
|
{
|
|
entity.HasKey(ctv => ctv.Id);
|
|
|
|
entity.HasMany(ctv => ctv.CompanyTables)
|
|
.WithMany();
|
|
|
|
entity.HasMany(ctv => ctv.Users)
|
|
.WithMany(u => u.CompanyTableViews);
|
|
});
|
|
|
|
// Product
|
|
modelBuilder.Entity<DbProduct>(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<DbProductRating>(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<DbProductTable>(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<DbProductTableView>(entity =>
|
|
{
|
|
entity.HasKey(ptv => ptv.Id);
|
|
|
|
entity.HasMany(ptv => ptv.ProductTables)
|
|
.WithMany();
|
|
|
|
entity.HasMany(ptv => ptv.Users)
|
|
.WithMany(u => u.ProductTableViews);
|
|
});
|
|
|
|
// User
|
|
modelBuilder.Entity<DbUser>(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<DbUserCompanyTableStat>(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<DbUserProductTableStat>(entity =>
|
|
{
|
|
entity.HasKey(upts => new { upts.UserId, upts.ProductTableId });
|
|
|
|
entity.HasOne(upts => upts.User)
|
|
.WithMany(u => u.UserProductTableStats);
|
|
|
|
entity.HasOne(upts => upts.ProductTable)
|
|
.WithMany();
|
|
});
|
|
}
|
|
} |