using Microsoft.EntityFrameworkCore; using ROLAC.API.Entities.Logging; namespace ROLAC.API.Data.Logging; /// /// Single source of truth for the SystemLog / AuditLog table schema. Applied by /// (so the startup migration creates the tables) AND by /// (so runtime reads/writes map to the same shape). /// public static class LogModelConfiguration { public static void Configure(ModelBuilder builder) { builder.Entity(entity => { entity.ToTable("SystemLogs"); entity.HasKey(e => e.Id); entity.Property(e => e.Level).HasConversion(); entity.Property(e => e.Category).HasMaxLength(256).IsRequired(); entity.Property(e => e.Message).IsRequired(); // text entity.Property(e => e.RequestPath).HasMaxLength(2048); entity.Property(e => e.HttpMethod).HasMaxLength(10); entity.Property(e => e.UserId).HasMaxLength(450); entity.Property(e => e.IpAddress).HasMaxLength(45); entity.Property(e => e.CorrelationId).HasMaxLength(64); entity.HasIndex(e => e.Timestamp); entity.HasIndex(e => e.Level); entity.HasIndex(e => new { e.Timestamp, e.Level }); entity.HasIndex(e => e.UserId).HasFilter("\"UserId\" IS NOT NULL"); }); builder.Entity(entity => { entity.ToTable("AuditLogs"); entity.HasKey(e => e.Id); entity.Property(e => e.Level).HasConversion(); entity.Property(e => e.Action).HasMaxLength(40).IsRequired(); entity.Property(e => e.Category).HasMaxLength(40).IsRequired(); entity.Property(e => e.EntityName).HasMaxLength(128); entity.Property(e => e.EntityId).HasMaxLength(64); entity.Property(e => e.Changes).HasColumnType("jsonb"); entity.Property(e => e.Summary).HasMaxLength(512); entity.Property(e => e.UserId).HasMaxLength(450); entity.Property(e => e.UserEmail).HasMaxLength(256); entity.Property(e => e.IpAddress).HasMaxLength(45); entity.Property(e => e.CorrelationId).HasMaxLength(64); entity.HasIndex(e => e.Timestamp); entity.HasIndex(e => new { e.Category, e.Timestamp }); entity.HasIndex(e => new { e.EntityName, e.EntityId }); entity.HasIndex(e => e.Action); entity.HasIndex(e => e.UserId).HasFilter("\"UserId\" IS NOT NULL"); }); } }