From 43483d3e067110386c0d6e4565b39518fb7cf961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Tue, 4 Jul 2023 10:34:59 +0500 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Переименовал метод проверки 2. Сделал получение данных из кэша 3. Переделал логику проверки --- .../Services/WellService.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 01f9cb32..d55bd2a5 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -128,7 +128,7 @@ namespace AsbCloudInfrastructure.Services public override async Task 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 UpdateAsync(WellDto dto, + public override async Task 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 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)