diff --git a/DrinkRateAPI/Contexts/ApplicationDbContext.cs b/DrinkRateAPI/Contexts/ApplicationDbContext.cs index 2942ec9..df3475b 100644 --- a/DrinkRateAPI/Contexts/ApplicationDbContext.cs +++ b/DrinkRateAPI/Contexts/ApplicationDbContext.cs @@ -90,5 +90,69 @@ public class ApplicationDbContext : DbContext 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); + }); } } \ No newline at end of file