diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 7645c0aa..01f9cb32 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -126,8 +126,11 @@ namespace AsbCloudInfrastructure.Services return wells; } - public override async Task InsertAsync(WellDto dto, CancellationToken token = default) + public override async Task InsertAsync(WellDto dto, CancellationToken token) { + if (await IsExistingTelemetryAsync(dto.IdTelemetry, token)) + throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto)); + if (dto.IdWellType is < 1 or > 2) throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto)); @@ -158,8 +161,11 @@ namespace AsbCloudInfrastructure.Services } public override async Task UpdateAsync(WellDto dto, - CancellationToken token = default) + CancellationToken token) { + if (await IsExistingTelemetryAsync(dto.IdTelemetry, token)) + throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto)); + if (dto.IdWellType is < 1 or > 2) throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto)); @@ -335,6 +341,15 @@ namespace AsbCloudInfrastructure.Services throw new Exception($"Can't find timezone for well {wellDto.Caption} id: {wellDto.Id}"); } + private Task IsExistingTelemetryAsync(int? idTelemetry, CancellationToken cancellationToken) + { + if (!idTelemetry.HasValue) + return Task.FromResult(false); + + return dbSet.AnyAsync(x => x.IdTelemetry == + idTelemetry.Value, cancellationToken); + } + private static AsbCloudDb.Model.IMapPoint GetCoordinates(Well well) { if (well is null)