using Microsoft.EntityFrameworkCore; using TaskTracker.Core.Entities; namespace TaskTracker.Infrastructure.Data; public class TaskTrackerDbContext(DbContextOptions options) : DbContext(options) { public DbSet Tasks => Set(); public DbSet Notes => Set(); public DbSet ContextEvents => Set(); public DbSet AppMappings => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(e => { e.HasKey(t => t.Id); e.Property(t => t.Title).HasMaxLength(500).IsRequired(); e.Property(t => t.Status).HasConversion().HasMaxLength(50); e.Property(t => t.Category).HasMaxLength(100); e.HasMany(t => t.SubTasks).WithOne(t => t.ParentTask).HasForeignKey(t => t.ParentTaskId).OnDelete(DeleteBehavior.Restrict); e.HasIndex(t => t.ParentTaskId); e.HasMany(t => t.Notes).WithOne(n => n.WorkTask).HasForeignKey(n => n.WorkTaskId); e.HasMany(t => t.ContextEvents).WithOne(c => c.WorkTask).HasForeignKey(c => c.WorkTaskId); }); modelBuilder.Entity(e => { e.HasKey(n => n.Id); e.Property(n => n.Content).IsRequired(); e.Property(n => n.Type).HasConversion().HasMaxLength(50); }); modelBuilder.Entity(e => { e.HasKey(c => c.Id); e.Property(c => c.Source).HasMaxLength(100).IsRequired(); e.Property(c => c.AppName).HasMaxLength(200).IsRequired(); e.Property(c => c.WindowTitle).HasMaxLength(1000).IsRequired(); e.Property(c => c.Url).HasMaxLength(2000); e.HasIndex(c => c.Timestamp); }); modelBuilder.Entity(e => { e.HasKey(m => m.Id); e.Property(m => m.Pattern).HasMaxLength(500).IsRequired(); e.Property(m => m.MatchType).HasMaxLength(50).IsRequired(); e.Property(m => m.Category).HasMaxLength(100).IsRequired(); e.Property(m => m.FriendlyName).HasMaxLength(200); }); // Seed default app mappings modelBuilder.Entity().HasData( new AppMapping { Id = 1, Pattern = "SLDWORKS", MatchType = "ProcessName", Category = "Engineering", FriendlyName = "SolidWorks" }, new AppMapping { Id = 2, Pattern = "OUTLOOK", MatchType = "ProcessName", Category = "Email", FriendlyName = "Outlook" }, new AppMapping { Id = 3, Pattern = "notepad", MatchType = "ProcessName", Category = "General", FriendlyName = "Notepad" }, new AppMapping { Id = 4, Pattern = "pep", MatchType = "UrlContains", Category = "LaserCutting", FriendlyName = "PEP System" }, new AppMapping { Id = 5, Pattern = "solidworks", MatchType = "TitleContains", Category = "Engineering", FriendlyName = "SolidWorks" } ); } }