diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 7645c0aa..d55bd2a5 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 (IsTelemetryAssignedToDifferentWell(dto)) + throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto)); + if (dto.IdWellType is < 1 or > 2) throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto)); @@ -157,9 +160,12 @@ namespace AsbCloudInfrastructure.Services throw new NotImplementedException(); } - public override async Task UpdateAsync(WellDto dto, - CancellationToken token = default) + public override async Task UpdateAsync(WellDto dto, + CancellationToken token) { + if (IsTelemetryAssignedToDifferentWell(dto)) + throw new ArgumentInvalidException("Телеметрия уже была привязана к другой скважине.", nameof(dto)); + if (dto.IdWellType is < 1 or > 2) throw new ArgumentInvalidException("Тип скважины указан неправильно.", nameof(dto)); @@ -335,6 +341,20 @@ namespace AsbCloudInfrastructure.Services throw new Exception($"Can't find timezone for well {wellDto.Caption} id: {wellDto.Id}"); } + private bool IsTelemetryAssignedToDifferentWell(WellDto wellDto) + { + if (!wellDto.IdTelemetry.HasValue) + return false; + + var existingWellWithAssignedTelemetry = GetCache() + .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) { if (well is null)