Исправления после ревью

1. Переименовал метод проверки
2. Сделал получение данных из кэша
3. Переделал логику проверки
This commit is contained in:
parent 7480b63ccb
commit 43483d3e06

View File

@ -128,7 +128,7 @@ namespace AsbCloudInfrastructure.Services
public override async Task<int> InsertAsync(WellDto dto, CancellationToken token)
{
if (await IsExistingTelemetryAsync(dto.IdTelemetry, token))
if (IsTelemetryAssignedToDifferentWell(dto))
throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto));
if (dto.IdWellType is < 1 or > 2)
@ -160,10 +160,10 @@ namespace AsbCloudInfrastructure.Services
throw new NotImplementedException();
}
public override async Task<int> UpdateAsync(WellDto dto,
public override async Task<int> UpdateAsync(WellDto dto,
CancellationToken token)
{
if (await IsExistingTelemetryAsync(dto.IdTelemetry, token))
if (IsTelemetryAssignedToDifferentWell(dto))
throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto));
if (dto.IdWellType is < 1 or > 2)
@ -341,13 +341,18 @@ namespace AsbCloudInfrastructure.Services
throw new Exception($"Can't find timezone for well {wellDto.Caption} id: {wellDto.Id}");
}
private Task<bool> IsExistingTelemetryAsync(int? idTelemetry, CancellationToken cancellationToken)
private bool IsTelemetryAssignedToDifferentWell(WellDto wellDto)
{
if (!idTelemetry.HasValue)
return Task.FromResult(false);
if (!wellDto.IdTelemetry.HasValue)
return false;
var existingWellWithAssignedTelemetry = GetCache()
.FirstOrDefault(x => x.IdTelemetry == wellDto.IdTelemetry);
if (existingWellWithAssignedTelemetry is null)
return false;
return dbSet.AnyAsync(x => x.IdTelemetry ==
idTelemetry.Value, cancellationToken);
return existingWellWithAssignedTelemetry.Id != wellDto.Id;
}
private static AsbCloudDb.Model.IMapPoint GetCoordinates(Well well)