The ASP.NET Core solution (mws.api/mws.application/mws.domain/ mws.infrastructure) existed only as untracked working files. Adding it to version control, plus a .gitignore for build output and local tooling directories, so the CQRS/mediator refactor plan has a real git history to branch and diff against.
178 lines
7.0 KiB
C#
178 lines
7.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Mws.Application.Auth;
|
|
using Mws.Application.Permissions;
|
|
using Mws.Domain.Documents;
|
|
using Mws.Domain.Projects;
|
|
using Mws.Domain.Roles;
|
|
using Mws.Domain.Tasks;
|
|
using Mws.Domain.Users;
|
|
using TaskPriority = Mws.Domain.Tasks.TaskPriority;
|
|
using TaskStatus = Mws.Domain.Tasks.TaskStatus;
|
|
|
|
namespace Mws.Infrastructure.Persistence;
|
|
|
|
public static class DbSeeder
|
|
{
|
|
public static async Task SeedAsync(AppDbContext db, IPasswordHasher passwordHasher)
|
|
{
|
|
if (await db.Users.AnyAsync())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
var adminRole = new Role { Id = Guid.NewGuid(), Name = "Admin", IsSystem = true, CreatedAt = now, UpdatedAt = now };
|
|
var memberRole = new Role { Id = Guid.NewGuid(), Name = "Member", IsSystem = true, CreatedAt = now, UpdatedAt = now };
|
|
|
|
adminRole.Permissions = ScreenCatalog.Screens.Select(s => new RolePermission
|
|
{
|
|
RoleId = adminRole.Id, Screen = s.Key, CanView = true, CanCreate = true, CanEdit = true, CanDelete = true,
|
|
}).ToList();
|
|
|
|
memberRole.Permissions = ScreenCatalog.Screens
|
|
.Where(s => s.Key is not ("accounts" or "roles"))
|
|
.Select(s => new RolePermission
|
|
{
|
|
RoleId = memberRole.Id, Screen = s.Key, CanView = true, CanCreate = true, CanEdit = true, CanDelete = false,
|
|
}).ToList();
|
|
|
|
db.Roles.AddRange(adminRole, memberRole);
|
|
await db.SaveChangesAsync();
|
|
|
|
var admin = new User
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Username = "admin",
|
|
PasswordHash = passwordHasher.Hash("password"),
|
|
DisplayName = "Admin",
|
|
RoleId = adminRole.Id,
|
|
CreatedAt = now,
|
|
UpdatedAt = now,
|
|
};
|
|
|
|
var alice = new User
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Username = "alice",
|
|
PasswordHash = passwordHasher.Hash("password"),
|
|
DisplayName = "Alice",
|
|
RoleId = memberRole.Id,
|
|
CreatedAt = now,
|
|
UpdatedAt = now,
|
|
};
|
|
|
|
var bob = new User
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Username = "bob",
|
|
PasswordHash = passwordHasher.Hash("password"),
|
|
DisplayName = "Bob",
|
|
RoleId = memberRole.Id,
|
|
CreatedAt = now,
|
|
UpdatedAt = now,
|
|
};
|
|
|
|
db.Users.AddRange(admin, alice, bob);
|
|
await db.SaveChangesAsync();
|
|
|
|
var project = new Project
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Name = "MWS",
|
|
Description = "My Workspace - internal tool for the team.",
|
|
Status = ProjectStatus.Active,
|
|
CreatedAt = now,
|
|
UpdatedAt = now,
|
|
};
|
|
|
|
db.Projects.Add(project);
|
|
|
|
db.ProjectMembers.AddRange(
|
|
new ProjectMember { ProjectId = project.Id, UserId = admin.Id, Role = MemberRole.Owner },
|
|
new ProjectMember { ProjectId = project.Id, UserId = alice.Id, Role = MemberRole.Member },
|
|
new ProjectMember { ProjectId = project.Id, UserId = bob.Id, Role = MemberRole.Member });
|
|
|
|
db.ProjectMemberPermissions.AddRange(
|
|
new ProjectMemberPermission { ProjectId = project.Id, UserId = alice.Id, Screen = ProjectPermissionScreens.Documents, CanView = true },
|
|
new ProjectMemberPermission { ProjectId = project.Id, UserId = bob.Id, Screen = ProjectPermissionScreens.Documents, CanView = true });
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
var requirements = new Document
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, ParentId = null,
|
|
Title = "Requirements", Type = DocumentType.Folder,
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedBy = admin.Id, UpdatedAt = now,
|
|
};
|
|
|
|
var design = new Document
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, ParentId = null,
|
|
Title = "Technical Design", Type = DocumentType.Folder,
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedBy = admin.Id, UpdatedAt = now,
|
|
};
|
|
|
|
var notes = new Document
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, ParentId = null,
|
|
Title = "Notes", Type = DocumentType.Folder,
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedBy = admin.Id, UpdatedAt = now,
|
|
};
|
|
|
|
db.Documents.AddRange(requirements, design, notes);
|
|
db.Documents.Add(new Document
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, ParentId = requirements.Id,
|
|
Title = "Authentication", Type = DocumentType.Document,
|
|
Content = "<h2>Authentication</h2><p>Phase 1 uses simple username/password with JWT.</p>",
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedBy = admin.Id, UpdatedAt = now,
|
|
});
|
|
db.Documents.Add(new Document
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, ParentId = requirements.Id,
|
|
Title = "User Management", Type = DocumentType.Document,
|
|
Content = "<h2>User Management</h2><p>Users are created via seeding in Phase 1.</p>",
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedBy = admin.Id, UpdatedAt = now,
|
|
});
|
|
db.Documents.Add(new Document
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, ParentId = design.Id,
|
|
Title = "API Design", Type = DocumentType.Document,
|
|
Content = "<h2>API Design</h2><p>REST API with ASP.NET Core Web API.</p>",
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedBy = admin.Id, UpdatedAt = now,
|
|
});
|
|
await db.SaveChangesAsync();
|
|
|
|
db.Tasks.AddRange(
|
|
new TaskItem
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, Title = "Set up solution",
|
|
Status = TaskStatus.Done, Priority = TaskPriority.High, AssigneeId = admin.Id,
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedAt = now,
|
|
},
|
|
new TaskItem
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, Title = "Implement authentication",
|
|
Description = "Login endpoint with BCrypt and JWT.",
|
|
Status = TaskStatus.InProgress, Priority = TaskPriority.High, AssigneeId = alice.Id,
|
|
DueDate = now.AddDays(3),
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedAt = now,
|
|
},
|
|
new TaskItem
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, Title = "Task board drag & drop",
|
|
Status = TaskStatus.Todo, Priority = TaskPriority.Medium, AssigneeId = bob.Id,
|
|
DueDate = now.AddDays(7),
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedAt = now,
|
|
},
|
|
new TaskItem
|
|
{
|
|
Id = Guid.NewGuid(), ProjectId = project.Id, Title = "Polish documents UI",
|
|
Status = TaskStatus.Todo, Priority = TaskPriority.Low,
|
|
CreatedBy = admin.Id, CreatedAt = now, UpdatedAt = now,
|
|
});
|
|
|
|
await db.SaveChangesAsync();
|
|
}
|
|
} |