Files
2026-10-08 10:43:59 +08:00

32 lines
1.2 KiB
C#

using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Menus.Commands;
/// <summary>命令定义</summary>
public record Menu_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class ActiveOrDeactivateMenuCommandHandler(
IMenuWriteRepository menuWriteRepository,
IPublisher eventBus) : IRequestHandler<Menu_ActiveOrDeactivateCommand, bool>
{
public async Task<bool> Handle(Menu_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
{
var menu = await menuWriteRepository.GetByIdAsync(command.Id);
if (menu == null) throw new F9NotFoundException("菜单", command.Id);
if (command.Active) menu.Activate();
else menu.Deactivate();
var result = await menuWriteRepository.ActiveOrDeactive(command.Id, menu.IsActive);
if (result == false) throw new F9OperationException("启用/禁用", "操作");
foreach (var de in menu.DomainEvents) await eventBus.Publish(de, cancellationToken);
menu.ClearDomainEvents();
return result;
}
}