Skip to content

Commit ddae19f

Browse files
separate seed data from migrations
1 parent e3beca7 commit ddae19f

File tree

10 files changed

+425
-107
lines changed

10 files changed

+425
-107
lines changed

UserManagement.Contracts/Models/Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Module
99
[MaxLength(100)] public string Area { get; set; } = null!;
1010
[MaxLength(100)] public string Controller { get; set; } = null!;
1111
[MaxLength(100)] public string Action { get; set; } = null!;
12-
[MaxLength(100)] public string Type { get; set; } = null!;
12+
[MaxLength(100)] public string Type { get; set; } = "WebApp";
1313
public int CategoryId { get; set; }
1414
public Category Category { get; set; } = null!;
1515
public ICollection<Function> Functions { get; set; } = new List<Function>();

UserManagementApi/DbSeeder.cs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace UserManagementApi
77
{
88
public static class DbSeeder
99
{
10-
public static void Seed(IServiceProvider serviceProvider)
10+
public static void SeedCore(IServiceProvider serviceProvider)
1111
{
1212
using var scope = serviceProvider.CreateScope();
1313
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
@@ -77,5 +77,81 @@ public static void Seed(IServiceProvider serviceProvider)
7777

7878
context.SaveChanges();
7979
}
80+
public static void SeedFeatureApiPermissions(IServiceProvider serviceProvider)
81+
{
82+
using var scope = serviceProvider.CreateScope();
83+
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
84+
85+
// 1) Ensure Administration category
86+
var adminCat = db.Categories
87+
.FirstOrDefault(c => c.Name == "Administration");
88+
if (adminCat == null)
89+
{
90+
adminCat = new Category { Name = "Administration" };
91+
db.Categories.Add(adminCat);
92+
db.SaveChanges();
93+
}
94+
95+
// 2) Ensure ApiLogs module
96+
var apiLogs = db.Modules.FirstOrDefault(m =>
97+
m.Name == "ApiLogs" &&
98+
m.Controller == "ErrorLogs" &&
99+
m.Action == "GetAllErrors");
100+
101+
if (apiLogs == null)
102+
{
103+
apiLogs = new Module
104+
{
105+
Name = "ApiLogs",
106+
Area = "",
107+
Controller = "ErrorLogs",
108+
Action = "GetAllErrors",
109+
CategoryId = adminCat.Id,
110+
Type = "Api"
111+
};
112+
db.Modules.Add(apiLogs);
113+
db.SaveChanges();
114+
}
115+
116+
// 3) Ensure function
117+
if (!db.Functions.Any(f => f.Code == "Api.Logs.View"))
118+
{
119+
db.Functions.Add(new Function
120+
{
121+
Code = "Api.Logs.View",
122+
DisplayName = "Api View Logs",
123+
ModuleId = apiLogs.Id
124+
});
125+
db.SaveChanges();
126+
}
127+
128+
// 4) Ensure allan user
129+
var allan = db.Users.FirstOrDefault(u => u.UserName == "allan");
130+
if (allan == null)
131+
{
132+
var hash = BCrypt.Net.BCrypt.HashPassword("allan");
133+
allan = new AppUser
134+
{
135+
UserName = "allan",
136+
Password = hash
137+
};
138+
db.Users.Add(allan);
139+
db.SaveChanges();
140+
}
141+
142+
// 5) Ensure allan ↔ Admin role
143+
var adminRole = db.Roles.FirstOrDefault(r => r.Name == "Admin");
144+
if (adminRole != null &&
145+
!db.UserRoles.Any(ur => ur.UserId == allan.Id && ur.RoleId == adminRole.Id))
146+
{
147+
db.UserRoles.Add(new UserRole
148+
{
149+
UserId = allan.Id,
150+
RoleId = adminRole.Id
151+
});
152+
db.SaveChanges();
153+
}
154+
}
80155
}
156+
81157
}

