36 lines
1.4 KiB
C#
36 lines
1.4 KiB
C#
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;
|
|
}
|
|
}
|