first commit
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Department_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class ActiveOrDeactivateDepartmentCommandHandler(
|
||||
IDepartmentWriteRepository departmentWriteRepository,
|
||||
IPublisher eventBus) : IRequestHandler<Department_ActiveOrDeactivateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Department_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var dep = await departmentWriteRepository.GetByIdAsync(command.Id, CancellationToken.None);
|
||||
if (dep == null) throw new F9NotFoundException("部门", command.Id);
|
||||
|
||||
if (command.Active) dep.Activate();
|
||||
else dep.Deactivate();
|
||||
|
||||
var result = await departmentWriteRepository.ActiveOrDeactive(command.Id, dep.IsActive);
|
||||
if (result == false) throw new F9OperationException("启用/禁用", "操作");
|
||||
|
||||
foreach (var de in dep.DomainEvents) await eventBus.Publish(de, cancellationToken);
|
||||
dep.ClearDomainEvents();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Enums.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Department_CreateCommand(string Code, string Name, long ParentId, Department_ManageType_Enum ManageType, Department_DutyType_Enum DutyType, int SortOrder, string Description) : IRequest<long>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class CreateDepartmentCommandHandler(
|
||||
IDepartmentWriteRepository departmentRepository,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||||
IPublisher eventBus) : IRequestHandler<Department_CreateCommand, long>
|
||||
{
|
||||
public async Task<long> Handle(Department_CreateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("部门编码不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("部门名称不能为空");
|
||||
|
||||
var dep = new Department(snowflakeIdGenerator.CreateId(), command.Code, command.Name, command.ParentId,
|
||||
command.ManageType, command.DutyType, command.SortOrder, command.Description);
|
||||
|
||||
var result = await departmentRepository.AddAsync(dep);
|
||||
if (result == false) throw new F9OperationException("部门", "新增");
|
||||
|
||||
foreach (var de in dep.DomainEvents) await eventBus.Publish(de, cancellationToken);
|
||||
dep.ClearDomainEvents();
|
||||
|
||||
return dep.Id;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Department_DelCommand(long Id) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class DelDepartmentCommandHandler(
|
||||
IDepartmentWriteRepository departmentWriteRepository) : IRequestHandler<Department_DelCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Department_DelCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("部门ID不能为空");
|
||||
|
||||
var result = await departmentWriteRepository.SoftDeleteAsync(command.Id);
|
||||
if (result == false) throw new F9OperationException("部门", "删除");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Enums.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Department_UpdateCommand(long Id, string Code, string Name, long ParentId, Department_ManageType_Enum ManageType, Department_DutyType_Enum DutyType, int SortOrder, string Description) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class UpdateDepartmentCommandHandler(
|
||||
IDepartmentWriteRepository departmentRepository) : IRequestHandler<Department_UpdateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Department_UpdateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("部门编码不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("部门名称不能为空");
|
||||
|
||||
var dep = new Department(command.Id, command.Code, command.Name, command.ParentId,
|
||||
command.ManageType, command.DutyType, command.SortOrder, command.Description);
|
||||
|
||||
var result = await departmentRepository.UpdateAsync(dep);
|
||||
if (result == false) throw new F9OperationException("部门", "修改");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using F9WebAiTest.Application.Dtos.SYS.Departments;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.Departments;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Queries;
|
||||
|
||||
public record GetDepartmentAllQuery() : IRequest<List<DepartmentReadDto_View>>;
|
||||
|
||||
public class GetDepartmentAllQueryHandler(IDepartmentReadRepository departmentReadRepository) : IRequestHandler<GetDepartmentAllQuery, List<DepartmentReadDto_View>>
|
||||
{
|
||||
public async Task<List<DepartmentReadDto_View>> Handle(GetDepartmentAllQuery query, CancellationToken ct)
|
||||
=> await departmentReadRepository.GetAllAsync();
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Menus.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Menu_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class ActiveOrDeactivateMenuCommandHandler(
|
||||
IMenuWriteRepository menuWriteRepository,
|
||||
IPublisher eventBus) : IRequestHandler<Menu_ActiveOrDeactivateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Menu_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var menu = await menuWriteRepository.GetByIdAsync(command.Id);
|
||||
if (menu == null) throw new F9NotFoundException("菜单", command.Id);
|
||||
|
||||
if (command.Active) menu.Activate();
|
||||
else menu.Deactivate();
|
||||
|
||||
var result = await menuWriteRepository.ActiveOrDeactive(command.Id, menu.IsActive);
|
||||
if (result == false) throw new F9OperationException("启用/禁用", "操作");
|
||||
|
||||
foreach (var de in menu.DomainEvents) await eventBus.Publish(de, cancellationToken);
|
||||
menu.ClearDomainEvents();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Menus.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Menu_CreateCommand(string Name, string IconSvg, MenuType_Enum MenuType, string AssemblyName, string TypeFullName, long ParentId, int SortOrder) : IRequest<long>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class CreateMenuCommandHandler(
|
||||
IMenuWriteRepository menuRepository,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||||
IPublisher eventBus) : IRequestHandler<Menu_CreateCommand, long>
|
||||
{
|
||||
public async Task<long> Handle(Menu_CreateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("菜单名称不能为空");
|
||||
|
||||
var menu = new Menu(snowflakeIdGenerator.CreateId(), command.Name, command.IconSvg,
|
||||
command.MenuType, command.AssemblyName, command.TypeFullName, command.ParentId, command.SortOrder);
|
||||
|
||||
var result = await menuRepository.AddAsync(menu);
|
||||
if (result == false) throw new F9OperationException("菜单", "新增");
|
||||
|
||||
foreach (var de in menu.DomainEvents) await eventBus.Publish(de, cancellationToken);
|
||||
menu.ClearDomainEvents();
|
||||
|
||||
return menu.Id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Menus.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Menu_DelCommand(long Id) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class DelMenuCommandHandler(
|
||||
IMenuWriteRepository menuWriteRepository) : IRequestHandler<Menu_DelCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Menu_DelCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("菜单ID不能为空");
|
||||
|
||||
var result = await menuWriteRepository.SoftDeleteAsync(command.Id);
|
||||
if (result == false) throw new F9OperationException("菜单", "删除");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Menus.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Menu_UpdateCommand(long Id, string Name, string IconSvg, MenuType_Enum MenuType, string AssemblyName, string TypeFullName, long ParentId, int SortOrder) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class UpdateMenuCommandHandler(
|
||||
IMenuWriteRepository menuRepository) : IRequestHandler<Menu_UpdateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Menu_UpdateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("菜单名称不能为空");
|
||||
|
||||
var menu = new Menu(command.Id, command.Name, command.IconSvg,
|
||||
command.MenuType, command.AssemblyName, command.TypeFullName, command.ParentId, command.SortOrder);
|
||||
|
||||
var result = await menuRepository.UpdateAsync(menu);
|
||||
if (result == false) throw new F9OperationException("菜单", "修改");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Application.Interfaces.UOW;
|
||||
using F9WebAiTest.Domain.Common;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
using NotificationTypeEntity = F9WebAiTest.Domain.Entities.Notice.NotificationType;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Permissions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record RolePermission_AssignCommand(long RoleId, Permission_Type_Enum PermissionType, Resource_Type_Enum ResourceType, string ResourceIdentity, long ResourceId) : IRequest<long>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class AssignRolePermissionCommandHandler(
|
||||
IRoleWriteRepository roleRepository,
|
||||
IUnitOfWork_F9 unitOfWork,
|
||||
IRolePermissionWriteRepository rolePermissionRep,
|
||||
IPermissionWriteRepository permissionWriteRepository,
|
||||
IRepository<NotificationTypeEntity> notificationTypeRepository,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||||
IPublisher eventBus) : IRequestHandler<RolePermission_AssignCommand, long>
|
||||
{
|
||||
public async Task<long> Handle(RolePermission_AssignCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.RoleId <= 0) throw new F9OperationException("角色ID不能为空");
|
||||
if (command.ResourceId <= 0) throw new F9OperationException("资源ID不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.ResourceIdentity)) throw new F9OperationException("资源标识不能为空");
|
||||
|
||||
Role role = null;
|
||||
long rolePermissionId = 0;
|
||||
|
||||
var ok = await unitOfWork.ExecuteWithUow(async () =>
|
||||
{
|
||||
// 查或创建权限
|
||||
var dbPermission = await permissionWriteRepository.GetByIdAsync(command.PermissionType, command.ResourceType, command.ResourceId);
|
||||
if (dbPermission == null)
|
||||
{
|
||||
dbPermission = new Permission(
|
||||
snowflakeIdGenerator.CreateId(),
|
||||
command.PermissionType,
|
||||
command.ResourceType,
|
||||
command.ResourceIdentity,
|
||||
command.ResourceId);
|
||||
|
||||
// 消息订阅权限:Code 对齐消息类型的 RequiredPermission(业务权限码),保证订阅校验一致
|
||||
if (command.ResourceType == Resource_Type_Enum.消息订阅)
|
||||
{
|
||||
var type = await notificationTypeRepository.GetByIdAsync(command.ResourceId);
|
||||
if (type != null && !string.IsNullOrEmpty(type.RequiredPermission))
|
||||
dbPermission.SetCode(type.RequiredPermission);
|
||||
}
|
||||
|
||||
await permissionWriteRepository.AddRangeAsync(new[] { dbPermission });
|
||||
}
|
||||
|
||||
// 绑定角色权限
|
||||
var rolePermission = new RolePermission(snowflakeIdGenerator.CreateId(), command.RoleId, dbPermission.Id);
|
||||
var result = await rolePermissionRep.AssignAsync(rolePermission);
|
||||
|
||||
role = await roleRepository.GetByIdAsync(command.RoleId);
|
||||
role.AddPermission(dbPermission.Id, command.ResourceType);
|
||||
|
||||
rolePermissionId = rolePermission.Id;
|
||||
return result;
|
||||
});
|
||||
|
||||
if (!ok) throw new F9OperationException("权限", "绑定");
|
||||
|
||||
// 发布领域事件
|
||||
foreach (var domainEvent in role.DomainEvents)
|
||||
await eventBus.Publish(domainEvent, cancellationToken);
|
||||
role.ClearDomainEvents();
|
||||
|
||||
return rolePermissionId;
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record RolePermission_RemoveCommand(long Id, Resource_Type_Enum ResourceType) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class RemoveRolePermissionCommandHandler(
|
||||
IRoleWriteRepository roleRepository,
|
||||
IRolePermissionWriteRepository rolePermissionWriteRepository,
|
||||
IPublisher eventBus) : IRequestHandler<RolePermission_RemoveCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(RolePermission_RemoveCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("角色权限ID不能为空");
|
||||
|
||||
var rolePermission = await rolePermissionWriteRepository.GetByIdAsync(command.Id);
|
||||
if (rolePermission == null) throw new F9NotFoundException("角色权限", command.Id);
|
||||
|
||||
var role = await roleRepository.GetByIdAsync(rolePermission.RoleId);
|
||||
role.RemovePermission(rolePermission.PermissionId, command.ResourceType);
|
||||
|
||||
var result = await rolePermissionWriteRepository.RemoveAsync(command.Id);
|
||||
if (result == false) throw new F9OperationException("权限", "解绑");
|
||||
|
||||
// 发布领域事件
|
||||
foreach (var domainEvent in role.DomainEvents)
|
||||
await eventBus.Publish(domainEvent, cancellationToken);
|
||||
role.ClearDomainEvents();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Role_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class ActiveOrDeactivateRoleCommandHandler(
|
||||
IRoleWriteRepository roleWriteRepository) : IRequestHandler<Role_ActiveOrDeactivateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Role_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var role = await roleWriteRepository.GetByIdAsync(command.Id);
|
||||
if (role == null) throw new F9NotFoundException("角色", command.Id);
|
||||
|
||||
if (command.Active) role.Activate();
|
||||
else role.Deactivate();
|
||||
|
||||
var result = await roleWriteRepository.ActiveOrDeactive(command.Id, role.IsActive);
|
||||
if (result == false) throw new F9OperationException("启用/禁用", "操作");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Role_CreateCommand(string Name, string Code, int SortOrder, string Description) : IRequest<long>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class CreateRoleCommandHandler(
|
||||
IRoleWriteRepository roleRepository,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator) : IRequestHandler<Role_CreateCommand, long>
|
||||
{
|
||||
public async Task<long> Handle(Role_CreateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("角色名称不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("角色编码不能为空");
|
||||
|
||||
var newEntity = new Role(snowflakeIdGenerator.CreateId(), command.Name, command.Code, command.SortOrder, command.Description);
|
||||
var result = await roleRepository.AddAsync(newEntity);
|
||||
if (result == false) throw new F9OperationException("角色", "新增");
|
||||
return newEntity.Id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Role_DelCommand(long Id) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class DelRoleCommandHandler(
|
||||
IRoleWriteRepository roleWriteRepository) : IRequestHandler<Role_DelCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Role_DelCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("角色ID不能为空");
|
||||
|
||||
var result = await roleWriteRepository.SoftDeleteAsync(command.Id);
|
||||
if (result == false) throw new F9OperationException("角色", "删除");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Role_UpdateCommand(long Id, string Name, string Code, int SortOrder, string Description) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class UpdateRoleCommandHandler(
|
||||
IRoleWriteRepository roleRepository) : IRequestHandler<Role_UpdateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Role_UpdateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("角色名称不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("角色编码不能为空");
|
||||
|
||||
var newEntity = new Role(command.Id, command.Name, command.Code, command.SortOrder, command.Description);
|
||||
var result = await roleRepository.UpdateAsync(newEntity);
|
||||
if (result == false) throw new F9OperationException("角色", "修改");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Queries;
|
||||
|
||||
public record GetRoleAllQuery() : IRequest<List<RoleReadDto_View>>;
|
||||
|
||||
public class GetRoleAllQueryHandler(IRoleReadRepository roleReadRepository) : IRequestHandler<GetRoleAllQuery, List<RoleReadDto_View>>
|
||||
{
|
||||
public async Task<List<RoleReadDto_View>> Handle(GetRoleAllQuery query, CancellationToken ct)
|
||||
=> await roleReadRepository.GetAllAsync();
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Roles;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Queries;
|
||||
|
||||
public record GetRoleByIdForEditQuery(long Id) : IRequest<RoleReadDto_Edit>;
|
||||
|
||||
public class GetRoleByIdForEditQueryHandler(IRoleReadRepository roleReadRepository) : IRequestHandler<GetRoleByIdForEditQuery, RoleReadDto_Edit>
|
||||
{
|
||||
public async Task<RoleReadDto_Edit> Handle(GetRoleByIdForEditQuery query, CancellationToken ct)
|
||||
=> await roleReadRepository.GetByIdAsync(query.Id);
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Roles;
|
||||
using F9WebAiTest.Domain.Enums.SYS.RBAC;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Queries;
|
||||
|
||||
public record GetRolePermissionsByResourceTypeQuery(long RoleId, Resource_Type_Enum ResourceType) : IRequest<List<RolePermissionReadDto_View>>;
|
||||
|
||||
public class GetRolePermissionsByResourceTypeQueryHandler(IRoleReadRepository roleReadRepository) : IRequestHandler<GetRolePermissionsByResourceTypeQuery, List<RolePermissionReadDto_View>>
|
||||
{
|
||||
public async Task<List<RolePermissionReadDto_View>> Handle(GetRolePermissionsByResourceTypeQuery query, CancellationToken ct)
|
||||
=> await roleReadRepository.GetRolePermissionsByResourceType(query.RoleId, query.ResourceType);
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record UserRole_AssignCommand(long UserId, long RoleId) : IRequest<long>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class UserRole_AssignCommandHandler(
|
||||
IUserRoleWriteRepository userRoleWriteRepository,
|
||||
IUserWriteRepository userWriteRepository,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||||
IPublisher eventBus) : IRequestHandler<UserRole_AssignCommand, long>
|
||||
{
|
||||
public async Task<long> Handle(UserRole_AssignCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.UserId <= 0) throw new F9OperationException("用户ID不能为空");
|
||||
if (command.RoleId <= 0) throw new F9OperationException("角色ID不能为空");
|
||||
|
||||
var newEntity = new UserRole(snowflakeIdGenerator.CreateId(), command.UserId, command.RoleId);
|
||||
|
||||
var user = await userWriteRepository.GetByIdAsync(command.UserId);
|
||||
if (user == null) throw new F9NotFoundException("用户", command.UserId);
|
||||
user.AddRole(newEntity.Id);
|
||||
|
||||
var result = await userRoleWriteRepository.AssignAsync(newEntity);
|
||||
if (result == false) throw new F9OperationException("权限", "绑定");
|
||||
|
||||
// 发布领域事件
|
||||
foreach (var domainEvent in user.DomainEvents)
|
||||
await eventBus.Publish(domainEvent, cancellationToken);
|
||||
user.ClearDomainEvents();
|
||||
|
||||
return newEntity.Id;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record UserRole_RemoveCommand(long UserId, long RoleId) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class UserRole_RemoveCommandHandler(
|
||||
IUserWriteRepository userWriteRepository,
|
||||
IUserRoleWriteRepository userRoleWriteRepository,
|
||||
IPublisher eventBus) : IRequestHandler<UserRole_RemoveCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(UserRole_RemoveCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.UserId <= 0) throw new F9OperationException("用户ID不能为空");
|
||||
if (command.RoleId <= 0) throw new F9OperationException("角色ID不能为空");
|
||||
|
||||
var user = await userWriteRepository.GetByIdAsync(command.UserId);
|
||||
if (user == null) throw new F9NotFoundException("用户", command.UserId);
|
||||
user.RemoveRole(command.RoleId);
|
||||
|
||||
var result = await userRoleWriteRepository.RemoveAsync(command.UserId, command.RoleId);
|
||||
if (result == false) throw new F9OperationException("权限", "解绑");
|
||||
|
||||
// 发布领域事件
|
||||
foreach (var domainEvent in user.DomainEvents)
|
||||
await eventBus.Publish(domainEvent, cancellationToken);
|
||||
user.ClearDomainEvents();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record User_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class ActiveOrDeactivateUserCommandHandler(
|
||||
IUserWriteRepository userWriteRepository) : IRequestHandler<User_ActiveOrDeactivateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(User_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var user = await userWriteRepository.GetByIdAsync(command.Id);
|
||||
if (user == null) throw new F9NotFoundException("用户", command.Id);
|
||||
|
||||
if (command.Active) user.Activate();
|
||||
else user.Deactivate();
|
||||
|
||||
var result = await userWriteRepository.ActiveOrDeactive(command.Id, user.IsActive);
|
||||
if (result == false) throw new F9OperationException("启用/禁用", "操作");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.Security;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Application.Interfaces.UOW;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Entities.SYS.RBAC;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record User_CreateCommand(string UserName, string PhoneNumber, string LoginName, long DepartmentId, int SortOrder) : IRequest<long>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class CreateUserCommandHandler(
|
||||
IUnitOfWork_F9 unitOfWork,
|
||||
IUserWriteRepository userRepository,
|
||||
IDepartmentWriteRepository departmentWriteRepository,
|
||||
IDepartmentUserRepository departmentUserRepository,
|
||||
IPasswordHasher passwordHasher,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||||
IPublisher eventBus) : IRequestHandler<User_CreateCommand, long>
|
||||
{
|
||||
public async Task<long> Handle(User_CreateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
// 手写校验
|
||||
if (string.IsNullOrWhiteSpace(command.UserName)) throw new F9OperationException("用户名不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.LoginName)) throw new F9OperationException("登录名不能为空");
|
||||
if (command.DepartmentId <= 0) throw new F9OperationException("部门不能为空");
|
||||
|
||||
User createdUser = null;
|
||||
var ok = await unitOfWork.ExecuteWithUow(async () =>
|
||||
{
|
||||
// 1. 验证部门是否存在(应用层协调)
|
||||
var department = await departmentWriteRepository.GetByIdAsync(command.DepartmentId, cancellationToken);
|
||||
if (department == null) throw new F9NotFoundException("部门不存在");
|
||||
|
||||
// 2. 创建用户聚合根
|
||||
var user = new User(
|
||||
id: snowflakeIdGenerator.CreateId(),
|
||||
command.UserName,
|
||||
command.PhoneNumber,
|
||||
command.LoginName,
|
||||
command.DepartmentId,
|
||||
command.SortOrder);
|
||||
|
||||
// 设置默认密码
|
||||
var defaultPasswordMd5 = passwordHasher.GetMd5Hash(user.GetDefaultPasswordText());
|
||||
var passwordHash = passwordHasher.HashPassword(defaultPasswordMd5);
|
||||
user.SetPassword(passwordHash);
|
||||
|
||||
await userRepository.AddAsync(user);
|
||||
|
||||
// 3. 创建部门用户关联
|
||||
var depUser = new DepartmentUser(snowflakeIdGenerator.CreateId(), command.DepartmentId, user.Id);
|
||||
await departmentUserRepository.AssignAsync(depUser);
|
||||
|
||||
createdUser = user;
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!ok) throw new F9OperationException("用户", "新增");
|
||||
|
||||
// 4. 事务提交后发布领域事件
|
||||
foreach (var domainEvent in createdUser.DomainEvents)
|
||||
await eventBus.Publish(domainEvent, cancellationToken);
|
||||
createdUser.ClearDomainEvents();
|
||||
|
||||
return createdUser.Id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record User_DelCommand(long Id) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class DelUserCommandHandler(
|
||||
IUserWriteRepository userWriteRepository) : IRequestHandler<User_DelCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(User_DelCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("用户ID不能为空");
|
||||
|
||||
var result = await userWriteRepository.SoftDeleteAsync(command.Id);
|
||||
if (result == false) throw new F9OperationException("用户", "删除");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.Security;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record User_ResetPasswordCommand(long Id) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class ResetPasswordUserCommandHandler(
|
||||
IUserWriteRepository userWriteRepository,
|
||||
IPasswordHasher passwordHasher) : IRequestHandler<User_ResetPasswordCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(User_ResetPasswordCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("用户ID不能为空");
|
||||
|
||||
var dbUser = await userWriteRepository.GetByIdAsync(command.Id);
|
||||
if (dbUser == null) throw new F9NotFoundException("用户", command.Id);
|
||||
|
||||
// 设置默认密码
|
||||
var defaultPasswordMd5 = passwordHasher.GetMd5Hash(dbUser.GetDefaultPasswordText());
|
||||
var passwordHash = passwordHasher.HashPassword(defaultPasswordMd5);
|
||||
dbUser.SetPassword(passwordHash);
|
||||
|
||||
var result = await userWriteRepository.UpdatePassworkdAsync(command.Id, dbUser.PasswordHash);
|
||||
if (result == false) throw new F9OperationException("用户", "重置密码");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
||||
using F9WebAiTest.Application.Interfaces.UOW;
|
||||
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record User_UpdateCommand(long Id, string UserName, string PhoneNumber, string LoginName, long DepartmentId, int SortOrder) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class User_UpdateCommandHandler(
|
||||
IUnitOfWork_F9 unitOfWork,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||||
IUserWriteRepository userWriteRepository,
|
||||
IDepartmentUserRepository departmentUserRepository) : IRequestHandler<User_UpdateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(User_UpdateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("用户ID不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.UserName)) throw new F9OperationException("用户名不能为空");
|
||||
if (command.DepartmentId <= 0) throw new F9OperationException("部门不能为空");
|
||||
|
||||
return await unitOfWork.ExecuteWithUow(async () =>
|
||||
{
|
||||
// 获取数据库用户
|
||||
var dbUser = await userWriteRepository.GetByIdAsync(command.Id);
|
||||
if (dbUser == null) throw new F9NotFoundException("用户", command.Id);
|
||||
|
||||
// 移除原部门关系
|
||||
await departmentUserRepository.RemoveAsync(dbUser.DepartmentId, dbUser.Id);
|
||||
|
||||
// 领域更新
|
||||
dbUser.Update(command.UserName, command.PhoneNumber, command.LoginName, command.DepartmentId, command.SortOrder);
|
||||
|
||||
// 持久化
|
||||
var result = await userWriteRepository.UpdateAsync(dbUser);
|
||||
|
||||
// 添加新部门关系
|
||||
await departmentUserRepository.AssignAsync(new DepartmentUser(
|
||||
snowflakeIdGenerator.CreateId(), dbUser.DepartmentId, dbUser.Id));
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Users;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Queries;
|
||||
|
||||
public record GetUserByIdQuery(long id) : IRequest<UserReadDto_Edit>;
|
||||
|
||||
public class GetUserByIdQueryHandler(IUserReadRepository userReadRepository) : IRequestHandler<GetUserByIdQuery, UserReadDto_Edit>
|
||||
{
|
||||
public async Task<UserReadDto_Edit> Handle(GetUserByIdQuery query, CancellationToken ct)
|
||||
=> await userReadRepository.GetByIdAsync(query.id);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Users;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Queries;
|
||||
|
||||
public record GetUserByIdsQuery(List<long> Ids) : IRequest<List<UserReadDto_Edit>>;
|
||||
|
||||
public class GetUserByIdsQueryHandler(IUserReadRepository userReadRepository) : IRequestHandler<GetUserByIdsQuery, List<UserReadDto_Edit>>
|
||||
{
|
||||
public async Task<List<UserReadDto_Edit>> Handle(GetUserByIdsQuery query, CancellationToken ct)
|
||||
=> await userReadRepository.GetByIdsAsync(query.Ids);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using F9WebAiTest.Application.Dtos.Common;
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Users;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Queries;
|
||||
|
||||
public record GetUserPagedQuery(long DepartmentId, string Like, int Page = 1, int PageSize = 10) : IRequest<PagedResult<UserReadDto_View>>;
|
||||
|
||||
public class GetUserPagedQueryHandler(IUserReadRepository userReadRepository) : IRequestHandler<GetUserPagedQuery, PagedResult<UserReadDto_View>>
|
||||
{
|
||||
public async Task<PagedResult<UserReadDto_View>> Handle(GetUserPagedQuery query, CancellationToken ct)
|
||||
=> await userReadRepository.GetPagedAsync(query.DepartmentId, query.Like, query.Page, query.PageSize);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using F9WebAiTest.Application.Dtos.SYS.RBAC.Users;
|
||||
using F9WebAiTest.Application.Interfaces.Repositories.SYS.RBAC.Users;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Queries;
|
||||
|
||||
public record GetUserRolesByUserIdQuery(long UserId) : IRequest<List<UserRoleReadDto_Cache>>;
|
||||
|
||||
public class GetUserRolesByUserIdQueryHandler(IUserReadRepository userReadRepository) : IRequestHandler<GetUserRolesByUserIdQuery, List<UserRoleReadDto_Cache>>
|
||||
{
|
||||
public async Task<List<UserRoleReadDto_Cache>> Handle(GetUserRolesByUserIdQuery query, CancellationToken ct)
|
||||
=> await userReadRepository.GetUserRolesByUserId(query.UserId);
|
||||
}
|
||||
Reference in New Issue
Block a user