Files
f9-web-ai-test/backend/F9WebAiTest.Application/CQRS/SYS/RBAC/Roles/Commands/Role_CreateCommand.cs
T
2026-10-08 10:43:59 +08:00

28 lines
1.3 KiB
C#

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;
/// <summary>命令定义</summary>
public record Role_CreateCommand(string Name, string Code, int SortOrder, string Description) : IRequest<long>;
/// <summary>命令处理器</summary>
public class CreateRoleCommandHandler(
IRoleWriteRepository roleRepository,
ISnowflakeIdGenerator snowflakeIdGenerator) : IRequestHandler<Role_CreateCommand, long>
{
public async Task<long> 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;
}
}