using Microsoft.EntityFrameworkCore; using Tiku.Domain.Catalog; using Tiku.Domain.Common; using Tiku.Domain.Identity; using Tiku.Domain.Learning; using Tiku.Domain.QuestionBanks; using Tiku.Domain.Tenancy; namespace Tiku.Infrastructure.Persistence; public sealed class TikuDbContext(DbContextOptions options) : DbContext(options) { public DbSet Tenants => Set(); public DbSet Users => Set(); public DbSet UserIdentities => Set(); public DbSet TenantMemberships => Set(); public DbSet StudentProfiles => Set(); public DbSet Regions => Set(); public DbSet RegionModules => Set(); public DbSet ModuleNodes => Set(); public DbSet Schools => Set(); public DbSet Majors => Set(); public DbSet Subjects => Set(); public DbSet Categories => Set(); public DbSet QuestionBanks => Set(); public DbSet Questions => Set(); public DbSet QuestionVersions => Set(); public DbSet PracticeSessions => Set(); public DbSet AnswerRecords => Set(); public DbSet FavoriteQuestions => Set(); public DbSet WrongQuestions => Set(); public DbSet RecentPractices => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasPostgresExtension("citext"); modelBuilder.ApplyConfigurationsFromAssembly(typeof(TikuDbContext).Assembly); modelBuilder.UseSnakeCaseIdentifiers(); } public override int SaveChanges(bool acceptAllChangesOnSuccess) { UpdateTimestamps(); return base.SaveChanges(acceptAllChangesOnSuccess); } public override Task SaveChangesAsync( bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { UpdateTimestamps(); return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); } private void UpdateTimestamps() { var now = DateTimeOffset.UtcNow; foreach (var entry in ChangeTracker.Entries()) { if (entry.State == EntityState.Added) { entry.Entity.CreatedAt = now; } if (entry.State is EntityState.Added or EntityState.Modified) { entry.Entity.UpdatedAt = now; } } } }