using GameList.Domain; using Microsoft.EntityFrameworkCore; namespace GameList.Data; public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Players => Set(); public DbSet Suggestions => Set(); public DbSet Votes => Set(); public DbSet AppState => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(builder => { builder.HasKey(p => p.Id); builder.Property(p => p.DisplayName).HasMaxLength(64); builder.HasMany(p => p.Suggestions) .WithOne(s => s.Player!) .HasForeignKey(s => s.PlayerId) .OnDelete(DeleteBehavior.Cascade); builder.HasMany(p => p.Votes) .WithOne(v => v.Player!) .HasForeignKey(v => v.PlayerId) .OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity(builder => { builder.HasKey(s => s.Id); builder.Property(s => s.Name).IsRequired().HasMaxLength(100); builder.Property(s => s.Genre).HasMaxLength(50); builder.Property(s => s.Description).HasMaxLength(500); builder.Property(s => s.ScreenshotUrl).HasMaxLength(2048); builder.Property(s => s.YoutubeUrl).HasMaxLength(2048); }); modelBuilder.Entity(builder => { builder.HasKey(v => v.Id); builder.Property(v => v.Score).IsRequired(); builder.HasIndex(v => new { v.PlayerId, v.SuggestionId }).IsUnique(); }); modelBuilder.Entity(builder => { builder.HasKey(s => s.Id); builder.HasData(new AppState { Id = 1, CurrentPhase = Phase.Suggest, UpdatedAt = DateTimeOffset.UnixEpoch }); }); } }