Skip to content

Commit 2f1eab6

Browse files
Migratons added and seed data updated
1 parent b66f6d8 commit 2f1eab6

File tree

7 files changed

+798
-62
lines changed

7 files changed

+798
-62
lines changed

UserManagementApi/Data/AppDbContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
1818
protected override void OnModelCreating(ModelBuilder b)
1919
{
2020
base.OnModelCreating(b);
21+
2122

2223
// Keys for join entities
2324
b.Entity<UserRole>().HasKey(x => new { x.UserId, x.RoleId });
@@ -55,8 +56,6 @@ protected override void OnModelCreating(ModelBuilder b)
5556
.HasOne(rf => rf.Function)
5657
.WithMany(f => f.RoleFunctions)
5758
.HasForeignKey(rf => rf.FunctionId);
58-
59-
6059
}
6160
}
6261
}

UserManagementApi/DbSeeder.cs

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,70 @@ public static void Seed(IServiceProvider serviceProvider)
1111
using var scope = serviceProvider.CreateScope();
1212
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
1313

14-
// Apply any pending migrations
14+
// Apply pending migrations
1515
context.Database.Migrate();
1616

17-
// Only seed if DB is empty (idempotent)
18-
var categories = new List<Category>
19-
{
20-
new Category { Id = 1, Name = "Administration" },
21-
new Category { Id = 2, Name = "Operations" }
22-
};
17+
// Only seed once
18+
if (context.Users.Any() || context.Roles.Any() || context.Categories.Any())
19+
return;
2320

24-
var modules = new List<Module>
25-
{
26-
new Module { Id = 1, Name = "User Management", Area = "Admin", Controller = "Users", Action = "Index", CategoryId = 1 },
27-
new Module { Id = 2, Name = "Role Management", Area = "Admin", Controller = "Roles", Action = "Index", CategoryId = 1 },
28-
new Module { Id = 3, Name = "Payments", Area = "Ops", Controller = "Payments", Action = "Index", CategoryId = 2 }
29-
};
21+
// ----- Categories -----
22+
var adminCat = new Category { Name = "Administration" };
23+
var opsCat = new Category { Name = "Operations" };
24+
context.Categories.AddRange(adminCat, opsCat);
25+
context.SaveChanges(); // get IDs
3026

31-
var functions = new List<Function>
32-
{
33-
new Function { Id = 1, ModuleId = 1, Code = "Users.View", DisplayName = "View Users" },
34-
new Function { Id = 2, ModuleId = 1, Code = "Users.Edit", DisplayName = "Edit Users" },
35-
new Function { Id = 3, ModuleId = 2, Code = "Roles.View", DisplayName = "View Roles" },
36-
new Function { Id = 4, ModuleId = 2, Code = "Roles.Assign", DisplayName = "Assign Roles" },
37-
new Function { Id = 5, ModuleId = 3, Code = "Payments.View", DisplayName = "View Payments" }
38-
};
39-
40-
var roles = new List<Role>
41-
{
42-
new Role { Id = 1, Name = "Admin" },
43-
new Role { Id = 2, Name = "Operator" }
44-
};
27+
// ----- Modules (link via navigation) -----
28+
var modUsers = new Module { Name = "User Management", Area = "Admin", Controller = "Users", Action = "Index", Category = adminCat };
29+
var modRoles = new Module { Name = "Role Management", Area = "Admin", Controller = "Roles", Action = "Index", Category = adminCat };
30+
var modPay = new Module { Name = "Payments", Area = "Ops", Controller = "Payments", Action = "Index", Category = opsCat };
31+
context.Modules.AddRange(modUsers, modRoles, modPay);
32+
context.SaveChanges();
4533