UserManagementApi/Migrations/20251101102948_AddModuleTypeAndApiLogsSeed.cs

Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -10,117 +10,23 @@ public partial class AddModuleTypeAndApiLogsSeed : Migration
1010
/// <inheritdoc />
1111
protected override void Up(MigrationBuilder migrationBuilder)
1212
{
13-
// 1) Add column as nullable first
1413
migrationBuilder.AddColumn<string>(
15-
name: "Type",
16-
table: "Modules",
17-
type: "nvarchar(100)",
18-
maxLength: 100,
19-
nullable: true
20-
);
21-
22-
// 2) Backfill existing rows
23-
migrationBuilder.Sql(@"
24-
UPDATE Modules SET Type = 'WebApp' WHERE Type IS NULL;
25-
");
26-
27-
// 3) Make it required (non-nullable) after backfill
28-
migrationBuilder.AlterColumn<string>(
2914
name: "Type",
3015
table: "Modules",
3116
type: "nvarchar(100)",
3217
maxLength: 100,
3318
nullable: false,
34-
oldClrType: typeof(string),
35-
oldType: "nvarchar(100)",
36-
oldMaxLength: 100,
37-
oldNullable: true);
38-
39-
// 4) Seed Category -> Module -> Function (idempotent)
40-
migrationBuilder.Sql(@"
41-
DECLARE @catId INT;
42-
43-
-- Ensure Administration category exists
44-
SELECT @catId = Id FROM Categories WHERE Name = 'Administration';
45-
46-
-- Ensure ApiLogs module exists (Area='', Controller='ErrorLogs', Action='GetAllErrors')
47-
IF NOT EXISTS (
48-
SELECT 1 FROM Modules
49-
WHERE Name = 'ApiLogs' AND Controller = 'ErrorLogs' AND Action = 'GetAllErrors'
50-
)
51-
BEGIN
52-
INSERT INTO Modules ([Name],[Area],[Controller],[Action],[CategoryId],[Type])
53-
VALUES ('ApiLogs','', 'ErrorLogs','GetAllErrors', @catId, 'Api');
54-
END
55-
56-
DECLARE @modId INT;
57-
SELECT @modId = Id FROM Modules
58-
WHERE Name='ApiLogs' AND Controller='ErrorLogs' AND Action='GetAllErrors';
59-
60-
-- Ensure function Api.Logs.View exists
61-
IF NOT EXISTS (SELECT 1 FROM Functions WHERE [Code] = 'Api.Logs.View')
62-
BEGIN
63-
INSERT INTO Functions ([Code],[DisplayName],[ModuleId])
64-
VALUES ('Api.Logs.View','Api View Logs', @modId);
65-
END
66-
");
67-
68-
69-
// add allan if missing (hash created here once & inlined)
70-
var hash = BCrypt.Net.BCrypt.HashPassword("allan");
71-
migrationBuilder.Sql($@"
72-
IF NOT EXISTS (SELECT 1 FROM [Users] WHERE [UserName]='allan')
73-
BEGIN
74-
INSERT INTO [Users]([UserName],[Password]) VALUES ('allan','{hash}');
75-
END
76-
");
77-
78-
// link allan ↔ Operator if missing
79-
migrationBuilder.Sql(@"
80-
DECLARE @uid INT = (SELECT TOP 1 [Id] FROM [Users] WHERE [UserName]='allan');
81-
DECLARE @rid INT = (SELECT TOP 1 [Id] FROM [Roles] WHERE [Name]='Admin');
82-
83-
IF @uid IS NOT NULL AND @rid IS NOT NULL
84-
AND NOT EXISTS(SELECT 1 FROM [UserRoles] WHERE [UserId]=@uid AND [RoleId]=@rid)
85-
BEGIN
86-
INSERT INTO [UserRoles]([UserId],[RoleId]) VALUES (@uid,@rid);
87-
END
88-
");
89-
19+
defaultValue: "WebApp"
20+
);
9021
}
9122

9223
protected override void Down(MigrationBuilder migrationBuilder)
93-
{
94-
// Remove function
95-
migrationBuilder.Sql(@"
96-
DELETE F
97-
FROM Functions F
98-
WHERE F.[Code] = 'Api.Logs.View';
99-
");
100-
101-
// Remove module (only the one we added)
102-
migrationBuilder.Sql(@"
103-
DELETE M
104-
FROM Modules M
105-
WHERE M.[Name] = 'ApiLogs' AND M.[Controller] = 'ErrorLogs' AND M.[Action] = 'GetAllErrors';
106-
");
107-
24+
{
10825
// Drop the Type column
10926
migrationBuilder.DropColumn(
11027
name: "Type",
11128
table: "Modules"
11229
);
113-
114-
// Remove allan user and its roles
115-
migrationBuilder.Sql(@"
116-
DECLARE @uid INT = (SELECT TOP 1 [Id] FROM [Users] WHERE [UserName]='allan');
117-
IF @uid IS NOT NULL
118-
BEGIN
119-
DELETE FROM [UserRoles] WHERE [UserId]=@uid;
120-
DELETE FROM [Users] WHERE [Id]=@uid;
121-
END
122-
");
12330
}
124-
12531
}
12632
}

0 commit comments

Comments
 (0)