first commit
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Department_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class ActiveOrDeactivateDepartmentCommandHandler(
|
||||
IDepartmentWriteRepository departmentWriteRepository,
|
||||
IPublisher eventBus) : IRequestHandler<Department_ActiveOrDeactivateCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Department_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var dep = await departmentWriteRepository.GetByIdAsync(command.Id, CancellationToken.None);
|
||||
if (dep == null) throw new F9NotFoundException("部门", command.Id);
|
||||
|
||||
if (command.Active) dep.Activate();
|
||||
else dep.Deactivate();
|
||||
|
||||
var result = await departmentWriteRepository.ActiveOrDeactive(command.Id, dep.IsActive);
|
||||
if (result == false) throw new F9OperationException("启用/禁用", "操作");
|
||||
|
||||
foreach (var de in dep.DomainEvents) await eventBus.Publish(de, cancellationToken);
|
||||
dep.ClearDomainEvents();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
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_CreateCommand(string Code, string Name, long ParentId, Department_ManageType_Enum ManageType, Department_DutyType_Enum DutyType, int SortOrder, string Description) : IRequest<long>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class CreateDepartmentCommandHandler(
|
||||
IDepartmentWriteRepository departmentRepository,
|
||||
ISnowflakeIdGenerator snowflakeIdGenerator,
|
||||
IPublisher eventBus) : IRequestHandler<Department_CreateCommand, long>
|
||||
{
|
||||
public async Task<long> Handle(Department_CreateCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("部门编码不能为空");
|
||||
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("部门名称不能为空");
|
||||
|
||||
var dep = new Department(snowflakeIdGenerator.CreateId(), command.Code, command.Name, command.ParentId,
|
||||
command.ManageType, command.DutyType, command.SortOrder, command.Description);
|
||||
|
||||
var result = await departmentRepository.AddAsync(dep);
|
||||
if (result == false) throw new F9OperationException("部门", "新增");
|
||||
|
||||
foreach (var de in dep.DomainEvents) await eventBus.Publish(de, cancellationToken);
|
||||
dep.ClearDomainEvents();
|
||||
|
||||
return dep.Id;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
using F9WebAiTest.Application.Common.Exceptions;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
||||
using MediatR;
|
||||
|
||||
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
||||
|
||||
/// <summary>命令定义</summary>
|
||||
public record Department_DelCommand(long Id) : IRequest<bool>;
|
||||
|
||||
/// <summary>命令处理器</summary>
|
||||
public class DelDepartmentCommandHandler(
|
||||
IDepartmentWriteRepository departmentWriteRepository) : IRequestHandler<Department_DelCommand, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Department_DelCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
if (command.Id <= 0) throw new F9OperationException("部门ID不能为空");
|
||||
|
||||
var result = await departmentWriteRepository.SoftDeleteAsync(command.Id);
|
||||
if (result == false) throw new F9OperationException("部门", "删除");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user