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

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) public override async Task<int> InsertAsync(WellDto dto, CancellationToken token)
{ {
if (await IsExistingTelemetryAsync(dto.IdTelemetry, token)) if (IsTelemetryAssignedToDifferentWell(dto))
throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto)); throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto));
if (dto.IdWellType is < 1 or > 2) if (dto.IdWellType is < 1 or > 2)
@ -163,7 +163,7 @@ namespace AsbCloudInfrastructure.Services
public override async Task<int> UpdateAsync(WellDto dto, public override async Task<int> UpdateAsync(WellDto dto,
CancellationToken token) CancellationToken token)
{ {
if (await IsExistingTelemetryAsync(dto.IdTelemetry, token)) if (IsTelemetryAssignedToDifferentWell(dto))
throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto)); throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto));
if (dto.IdWellType is < 1 or > 2) 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}"); 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) if (!wellDto.IdTelemetry.HasValue)
return Task.FromResult(false); return false;
return dbSet.AnyAsync(x => x.IdTelemetry == var existingWellWithAssignedTelemetry = GetCache()
idTelemetry.Value, cancellationToken); .FirstOrDefault(x => x.IdTelemetry == wellDto.IdTelemetry);
if (existingWellWithAssignedTelemetry is null)
return false;
return existingWellWithAssignedTelemetry.Id != wellDto.Id;
} }
private static AsbCloudDb.Model.IMapPoint GetCoordinates(Well well) private static AsbCloudDb.Model.IMapPoint GetCoordinates(Well well)