From 90a0aa9e5ba3c4f2a9a64206ee61cc6cfb0ad486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 10 Jan 2022 17:39:33 +0500 Subject: [PATCH] Add TryGetTimezone methods. EnshureTimezonesIsSet() sets default timezone if can't find correct one. Edit WellDto to fix exception by constr: FK_t_well_t_well_type_id_well_type. Make IdWellType optional. --- AsbCloudApp/Data/WellDto.cs | 2 +- .../Services/WellService.cs | 47 +++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/AsbCloudApp/Data/WellDto.cs b/AsbCloudApp/Data/WellDto.cs index 0eac356d..f89c6956 100644 --- a/AsbCloudApp/Data/WellDto.cs +++ b/AsbCloudApp/Data/WellDto.cs @@ -10,7 +10,7 @@ namespace AsbCloudApp.Data public double? Longitude { get; set; } public SimpleTimezoneDto Timezone { get; set; } public string WellType { get; set; } - public int IdWellType { get; set; } + public int? IdWellType { get; set; } public int? IdCluster { get; set; } /// diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 05a705f5..14d5c2b7 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -184,7 +184,8 @@ namespace AsbCloudInfrastructure.Services entity.IdTelemetry = entity.IdTelemetry ?? dto.IdTelemetry ?? dto.Telemetry?.Id; if (dto.Timezone is null) - entity.Timezone = GetTimezone(dto.Id).Adapt(); + if (TryGetTimezone(dto.Id, out var timezoneDto)) + entity.Timezone = timezoneDto.Adapt(); return entity; } @@ -197,7 +198,8 @@ namespace AsbCloudInfrastructure.Services var dto = base.Convert(entity); if (entity.Timezone is null) - dto.Timezone = GetTimezone(entity); + if(TryGetTimezone(entity, out var timezone)) + dto.Timezone = timezone; dto.WellType = entity.WellType?.Caption; dto.Cluster = entity.Cluster?.Caption; @@ -219,7 +221,18 @@ namespace AsbCloudInfrastructure.Services { var wells = Cache.Where(w => w.Timezone is null).ToList(); foreach (var well in wells) - well.Timezone = GetTimezone(well).Adapt(); + { + if (TryGetTimezone(well, out var timezone)) + well.Timezone = timezone.Adapt(); + else + well.Timezone = new SimpleTimezone + { + Hours = 5, + IsOverride = false, + TimeZoneId = "Assumed", + }; + } + var wellsWithTz = wells.Where(w => w.Timezone is not null); if (wellsWithTz.Any()) { @@ -228,6 +241,20 @@ namespace AsbCloudInfrastructure.Services } } + private bool TryGetTimezone(int idWell, out SimpleTimezoneDto timezone) + { + timezone = null; + try + { + timezone = GetTimezone(idWell); + return timezone is not null; + } + catch + { + return false; + } + } + public SimpleTimezoneDto GetTimezone(int idWell) { var well = Cache.FirstOrDefault(c => c.Id == idWell); @@ -236,6 +263,20 @@ namespace AsbCloudInfrastructure.Services return GetTimezone(well); } + private bool TryGetTimezone(Well well, out SimpleTimezoneDto timezone) + { + timezone = null; + try + { + timezone = GetTimezone(well); + return timezone is not null; + } + catch + { + return false; + } + } + private SimpleTimezoneDto GetTimezone(Well well) { if (well == null)