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