diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 62fa5d3f..496e4729 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -26,6 +26,7 @@ using System; namespace AsbCloudInfrastructure { +#nullable enable public static class DependencyInjection { public static IAsbCloudDbContext MakeContext(string connectionString) @@ -49,7 +50,7 @@ namespace AsbCloudInfrastructure TypeAdapterConfig.GlobalSettings.Default.Config .ForType() - .MapWith((source) => source == default ? default : source.MakeTimeOnly()); + .MapWith((source) => source.MakeTimeOnly()); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() @@ -59,6 +60,7 @@ namespace AsbCloudInfrastructure .ForType() .MapWith((source) => new(source)); +#pragma warning disable CS8603 // Possible null reference return. TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .Ignore(dst => dst.Cluster, @@ -68,6 +70,7 @@ namespace AsbCloudInfrastructure dst => dst.WellCompositeSrcs, dst => dst.WellOperations, dst => dst.WellType); +#pragma warning restore CS8603 // Possible null reference return. TypeAdapterConfig.GlobalSettings.Default.Config .ForType() @@ -93,7 +96,7 @@ namespace AsbCloudInfrastructure services.AddMemoryCache(); - services.AddScoped(provider => provider.GetService()); + services.AddScoped(provider => provider.GetRequiredService()); services.AddScoped(); services.AddSingleton(new WitsInfoService()); @@ -140,25 +143,25 @@ namespace AsbCloudInfrastructure // admin crud services: services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( - s.GetService(), - s.GetService(), + s.GetRequiredService(), + s.GetRequiredService(), dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( - s.GetService(), - s.GetService(), + s.GetRequiredService(), + s.GetRequiredService(), dbSet => dbSet.Include(d => d.Clusters))); services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( - s.GetService(), - s.GetService(), + s.GetRequiredService(), + s.GetRequiredService(), dbSet => dbSet.Include(c => c.CompanyType))); services.AddTransient, CrudCacheRepositoryBase>(); services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( - s.GetService(), - s.GetService(), + s.GetRequiredService(), + s.GetRequiredService(), dbSet => dbSet .Include(c => c.Wells) .Include(c => c.Deposit))); // может быть включен в сервис ClusterService @@ -200,7 +203,7 @@ namespace AsbCloudInfrastructure where TService : class where TImplementation : class, TService => services.AddTransient() - .AddTransient(provider => new Lazy(provider.GetService)); + .AddTransient(provider => new Lazy(provider.GetRequiredService)); public static IServiceCollection AddTransientLazy(this IServiceCollection services, Func implementationFactory) where TService : class @@ -209,4 +212,5 @@ namespace AsbCloudInfrastructure .AddTransient(provider => new Lazy(() => implementationFactory(provider))); } +#nullable disable } diff --git a/AsbCloudInfrastructure/Services/MeasureService.cs b/AsbCloudInfrastructure/Services/MeasureService.cs index 56157786..d32a7882 100644 --- a/AsbCloudInfrastructure/Services/MeasureService.cs +++ b/AsbCloudInfrastructure/Services/MeasureService.cs @@ -13,6 +13,7 @@ using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { +#nullable enable public class MeasureService : IMeasureService { private readonly IAsbCloudDbContext db; @@ -40,7 +41,7 @@ namespace AsbCloudInfrastructure.Services return cache; } - public async Task GetLastAsync(int idWell, int idCategory, CancellationToken token) + public async Task GetLastOrDefaultAsync(int idWell, int idCategory, CancellationToken token) { var query = db.Measures .Include(m => m.Category) @@ -54,8 +55,11 @@ namespace AsbCloudInfrastructure.Services .ConfigureAwait(false); var timezone = wellService.GetTimezone(idWell); - var dto = Convert(entity, timezone.Hours); - return dto; + + if (entity is null) + return null; + + return Convert(entity, timezone.Hours); } public async Task> GetHisoryAsync(int idWell, int? idCategory = null, CancellationToken token = default) @@ -124,6 +128,9 @@ namespace AsbCloudInfrastructure.Services .FirstOrDefaultAsync(token) .ConfigureAwait(false); + if (entity is null) + throw new ArgumentInvalidException($"Measure doesn't exist", nameof(idWell)); + entity.IsDeleted = true; return await db.SaveChangesAsync(token).ConfigureAwait(false); @@ -140,16 +147,16 @@ namespace AsbCloudInfrastructure.Services private MeasureDto Convert(Measure entity, double hours) { var dto = entity.Adapt(); - dto.CategoryName = entity.Category?.Name; + dto.CategoryName = entity.Category?.Name ?? String.Empty; dto.Timestamp = entity.Timestamp.ToRemoteDateTime(hours); return dto; } private Measure Convert(MeasureDto dto, double hours) { var entity = dto.Adapt(); - entity.Category = null; entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(hours); return entity; } } +#nullable disable } diff --git a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs index e9ed3d11..fe8c77d1 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationImportService.cs @@ -11,6 +11,7 @@ using System.Linq; namespace AsbCloudInfrastructure.Services.WellOperationService { +#nullable enable /* * password for WellOperationImportTemplate.xlsx is ASB2020! */ @@ -36,7 +37,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService private readonly IAsbCloudDbContext db; private readonly IWellService wellService; - private List categories = null; + private List categories = null!; public List Categories { get @@ -52,7 +53,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService } } - private List sections = null; + private List sections = null!; public List Sections { get @@ -92,9 +93,6 @@ namespace AsbCloudInfrastructure.Services.WellOperationService .AsNoTracking() .ToList(); - if (!operations.Any()) - return null; - var timezone = wellService.GetTimezone(idWell); return MakeExelFileStream(operations, timezone.Hours); @@ -102,8 +100,12 @@ namespace AsbCloudInfrastructure.Services.WellOperationService public Stream GetExcelTemplateStream() { + var resourceName = System.Reflection.Assembly.GetExecutingAssembly() + .GetManifestResourceNames() + .FirstOrDefault(n => n.EndsWith("WellOperationImportTemplate.xlsx"))!; + var stream = System.Reflection.Assembly.GetExecutingAssembly() - .GetManifestResourceStream("AsbCloudInfrastructure.Services.WellOperationService.WellOperationImportTemplate.xlsx"); + .GetManifestResourceStream(resourceName)!; return stream; } @@ -126,14 +128,16 @@ namespace AsbCloudInfrastructure.Services.WellOperationService if (planOperations.Any()) { var sheetPlan = workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetNamePlan); - AddOperationsToSheet(sheetPlan, planOperations, timezoneOffset); + if (sheetPlan is not null) + AddOperationsToSheet(sheetPlan, planOperations, timezoneOffset); } var factOperations = operations.Where(o => o.IdType == 1); if (factOperations.Any()) { var sheetFact = workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetNameFact); - AddOperationsToSheet(sheetFact, factOperations, timezoneOffset); + if (sheetFact is not null) + AddOperationsToSheet(sheetFact, factOperations, timezoneOffset); } } @@ -328,4 +332,5 @@ namespace AsbCloudInfrastructure.Services.WellOperationService return operation; } } +#nullable disable }