using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
/// 命令定义
public record Role_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest;
/// 命令处理器
public class ActiveOrDeactivateRoleCommandHandler(
IRoleWriteRepository roleWriteRepository) : IRequestHandler
{
public async Task 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;
}
}