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

30 lines
1.4 KiB
C#

using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Application.Interfaces.SnowflakeID;
using F9WebAiTest.Domain.Entities.SYS.Departments;
using F9WebAiTest.Domain.Enums.SYS.Departments;
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
/// <summary>命令定义</summary>
public record Department_UpdateCommand(long Id, string Code, string Name, long ParentId, Department_ManageType_Enum ManageType, Department_DutyType_Enum DutyType, int SortOrder, string Description) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class UpdateDepartmentCommandHandler(
IDepartmentWriteRepository departmentRepository) : IRequestHandler<Department_UpdateCommand, bool>
{
public async Task<bool> Handle(Department_UpdateCommand command, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("部门编码不能为空");
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("部门名称不能为空");
var dep = new Department(command.Id, command.Code, command.Name, command.ParentId,
command.ManageType, command.DutyType, command.SortOrder, command.Description);
var result = await departmentRepository.UpdateAsync(dep);
if (result == false) throw new F9OperationException("部门", "修改");
return result;
}
}