Files
f9-web-ai-test/backend/F9WebAiTest.Application/CQRS/SYS/RBAC/Roles/Commands/Role_ActiveOrDeactivateCommand.cs
T
2026-10-08 10:43:59 +08:00

27 lines
1.0 KiB
C#

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;
}
}