using F9WebAiTest.Application.Common.Exceptions; using F9WebAiTest.Application.Interfaces.SnowflakeID; using F9WebAiTest.Domain.Entities.SYS.RBAC; using F9WebAiTest.Domain.Enums.SYS.RBAC; using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus; using MediatR; namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Menus.Commands; /// 命令定义 public record Menu_CreateCommand(string Name, string IconSvg, MenuType_Enum MenuType, string AssemblyName, string TypeFullName, long ParentId, int SortOrder) : IRequest; /// 命令处理器 public class CreateMenuCommandHandler( IMenuWriteRepository menuRepository, ISnowflakeIdGenerator snowflakeIdGenerator, IPublisher eventBus) : IRequestHandler { public async Task Handle(Menu_CreateCommand command, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("菜单名称不能为空"); var menu = new Menu(snowflakeIdGenerator.CreateId(), command.Name, command.IconSvg, command.MenuType, command.AssemblyName, command.TypeFullName, command.ParentId, command.SortOrder); var result = await menuRepository.AddAsync(menu); if (result == false) throw new F9OperationException("菜单", "新增"); foreach (var de in menu.DomainEvents) await eventBus.Publish(de, cancellationToken); menu.ClearDomainEvents(); return menu.Id; } }