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

26 lines
1.1 KiB
C#

using F9WebAiTest.Application.Common.Exceptions;
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_UpdateCommand(long Id, string Name, string Code, int SortOrder, string Description) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class UpdateRoleCommandHandler(
IRoleWriteRepository roleRepository) : IRequestHandler<Role_UpdateCommand, bool>
{
public async Task<bool> Handle(Role_UpdateCommand command, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("角色名称不能为空");
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("角色编码不能为空");
var newEntity = new Role(command.Id, command.Name, command.Code, command.SortOrder, command.Description);
var result = await roleRepository.UpdateAsync(newEntity);
if (result == false) throw new F9OperationException("角色", "修改");
return result;
}
}