27 lines
1.0 KiB
C#
27 lines
1.0 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 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;
|
|
}
|
|
}
|