CreateOrUpdateMapCommandHandler.cs
3.26 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
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Application.MessageBus.Responses;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 创建地图命令处理器
/// </summary>
public class CreateOrUpdateMapCommandHandler : IConsumer<CreateOrUpdateMapCommand>
{
private readonly ILogger<CreateOrUpdateMapCommandHandler> _logger;
private readonly IMapRepository _mapRepository;
public CreateOrUpdateMapCommandHandler(
ILogger<CreateOrUpdateMapCommandHandler> logger,
IMapRepository mapRepository)
{
_logger = logger;
_mapRepository = mapRepository;
}
public async Task Consume(ConsumeContext<CreateOrUpdateMapCommand> context)
{
var command = context.Message;
try
{
if(!Guid.TryParse(command.MapId, out Guid mapId))
{
// 检查地图编码是否已存在
var existingMap = await _mapRepository.GetByMapCodeAsync(
command.MapCode,
context.CancellationToken);
if (existingMap != null)
{
throw new InvalidOperationException($"地图编码 {command.MapCode} 已存在");
}
// 创建地图实体
var map = Map.Create
(
command.MapCode,
command.MapName,
(MapTYPE)command.MapType,
command.Version,
command.Description,
command.Active,
command.ResourceUrl,
command.PointsUrl,
command.ResourceAutoSync,
command.ResourceSyncInterval,
command.PointsAutoSync,
command.PointsSyncInterval
);
// 保存地图到数据库
await _mapRepository.AddAsync(map, context.CancellationToken);
}
else
{
var map = await _mapRepository.GetByIdAsync(
mapId,
context.CancellationToken);
if (map == null)
throw new InvalidOperationException($"未找到地图ID为 {mapId} 的地图");
map.Update
(
command.MapCode,
command.MapName,
(MapTYPE)(command.MapType),
command.Version,
command.Description,
command.Active,
command.ResourceUrl,
command.PointsUrl,
command.ResourceAutoSync,
command.ResourceSyncInterval,
command.PointsAutoSync,
command.PointsSyncInterval
);
await _mapRepository.UpdateAsync(map, context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}