Skip to content

Commit c50a98b

Browse files
Seed data added
1 parent a246c80 commit c50a98b

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

UserManagementApi/Controllers/UsersController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ public async Task<ActionResult<UserPermissionsDto>> GetPermissions(int userId)
4545
return Ok(dto);
4646
}
4747
}
48+
}

UserManagementApi/DbSeeder.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

UserManagementApi/Models/AppUser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class AppUser
66
{
77
public int Id { get; set; }
88
[MaxLength(120)] public string UserName { get; set; } = null!;
9+
[MaxLength(200)] public string Password { get; set; } = null!; // 🔑 new field
910
public ICollection<UserRole> UserRoles { get; set; } = new List<UserRole>();
1011
}
1112
}

UserManagementApi/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,7 @@
7272

7373
app.MapControllers();
7474

75+
// Seed sample data
76+
DbSeeder.Seed(app.Services);
77+
7578
app.Run();

0 commit comments

Comments
 (0)