Skip to content

feat:添加Admin后台管理#178

Merged
witskeeper merged 37 commits intonetcorepal:mainfrom
zhouda1fu:main
Jan 28, 2026
Merged

feat:添加Admin后台管理#178
witskeeper merged 37 commits intonetcorepal:mainfrom
zhouda1fu:main

Conversation

@zhouda1fu
Copy link
Copy Markdown
Contributor

No description provided.

Copy link
Copy Markdown
Member

@witskeeper witskeeper Jan 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

admin和demo code 相关的类文件应该在template.json中排除,无需在文件中做排除操作

@witskeeper
Copy link
Copy Markdown
Member

需要在.github/workflows/template-test.yml中添加对新的参数的测试条件

zhouda1fu and others added 25 commits January 21, 2026 23:08
- 删除 UseDemoCode 参数及所有相关代码和演示文件
- 为每个数据库/框架组合生成两套迁移文件(UseAdmin=true/false)
- 更新 template.json 条件规则,根据 UseAdmin 自动选择迁移文件
- 优化 CI/CD:合并 job,添加 use-admin 矩阵参数
- 更新文档,移除 UseDemoCode 相关说明
@witskeeper
Copy link
Copy Markdown
Member

witskeeper commented Jan 23, 2026

当admin=false 时报错,是因为没有任何Endpoint了,下面这行报错

builder.Services.AddFastEndpoints(o => o.IncludeAbstractValidators = true);

考虑放置一个默认的HelloEndpoint

�={"Timestamp":"2026-01-23T15:43:15.3167140+08:00","Level":"Fatal","MessageTemplate":"Application terminated unexpectedly","Exception":"System.InvalidOperationException: FastEndpoints was unable to find any endpoint declarations!\n at FastEndpoints.EndpointData..ctor(EndpointDiscoveryOptions options, CommandHandlerRegistry cmdHandlerRegistry)\n at FastEndpoints.MainExtensions.AddFastEndpoints(IServiceCollection services, Action`1 options)\n at Program.

$(String[] args) in /Users/xiao/TestProject/src/TestProject.Web/Program.cs:line 91"}

使用 -D MongoDB --UseAdmin true 生成时,此前存在事务、导航属性、跨集合查询、
Command 持久化等问题。本次通过模板条件编译 //#if (UseMongoDB) 做适配:

Infrastructure:
- ApplicationDbContext: 单机 MongoDB 禁用自动事务 (AutoTransactionBehavior.Never)
- UserEntityTypeConfiguration: UserDept 主键映射 _id、仅对 DeptId 建索引
- DesignTimeApplicationDbContextFactory: 增加 UseMongoDB 设计时连接
- Repositories (User/Role/Dept): MongoDB 下重写 GetAsync,用 IgnoreAutoIncludes/ID 过滤

Web:
- SeedDatabaseExtension: 种子数据检查 Roles/Users 时使用 IgnoreAutoIncludes
- UserQuery / RoleQuery: 各查询方法增加 MongoDB 分支,避免跨集合导航与复杂 LINQ
- UpdateRoleInfoCommand: MongoDB 分支直接维护 RolePermissions 集合
- UpdateUserCommand: MongoDB 分支直接维护 UserDepts,支持改部门

Domain:
- UserDept: 新增 UpdateDept(DeptId, string),仅 MongoDB 时编译,供 UpdateUser 就地更新

Tests:
- UserTests / RoleTests: GetAdminRoleIdAsync、CleanupTestDataAsync 等使用 IgnoreAutoIncludes
/// 更新角色信息命令处理器
/// </summary>
//#if (UseMongoDB)
/// MongoDB 不支持通过导航属性保存跨集合实体,需要直接操作 RolePermissions 集合
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里乱码了

//#endif
//#elif (UseMongoDB)
/// <summary>
/// 单机 MongoDB 不支持事务,禁用自动事务以避免 SaveChanges 报错。
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MongoDB是支持事物的,这里不能这样处理

/// <param name="status">状态(0=禁用,1=启用)</param>
public Dept(string name, string remark, DeptId parentId, int status)
{
CreatedAt = DateTimeOffset.Now;
Copy link
Copy Markdown
Member

@witskeeper witskeeper Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该使用DateTimeOffset.UtcNow,需要全局替换一下,各个地方都使用UtcNow

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修正

{
public async Task<RoleId> Handle(CreateRoleCommand request, CancellationToken cancellationToken)
{
var permissions = request.PermissionCodes.Select(perm => new RolePermission(perm));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里RolePermission的PermissionName和PermissionDescription为空,是否符合业务定义

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Permission可以控制前端左边菜单显示,以及各个接口的访问权限,但如果创建一个角色,比如普通用户,用来标识某个用户的角色,这个普通用户不分配Permission应该是没有问题吧。

zdf and others added 8 commits January 27, 2026 10:16
- 将 [Tags] 特性改为 Configure() 方法配置
- 添加 Description 配置
- 简化 XML 注释格式
- 统一 26 个 Endpoint 文件的代码风格
- 新增 PermissionMapper 辅助类,通过权限代码自动获取权限名称和描述
- 优化 CreateRoleCommand 和 UpdateRoleInfoCommand,使用 PermissionMapper 自动填充权限信息
- 优化 SeedDatabaseExtension,使用 PermissionMapper 避免重复定义映射关系
@witskeeper
Copy link
Copy Markdown
Member

@zhouda1fu 感谢PR,这是一次飞跃的升级

@witskeeper witskeeper merged commit c712fea into netcorepal:main Jan 28, 2026
167 checks passed
@witskeeper
Copy link
Copy Markdown
Member

#175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants