122 lines
5.0 KiB
C#
122 lines
5.0 KiB
C#
using maintenance.Data.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace maintenance.Data;
|
|
|
|
public sealed class AppDbContext : DbContext
|
|
{
|
|
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<TemplateEntity> Templates => Set<TemplateEntity>();
|
|
public DbSet<AppMappingEntity> AppMappings => Set<AppMappingEntity>();
|
|
public DbSet<FeatureFlagEntity> FeatureFlags => Set<FeatureFlagEntity>();
|
|
public DbSet<MaintenanceWindowEntity> MaintenanceWindows => Set<MaintenanceWindowEntity>();
|
|
public DbSet<AdminGroupEntity> AdminGroups => Set<AdminGroupEntity>();
|
|
public DbSet<AuthorizationGroupEntity> AuthorizationGroups => Set<AuthorizationGroupEntity>();
|
|
public DbSet<PermissionRuleEntity> PermissionRules => Set<PermissionRuleEntity>();
|
|
public DbSet<AuditRecordEntity> AuditRecords => Set<AuditRecordEntity>();
|
|
public DbSet<ManagedPageEntity> ManagedPages => Set<ManagedPageEntity>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<TemplateEntity>(e =>
|
|
{
|
|
e.ToTable("Template");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.Name).HasMaxLength(50).IsRequired();
|
|
e.Property(x => x.Title).HasMaxLength(200).IsRequired();
|
|
e.Property(x => x.LastUpdatedBy).HasMaxLength(200).IsRequired();
|
|
e.HasIndex(x => x.Name).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<AppMappingEntity>(e =>
|
|
{
|
|
e.ToTable("AppMapping");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.AppId).HasMaxLength(100).IsRequired();
|
|
e.Property(x => x.DisplayName).HasMaxLength(200).IsRequired();
|
|
e.Property(x => x.DefaultTemplate).HasMaxLength(50).IsRequired();
|
|
e.HasIndex(x => x.AppId).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<FeatureFlagEntity>(e =>
|
|
{
|
|
e.ToTable("FeatureFlag");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.Key).HasMaxLength(100).IsRequired();
|
|
e.Property(x => x.Scope).HasMaxLength(50).IsRequired();
|
|
e.HasIndex(x => new { x.Key, x.Scope }).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<MaintenanceWindowEntity>(e =>
|
|
{
|
|
e.ToTable("MaintenanceWindow");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.AppId).HasMaxLength(100);
|
|
e.HasIndex(x => new { x.AppId, x.StartTimeEastern, x.EndTimeEastern });
|
|
});
|
|
|
|
modelBuilder.Entity<AdminGroupEntity>(e =>
|
|
{
|
|
e.ToTable("AdminGroup");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.GroupClaimValue).HasMaxLength(256).IsRequired();
|
|
e.Property(x => x.NormalizedGroupClaimValue).HasMaxLength(256).IsRequired();
|
|
e.HasIndex(x => x.NormalizedGroupClaimValue).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<AuthorizationGroupEntity>(e =>
|
|
{
|
|
e.ToTable("AuthorizationGroup");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.GroupClaimValue).HasMaxLength(256).IsRequired();
|
|
e.Property(x => x.Role).HasMaxLength(50).IsRequired();
|
|
e.HasIndex(x => x.GroupClaimValue).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<PermissionRuleEntity>(e =>
|
|
{
|
|
e.ToTable("PermissionRule");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.PermissionKey).HasMaxLength(200).IsRequired();
|
|
e.Property(x => x.Effect).HasMaxLength(10).IsRequired();
|
|
e.HasOne(x => x.Group)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.GroupId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
e.HasIndex(x => new { x.PermissionKey, x.GroupId }).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<AuditRecordEntity>(e =>
|
|
{
|
|
e.ToTable("AuditRecord");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.Actor).HasMaxLength(200).IsRequired();
|
|
e.Property(x => x.Action).HasMaxLength(100).IsRequired();
|
|
e.Property(x => x.Target).HasMaxLength(200).IsRequired();
|
|
e.HasIndex(x => x.TimestampUtc);
|
|
});
|
|
|
|
modelBuilder.Entity<ManagedPageEntity>(e =>
|
|
{
|
|
e.ToTable("ManagedPage");
|
|
e.HasKey(x => x.Id);
|
|
e.Property(x => x.Key).HasMaxLength(100).IsRequired();
|
|
e.Property(x => x.Stub).HasMaxLength(100);
|
|
e.Property(x => x.NormalizedStub).HasMaxLength(100);
|
|
e.Property(x => x.DisplayName).HasMaxLength(200).IsRequired();
|
|
e.Property(x => x.PageKind).HasMaxLength(50).IsRequired();
|
|
e.Property(x => x.RecurrenceRule).HasMaxLength(200);
|
|
e.Property(x => x.LastUpdatedBy).HasMaxLength(200).IsRequired();
|
|
e.Property(x => x.ContentJson).IsRequired();
|
|
e.HasIndex(x => x.Key).IsUnique();
|
|
e.HasIndex(x => new { x.PageKind, x.NormalizedStub })
|
|
.IsUnique()
|
|
.HasFilter("[NormalizedStub] IS NOT NULL");
|
|
e.HasIndex(x => new { x.PageKind, x.IsEnabled, x.Priority });
|
|
});
|
|
}
|
|
}
|