using F9WebAiTest.Application.Common.Exceptions; using F9WebAiTest.Application.Interfaces.SnowflakeID; using F9WebAiTest.Domain.Entities.SYS.RBAC; using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles; using MediatR; namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands; /// 命令定义 public record Role_CreateCommand(string Name, string Code, int SortOrder, string Description) : IRequest; /// 命令处理器 public class CreateRoleCommandHandler( IRoleWriteRepository roleRepository, ISnowflakeIdGenerator snowflakeIdGenerator) : IRequestHandler { public async Task Handle(Role_CreateCommand command, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("角色名称不能为空"); if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("角色编码不能为空"); var newEntity = new Role(snowflakeIdGenerator.CreateId(), command.Name, command.Code, command.SortOrder, command.Description); var result = await roleRepository.AddAsync(newEntity); if (result == false) throw new F9OperationException("角色", "新增"); return newEntity.Id; } }