|
| 1 | +using Microsoft.EntityFrameworkCore; |
| 2 | +using UserManagementApi.Data; |
| 3 | +using UserManagementApi.Models; |
| 4 | + |
| 5 | +namespace UserManagementApi |
| 6 | +{ |
| 7 | + public static class DbSeeder |
| 8 | + { |
| 9 | + public static void Seed(IServiceProvider serviceProvider) |
| 10 | + { |
| 11 | + using var scope = serviceProvider.CreateScope(); |
| 12 | + var context = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 13 | + |
| 14 | + // Apply any pending migrations |
| 15 | + context.Database.Migrate(); |
| 16 | + |
| 17 | + // Only seed if DB is empty (idempotent) |
| 18 | + if (!context.Categories.Any()) |
| 19 | + { |
| 20 | + var categories = new List<Category> |
| 21 | + { |
| 22 | + new Category { Id = 1, Name = "Administration" }, |
| 23 | + new Category { Id = 2, Name = "Operations" } |
| 24 | + }; |
| 25 | + |
| 26 | + var modules = new List<Module> |
| 27 | + { |
| 28 | + new Module { Id = 1, Name = "User Management", Area = "Admin", Controller = "Users", Action = "Index", CategoryId = 1 }, |
| 29 | + new Module { Id = 2, Name = "Role Management", Area = "Admin", Controller = "Roles", Action = "Index", CategoryId = 1 }, |
| 30 | + new Module { Id = 3, Name = "Payments", Area = "Ops", Controller = "Payments", Action = "Index", CategoryId = 2 } |
| 31 | + }; |
| 32 | + |
| 33 | + var functions = new List<Function> |
| 34 | + { |
| 35 | + new Function { Id = 1, ModuleId = 1, Code = "Users.View", DisplayName = "View Users" }, |
| 36 | + new Function { Id = 2, ModuleId = 1, Code = "Users.Edit", DisplayName = "Edit Users" }, |
| 37 | + new Function { Id = 3, ModuleId = 2, Code = "Roles.View", DisplayName = "View Roles" }, |
| 38 | + new Function { Id = 4, ModuleId = 2, Code = "Roles.Assign", DisplayName = "Assign Roles" }, |
| 39 | + new Function { Id = 5, ModuleId = 3, Code = "Payments.View", DisplayName = "View Payments" } |
| 40 | + }; |
| 41 | + |
| 42 | + var roles = new List<Role> |
| 43 | + { |
| 44 | + new Role { Id = 1, Name = "Admin" }, |
| 45 | + new Role { Id = 2, Name = "Operator" } |
| 46 | + }; |
| 47 | + |
| 48 | + var users = new List<AppUser> |
| 49 | + { |
| 50 | + new AppUser { Id = 1, UserName = "alice", Password = "alice" }, |
| 51 | + new AppUser { Id = 2, UserName = "bob", Password = "bob" } |
| 52 | + }; |
| 53 | + |
| 54 | + var userRoles = new List<UserRole> |
| 55 | + { |
| 56 | + new UserRole { UserId = 1, RoleId = 1 }, // alice → Admin |
| 57 | + new UserRole { UserId = 2, RoleId = 2 } // bob → Operator |
| 58 | + }; |
| 59 | + |
| 60 | + var roleFunctions = new List<RoleFunction> |
| 61 | + { |
| 62 | + // Admin gets everything |
| 63 | + new RoleFunction { RoleId = 1, FunctionId = 1 }, |
| 64 | + new RoleFunction { RoleId = 1, FunctionId = 2 }, |
| 65 | + new RoleFunction { RoleId = 1, FunctionId = 3 }, |
| 66 | + new RoleFunction { RoleId = 1, FunctionId = 4 }, |
| 67 | + new RoleFunction { RoleId = 1, FunctionId = 5 }, |
| 68 | + |
| 69 | + // Operator gets limited |
| 70 | + new RoleFunction { RoleId = 2, FunctionId = 1 }, // Users.View |
| 71 | + new RoleFunction { RoleId = 2, FunctionId = 5 } // Payments.View |
| 72 | + }; |
| 73 | + |
| 74 | + // Add and save |
| 75 | + context.Categories.AddRange(categories); |
| 76 | + context.Modules.AddRange(modules); |
| 77 | + context.Functions.AddRange(functions); |
| 78 | + context.Roles.AddRange(roles); |
| 79 | + context.Users.AddRange(users); |
| 80 | + context.UserRoles.AddRange(userRoles); |
| 81 | + context.RoleFunctions.AddRange(roleFunctions); |
| 82 | + |
| 83 | + context.SaveChanges(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments