|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using UserManagementApi.Models; |
| 3 | + |
| 4 | +namespace UserManagementApi.Data |
| 5 | +{ |
| 6 | + public class AppDbContext : DbContext |
| 7 | + { |
| 8 | + public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } |
| 9 | + |
| 10 | + public DbSet<Category> Categories => Set<Category>(); |
| 11 | + public DbSet<Module> Modules => Set<Module>(); |
| 12 | + public DbSet<Function> Functions => Set<Function>(); |
| 13 | + public DbSet<Role> Roles => Set<Role>(); |
| 14 | + public DbSet<AppUser> Users => Set<AppUser>(); |
| 15 | + public DbSet<UserRole> UserRoles => Set<UserRole>(); |
| 16 | + public DbSet<RoleFunction> RoleFunctions => Set<RoleFunction>(); |
| 17 | + |
| 18 | + protected override void OnModelCreating(ModelBuilder b) |
| 19 | + { |
| 20 | + base.OnModelCreating(b); |
| 21 | + |
| 22 | + // Keys for join entities |
| 23 | + b.Entity<UserRole>().HasKey(x => new { x.UserId, x.RoleId }); |
| 24 | + b.Entity<RoleFunction>().HasKey(x => new { x.RoleId, x.FunctionId }); |
| 25 | + |
| 26 | + // Relationships |
| 27 | + b.Entity<Module>() |
| 28 | + .HasOne(m => m.Category) |
| 29 | + .WithMany(c => c.Modules) |
| 30 | + .HasForeignKey(m => m.CategoryId) |
| 31 | + .OnDelete(DeleteBehavior.Cascade); |
| 32 | + |
| 33 | + b.Entity<Function>() |
| 34 | + .HasOne(f => f.Module) |
| 35 | + .WithMany(m => m.Functions) |
| 36 | + .HasForeignKey(f => f.ModuleId) |
| 37 | + .OnDelete(DeleteBehavior.Cascade); |
| 38 | + |
| 39 | + b.Entity<UserRole>() |
| 40 | + .HasOne(ur => ur.User) |
| 41 | + .WithMany(u => u.UserRoles) |
| 42 | + .HasForeignKey(ur => ur.UserId); |
| 43 | + |
| 44 | + b.Entity<UserRole>() |
| 45 | + .HasOne(ur => ur.Role) |
| 46 | + .WithMany(r => r.UserRoles) |
| 47 | + .HasForeignKey(ur => ur.RoleId); |
| 48 | + |
| 49 | + b.Entity<RoleFunction>() |
| 50 | + .HasOne(rf => rf.Role) |
| 51 | + .WithMany(r => r.RoleFunctions) |
| 52 | + .HasForeignKey(rf => rf.RoleId); |
| 53 | + |
| 54 | + b.Entity<RoleFunction>() |
| 55 | + .HasOne(rf => rf.Function) |
| 56 | + .WithMany(f => f.RoleFunctions) |
| 57 | + .HasForeignKey(rf => rf.FunctionId); |
| 58 | + |
| 59 | + // ---------- Seed Data ---------- |
| 60 | + // Categories |
| 61 | + b.Entity<Category>().HasData( |
| 62 | + new Category { Id = 1, Name = "Administration" }, |
| 63 | + new Category { Id = 2, Name = "Operations" } |
| 64 | + ); |
| 65 | + |
| 66 | + // Modules |
| 67 | + b.Entity<Module>().HasData( |
| 68 | + new Module { Id = 1, Name = "User Management", Area = "Admin", Controller = "Users", Action = "Index", CategoryId = 1 }, |
| 69 | + new Module { Id = 2, Name = "Role Management", Area = "Admin", Controller = "Roles", Action = "Index", CategoryId = 1 }, |
| 70 | + new Module { Id = 3, Name = "Payments", Area = "Ops", Controller = "Payments", Action = "Index", CategoryId = 2 } |
| 71 | + ); |
| 72 | + |
| 73 | + // Functions |
| 74 | + b.Entity<Function>().HasData( |
| 75 | + new Function { Id = 1, ModuleId = 1, Code = "Users.View", DisplayName = "View Users" }, |
| 76 | + new Function { Id = 2, ModuleId = 1, Code = "Users.Edit", DisplayName = "Edit Users" }, |
| 77 | + new Function { Id = 3, ModuleId = 2, Code = "Roles.View", DisplayName = "View Roles" }, |
| 78 | + new Function { Id = 4, ModuleId = 2, Code = "Roles.Assign", DisplayName = "Assign Roles" }, |
| 79 | + new Function { Id = 5, ModuleId = 3, Code = "Payments.View", DisplayName = "View Payments" } |
| 80 | + ); |
| 81 | + |
| 82 | + // Roles |
| 83 | + b.Entity<Role>().HasData( |
| 84 | + new Role { Id = 1, Name = "Admin" }, |
| 85 | + new Role { Id = 2, Name = "Operator" } |
| 86 | + ); |
| 87 | + |
| 88 | + // Users |
| 89 | + b.Entity<AppUser>().HasData( |
| 90 | + new AppUser { Id = 1, UserName = "alice" }, |
| 91 | + new AppUser { Id = 2, UserName = "bob" } |
| 92 | + ); |
| 93 | + |
| 94 | + // User ↔ Role |
| 95 | + b.Entity<UserRole>().HasData( |
| 96 | + new UserRole { UserId = 1, RoleId = 1 }, // alice → Admin |
| 97 | + new UserRole { UserId = 2, RoleId = 2 } // bob → Operator |
| 98 | + ); |
| 99 | + |
| 100 | + // Role ↔ Function |
| 101 | + b.Entity<RoleFunction>().HasData( |
| 102 | + // Admin gets everything |
| 103 | + new RoleFunction { RoleId = 1, FunctionId = 1 }, |
| 104 | + new RoleFunction { RoleId = 1, FunctionId = 2 }, |
| 105 | + new RoleFunction { RoleId = 1, FunctionId = 3 }, |
| 106 | + new RoleFunction { RoleId = 1, FunctionId = 4 }, |
| 107 | + new RoleFunction { RoleId = 1, FunctionId = 5 }, |
| 108 | + |
| 109 | + // Operator gets limited |
| 110 | + new RoleFunction { RoleId = 2, FunctionId = 1 }, // Users.View |
| 111 | + new RoleFunction { RoleId = 2, FunctionId = 5 } // Payments.View |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments