CreateOrUpdateStorageLocationCommandHandler.cs
9.27 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands.StorageLocations;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands.StorageLocations
{
/// <summary>
/// Create or update storage location command handler.
/// </summary>
public class CreateOrUpdateStorageLocationCommandHandler : IConsumer<CreateOrUpdateStorageLocationCommand>
{
private readonly ILogger<CreateOrUpdateStorageLocationCommandHandler> _logger;
private readonly IStorageLocationRepository _repository;
private readonly IStorageAreaRepository _storageAreaRepository;
private readonly IStorageLocationTypeRepository _storageLocationTypeRepository;
private readonly IMapNodeRepository _mapNodeRepository;
public CreateOrUpdateStorageLocationCommandHandler(
ILogger<CreateOrUpdateStorageLocationCommandHandler> logger,
IStorageLocationRepository repository,
IStorageAreaRepository storageAreaRepository,
IStorageLocationTypeRepository storageLocationTypeRepository,
IMapNodeRepository mapNodeRepository)
{
_logger = logger;
_repository = repository;
_storageAreaRepository = storageAreaRepository;
_storageLocationTypeRepository = storageLocationTypeRepository;
_mapNodeRepository = mapNodeRepository;
}
public async Task Consume(ConsumeContext<CreateOrUpdateStorageLocationCommand> context)
{
var command = context.Message;
try
{
ValidateCommand(command);
if (!Guid.TryParse(command.AreaId, out var areaId))
{
throw new InvalidOperationException("AreaId format is invalid");
}
var area = await _storageAreaRepository.GetByIdAsync(areaId, context.CancellationToken);
if (area == null)
{
throw new InvalidOperationException($"Storage area not found: {areaId}");
}
Guid? locationTypeId = null;
if (!string.IsNullOrWhiteSpace(command.LocationTypeId))
{
if (!Guid.TryParse(command.LocationTypeId, out var parsedLocationTypeId))
{
throw new InvalidOperationException("LocationTypeId format is invalid");
}
var locationType = await _storageLocationTypeRepository.GetByIdAsync(parsedLocationTypeId, context.CancellationToken);
if (locationType == null)
{
throw new InvalidOperationException($"Storage location type not found: {parsedLocationTypeId}");
}
locationTypeId = parsedLocationTypeId;
}
Guid? mapNodeId = null;
if (!string.IsNullOrWhiteSpace(command.MapNodeId))
{
var rawMapNodeValue = command.MapNodeId.Trim();
MapNode? mapNode = null;
if (Guid.TryParse(rawMapNodeValue, out var parsedMapNodeId))
{
mapNode = await _mapNodeRepository.GetByIdAsync(parsedMapNodeId, context.CancellationToken);
}
else
{
mapNode = await _mapNodeRepository.GetByNodeCodeAsync(rawMapNodeValue, context.CancellationToken);
}
if (mapNode == null)
{
throw new InvalidOperationException($"Map node not found: {rawMapNodeValue}");
}
mapNodeId = mapNode.NodeId;
}
var locationCode = command.LocationCode.Trim();
if (!Guid.TryParse(command.LocationId, out var locationId))
{
var existingByCode = await _repository.GetByLocationCodeAsync(locationCode, context.CancellationToken);
if (existingByCode != null)
{
throw new InvalidOperationException($"LocationCode already exists: {locationCode}");
}
if (mapNodeId.HasValue)
{
var existingByMapNode = await _repository.GetQueryable()
.FirstOrDefaultAsync(x => x.MapNodeId == mapNodeId.Value, context.CancellationToken);
if (existingByMapNode != null)
{
throw new InvalidOperationException($"Map node already bound by another storage location: {mapNodeId.Value}");
}
}
var entity = new StorageLocation
{
LocationId = Guid.NewGuid(),
AreaId = areaId,
LocationTypeId = locationTypeId,
MapNodeId = mapNodeId,
LocationCode = locationCode,
LocationName = string.IsNullOrWhiteSpace(command.LocationName) ? null : command.LocationName.Trim(),
LayerNumber = command.LayerNumber,
Orientation = command.Orientation,
Width = command.Width,
Depth = command.Depth,
Height = command.Height,
MaxLoadWeight = command.MaxLoadWeight,
Status = (StorageLocationStatus)command.Status,
IsActive = command.IsActive,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
};
await _repository.AddAsync(entity, context.CancellationToken);
await _repository.SaveChangesAsync(context.CancellationToken);
}
else
{
var entity = await _repository.GetByIdAsync(locationId, context.CancellationToken);
if (entity == null)
{
throw new InvalidOperationException($"Storage location not found: {locationId}");
}
var existingByCode = await _repository.GetByLocationCodeAsync(locationCode, context.CancellationToken);
if (existingByCode != null && existingByCode.LocationId != locationId)
{
throw new InvalidOperationException($"LocationCode already exists: {locationCode}");
}
if (mapNodeId.HasValue)
{
var existingByMapNode = await _repository.GetQueryable()
.FirstOrDefaultAsync(x => x.MapNodeId == mapNodeId.Value, context.CancellationToken);
if (existingByMapNode != null && existingByMapNode.LocationId != locationId)
{
throw new InvalidOperationException($"Map node already bound by another storage location: {mapNodeId.Value}");
}
}
entity.AreaId = areaId;
entity.LocationTypeId = locationTypeId;
entity.MapNodeId = mapNodeId;
entity.LocationCode = locationCode;
entity.LocationName = string.IsNullOrWhiteSpace(command.LocationName) ? null : command.LocationName.Trim();
entity.LayerNumber = command.LayerNumber;
entity.Orientation = command.Orientation;
entity.Width = command.Width;
entity.Depth = command.Depth;
entity.Height = command.Height;
entity.MaxLoadWeight = command.MaxLoadWeight;
entity.Status = (StorageLocationStatus)command.Status;
entity.IsActive = command.IsActive;
entity.UpdatedAt = DateTime.Now;
await _repository.UpdateAsync(entity, context.CancellationToken);
await _repository.SaveChangesAsync(context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
_logger.LogError(ex, "Create or update storage location failed");
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
private static void ValidateCommand(CreateOrUpdateStorageLocationCommand command)
{
if (string.IsNullOrWhiteSpace(command.LocationCode))
{
throw new InvalidOperationException("LocationCode is required");
}
if (string.IsNullOrWhiteSpace(command.AreaId))
{
throw new InvalidOperationException("AreaId is required");
}
if (!Enum.IsDefined(typeof(StorageLocationStatus), command.Status))
{
throw new InvalidOperationException($"Status is invalid: {command.Status}");
}
}
}
}