46-
var users = new List<AppUser>
47-
{
48-
new AppUser { Id = 1, UserName = "alice", Password = BCrypt.Net.BCrypt.HashPassword("alice") },
49-
new AppUser { Id = 2, UserName = "bob", Password = BCrypt.Net.BCrypt.HashPassword("boob") }
50-
};
34+
// ----- Functions (link via navigation) -----
35+
var fUsersView = new Function { Module = modUsers, Code = "Users.View", DisplayName = "View Users" };
36+
var fUsersEdit = new Function { Module = modUsers, Code = "Users.Edit", DisplayName = "Edit Users" };
37+
var fRolesView = new Function { Module = modRoles, Code = "Roles.View", DisplayName = "View Roles" };
38+
var fRolesAssign = new Function { Module = modRoles, Code = "Roles.Assign", DisplayName = "Assign Roles" };
39+
var fPayView = new Function { Module = modPay, Code = "Payments.View", DisplayName = "View Payments" };
40+
context.Functions.AddRange(fUsersView, fUsersEdit, fRolesView, fRolesAssign, fPayView);
41+
context.SaveChanges();
5142

52-
var userRoles = new List<UserRole>
53-
{
54-
new UserRole { UserId = 1, RoleId = 1 }, // alice → Admin
55-
new UserRole { UserId = 2, RoleId = 2 } // bob → Operator
56-
};
43+
// ----- Roles -----
44+
var adminRole = new Role { Name = "Admin" };
45+
var operatorRole = new Role { Name = "Operator" };
46+
context.Roles.AddRange(adminRole, operatorRole);
47+
context.SaveChanges();
5748

58-
var roleFunctions = new List<RoleFunction>
59-
{
60-
// Admin gets everything
61-
new RoleFunction { RoleId = 1, FunctionId = 1 },
62-
new RoleFunction { RoleId = 1, FunctionId = 2 },
63-
new RoleFunction { RoleId = 1, FunctionId = 3 },
64-
new RoleFunction { RoleId = 1, FunctionId = 4 },
65-
new RoleFunction { RoleId = 1, FunctionId = 5 },
49+
// ----- Users (hashed passwords) -----
50+
var alice = new AppUser { UserName = "alice", Password = BCrypt.Net.BCrypt.HashPassword("alice") };
51+
var bob = new AppUser { UserName = "bob", Password = BCrypt.Net.BCrypt.HashPassword("bob") };
52+
context.Users.AddRange(alice, bob);
53+
context.SaveChanges();
6654

67-
// Operator gets limited
68-
new RoleFunction { RoleId = 2, FunctionId = 1 }, // Users.View
69-
new RoleFunction { RoleId = 2, FunctionId = 5 } // Payments.View
70-
};
55+
// ----- User ↔ Role (use entity refs, not IDs) -----
56+
context.UserRoles.AddRange(
57+
new UserRole { User = alice, Role = adminRole },
58+
new UserRole { User = bob, Role = operatorRole }
59+
);
60+
context.SaveChanges();
7161

72-
// Add and save
73-
context.Categories.AddRange(categories);
74-
context.Modules.AddRange(modules);
75-
context.Functions.AddRange(functions);
76-
context.Roles.AddRange(roles);
77-
context.Users.AddRange(users);
78-
context.UserRoles.AddRange(userRoles);
79-
context.RoleFunctions.AddRange(roleFunctions);
62+
// ----- Role ↔ Function (use refs) -----
63+
// Admin → all
64+
context.RoleFunctions.AddRange(
65+
new RoleFunction { Role = adminRole, Function = fUsersView },
66+
new RoleFunction { Role = adminRole, Function = fUsersEdit },
67+
new RoleFunction { Role = adminRole, Function = fRolesView },
68+
new RoleFunction { Role = adminRole, Function = fRolesAssign },
69+
new RoleFunction { Role = adminRole, Function = fPayView }
70+
);
71+
// Operator → limited
72+
context.RoleFunctions.AddRange(
73+
new RoleFunction { Role = operatorRole, Function = fUsersView },
74+
new RoleFunction { Role = operatorRole, Function = fPayView }
75+
);
8076

8177
context.SaveChanges();
82-
8378
}
8479
}
8580
}

0 commit comments

Comments
 (0)