CreateOrUpdateNetActionPropertysCommandHandler.cs
4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Entities;
using Rcs.Domain.Enums;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 创建或更新网络动作属性命令处理器
/// @author zzy
/// </summary>
public class CreateOrUpdateNetActionPropertysCommandHandler : IConsumer<CreateOrUpdateNetActionPropertysCommand>
{
private readonly ILogger<CreateOrUpdateNetActionPropertysCommandHandler> _logger;
private readonly INetActionPropertysRepository _repository;
public CreateOrUpdateNetActionPropertysCommandHandler(
ILogger<CreateOrUpdateNetActionPropertysCommandHandler> logger,
INetActionPropertysRepository repository)
{
_logger = logger;
_repository = repository;
}
public async Task Consume(ConsumeContext<CreateOrUpdateNetActionPropertysCommand> context)
{
var command = context.Message;
try
{
if (!Guid.TryParse(command.NetActionId, out Guid netActionId))
{
// 创建新实体
var existing = await _repository.FirstOrDefaultAsync(
x => x.ActionName == command.ActionName,
context.CancellationToken);
if (existing != null)
{
throw new InvalidOperationException($"动作名称 {command.ActionName} 已存在");
}
var entity = new NetActionPropertys
{
NetActionId = Guid.NewGuid(),
ActionName = command.ActionName,
IsActive = command.IsActive,
RequestMethod = command.RequestMethod,
RequestUrl = command.RequestUrl,
RequestParams = command.RequestParams,
RepeatCount = command.RepeatCount,
IntervalTimeMs = command.IntervalTimeMs,
WaitResponseType = (ResponseWaitType)command.WaitResponseType,
ResponseValidationRule = command.ResponseValidationRule,
Description = command.Description,
ExtraProperties = command.ExtraProperties,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
await _repository.AddAsync(entity, context.CancellationToken);
await _repository.SaveChangesAsync(context.CancellationToken);
}
else
{
// 更新现有实体
var entity = await _repository.GetByIdAsync(netActionId, context.CancellationToken);
if (entity == null)
{
throw new InvalidOperationException($"未找到ID为 {netActionId} 的网络动作属性");
}
// 检查名称是否与其他记录冲突
var existing = await _repository.FirstOrDefaultAsync(
x => x.ActionName == command.ActionName && x.NetActionId != netActionId,
context.CancellationToken);
if (existing != null)
{
throw new InvalidOperationException($"动作名称 {command.ActionName} 已存在");
}
entity.ActionName = command.ActionName;
entity.IsActive = command.IsActive;
entity.RequestMethod = command.RequestMethod;
entity.RequestUrl = command.RequestUrl;
entity.RequestParams = command.RequestParams;
entity.RepeatCount = command.RepeatCount;
entity.IntervalTimeMs = command.IntervalTimeMs;
entity.WaitResponseType = command?.WaitResponseType == null
? ResponseWaitType.None
: (ResponseWaitType)command.WaitResponseType;
entity.ResponseValidationRule = command.ResponseValidationRule;
entity.Description = command.Description;
entity.ExtraProperties = command.ExtraProperties;
entity.UpdatedAt = DateTime.Now;
await _repository.UpdateAsync(entity, context.CancellationToken);
await _repository.SaveChangesAsync(context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}