nullable enable (часть 3)

This commit is contained in:
Olga Nemt 2023-04-14 10:02:38 +05:00
parent 5100a221e0
commit cb9e8dd672
3 changed files with 40 additions and 24 deletions

View File

@ -26,6 +26,7 @@ using System;
namespace AsbCloudInfrastructure namespace AsbCloudInfrastructure
{ {
#nullable enable
public static class DependencyInjection public static class DependencyInjection
{ {
public static IAsbCloudDbContext MakeContext(string connectionString) public static IAsbCloudDbContext MakeContext(string connectionString)
@ -49,7 +50,7 @@ namespace AsbCloudInfrastructure
TypeAdapterConfig.GlobalSettings.Default.Config TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<TimeDto, TimeOnly>() .ForType<TimeDto, TimeOnly>()
.MapWith((source) => source == default ? default : source.MakeTimeOnly()); .MapWith((source) => source.MakeTimeOnly());
TypeAdapterConfig.GlobalSettings.Default.Config TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<TimeOnly, TimeDto>() .ForType<TimeOnly, TimeDto>()
@ -59,6 +60,7 @@ namespace AsbCloudInfrastructure
.ForType<TimeOnly, TimeDto>() .ForType<TimeOnly, TimeDto>()
.MapWith((source) => new(source)); .MapWith((source) => new(source));
#pragma warning disable CS8603 // Possible null reference return.
TypeAdapterConfig.GlobalSettings.Default.Config TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<WellDto, Well>() .ForType<WellDto, Well>()
.Ignore(dst => dst.Cluster, .Ignore(dst => dst.Cluster,
@ -68,6 +70,7 @@ namespace AsbCloudInfrastructure
dst => dst.WellCompositeSrcs, dst => dst.WellCompositeSrcs,
dst => dst.WellOperations, dst => dst.WellOperations,
dst => dst.WellType); dst => dst.WellType);
#pragma warning restore CS8603 // Possible null reference return.
TypeAdapterConfig.GlobalSettings.Default.Config TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<ClusterDto, Cluster>() .ForType<ClusterDto, Cluster>()
@ -93,7 +96,7 @@ namespace AsbCloudInfrastructure
services.AddMemoryCache(); services.AddMemoryCache();
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>()); services.AddScoped<IAsbCloudDbContext>(provider => provider.GetRequiredService<AsbCloudDbContext>());
services.AddScoped<IEmailService, EmailService>(); services.AddScoped<IEmailService, EmailService>();
services.AddSingleton(new WitsInfoService()); services.AddSingleton(new WitsInfoService());
@ -140,25 +143,25 @@ namespace AsbCloudInfrastructure
// admin crud services: // admin crud services:
services.AddTransient<ICrudRepository<TelemetryDto>, CrudCacheRepositoryBase<TelemetryDto, Telemetry>>(s => services.AddTransient<ICrudRepository<TelemetryDto>, CrudCacheRepositoryBase<TelemetryDto, Telemetry>>(s =>
new CrudCacheRepositoryBase<TelemetryDto, Telemetry>( new CrudCacheRepositoryBase<TelemetryDto, Telemetry>(
s.GetService<IAsbCloudDbContext>(), s.GetRequiredService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(), s.GetRequiredService<IMemoryCache>(),
dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService
services.AddTransient<ICrudRepository<DepositDto>, CrudCacheRepositoryBase<DepositDto, Deposit>>(s => services.AddTransient<ICrudRepository<DepositDto>, CrudCacheRepositoryBase<DepositDto, Deposit>>(s =>
new CrudCacheRepositoryBase<DepositDto, Deposit>( new CrudCacheRepositoryBase<DepositDto, Deposit>(
s.GetService<IAsbCloudDbContext>(), s.GetRequiredService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(), s.GetRequiredService<IMemoryCache>(),
dbSet => dbSet.Include(d => d.Clusters))); dbSet => dbSet.Include(d => d.Clusters)));
services.AddTransient<ICrudRepository<CompanyDto>, CrudCacheRepositoryBase<CompanyDto, Company>>(s => services.AddTransient<ICrudRepository<CompanyDto>, CrudCacheRepositoryBase<CompanyDto, Company>>(s =>
new CrudCacheRepositoryBase<CompanyDto, Company>( new CrudCacheRepositoryBase<CompanyDto, Company>(
s.GetService<IAsbCloudDbContext>(), s.GetRequiredService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(), s.GetRequiredService<IMemoryCache>(),
dbSet => dbSet.Include(c => c.CompanyType))); dbSet => dbSet.Include(c => c.CompanyType)));
services.AddTransient<ICrudRepository<CompanyTypeDto>, CrudCacheRepositoryBase<CompanyTypeDto, CompanyType>>(); services.AddTransient<ICrudRepository<CompanyTypeDto>, CrudCacheRepositoryBase<CompanyTypeDto, CompanyType>>();
services.AddTransient<ICrudRepository<ClusterDto>, CrudCacheRepositoryBase<ClusterDto, Cluster>>(s => services.AddTransient<ICrudRepository<ClusterDto>, CrudCacheRepositoryBase<ClusterDto, Cluster>>(s =>
new CrudCacheRepositoryBase<ClusterDto, Cluster>( new CrudCacheRepositoryBase<ClusterDto, Cluster>(
s.GetService<IAsbCloudDbContext>(), s.GetRequiredService<IAsbCloudDbContext>(),
s.GetService<IMemoryCache>(), s.GetRequiredService<IMemoryCache>(),
dbSet => dbSet dbSet => dbSet
.Include(c => c.Wells) .Include(c => c.Wells)
.Include(c => c.Deposit))); // может быть включен в сервис ClusterService .Include(c => c.Deposit))); // может быть включен в сервис ClusterService
@ -200,7 +203,7 @@ namespace AsbCloudInfrastructure
where TService : class where TService : class
where TImplementation : class, TService where TImplementation : class, TService
=> services.AddTransient<TService, TImplementation>() => services.AddTransient<TService, TImplementation>()
.AddTransient(provider => new Lazy<TService>(provider.GetService<TService>)); .AddTransient(provider => new Lazy<TService>(provider.GetRequiredService<TService>));
public static IServiceCollection AddTransientLazy<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory) public static IServiceCollection AddTransientLazy<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
where TService : class where TService : class
@ -209,4 +212,5 @@ namespace AsbCloudInfrastructure
.AddTransient(provider => new Lazy<TService>(() => implementationFactory(provider))); .AddTransient(provider => new Lazy<TService>(() => implementationFactory(provider)));
} }
#nullable disable
} }

View File

@ -13,6 +13,7 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services namespace AsbCloudInfrastructure.Services
{ {
#nullable enable
public class MeasureService : IMeasureService public class MeasureService : IMeasureService
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
@ -40,7 +41,7 @@ namespace AsbCloudInfrastructure.Services
return cache; return cache;
} }
public async Task<MeasureDto> GetLastAsync(int idWell, int idCategory, CancellationToken token) public async Task<MeasureDto?> GetLastOrDefaultAsync(int idWell, int idCategory, CancellationToken token)
{ {
var query = db.Measures var query = db.Measures
.Include(m => m.Category) .Include(m => m.Category)
@ -54,8 +55,11 @@ namespace AsbCloudInfrastructure.Services
.ConfigureAwait(false); .ConfigureAwait(false);
var timezone = wellService.GetTimezone(idWell); 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<IEnumerable<MeasureDto>> GetHisoryAsync(int idWell, int? idCategory = null, CancellationToken token = default) public async Task<IEnumerable<MeasureDto>> GetHisoryAsync(int idWell, int? idCategory = null, CancellationToken token = default)
@ -124,6 +128,9 @@ namespace AsbCloudInfrastructure.Services
.FirstOrDefaultAsync(token) .FirstOrDefaultAsync(token)
.ConfigureAwait(false); .ConfigureAwait(false);
if (entity is null)
throw new ArgumentInvalidException($"Measure doesn't exist", nameof(idWell));
entity.IsDeleted = true; entity.IsDeleted = true;
return await db.SaveChangesAsync(token).ConfigureAwait(false); return await db.SaveChangesAsync(token).ConfigureAwait(false);
@ -140,16 +147,16 @@ namespace AsbCloudInfrastructure.Services
private MeasureDto Convert(Measure entity, double hours) private MeasureDto Convert(Measure entity, double hours)
{ {
var dto = entity.Adapt<MeasureDto>(); var dto = entity.Adapt<MeasureDto>();
dto.CategoryName = entity.Category?.Name; dto.CategoryName = entity.Category?.Name ?? String.Empty;
dto.Timestamp = entity.Timestamp.ToRemoteDateTime(hours); dto.Timestamp = entity.Timestamp.ToRemoteDateTime(hours);
return dto; return dto;
} }
private Measure Convert(MeasureDto dto, double hours) private Measure Convert(MeasureDto dto, double hours)
{ {
var entity = dto.Adapt<Measure>(); var entity = dto.Adapt<Measure>();
entity.Category = null;
entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(hours); entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(hours);
return entity; return entity;
} }
} }
#nullable disable
} }

View File

@ -11,6 +11,7 @@ using System.Linq;
namespace AsbCloudInfrastructure.Services.WellOperationService namespace AsbCloudInfrastructure.Services.WellOperationService
{ {
#nullable enable
/* /*
* password for WellOperationImportTemplate.xlsx is ASB2020! * password for WellOperationImportTemplate.xlsx is ASB2020!
*/ */
@ -36,7 +37,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly IWellService wellService; private readonly IWellService wellService;
private List<WellOperationCategory> categories = null; private List<WellOperationCategory> categories = null!;
public List<WellOperationCategory> Categories public List<WellOperationCategory> Categories
{ {
get get
@ -52,7 +53,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
} }
} }
private List<WellSectionType> sections = null; private List<WellSectionType> sections = null!;
public List<WellSectionType> Sections public List<WellSectionType> Sections
{ {
get get
@ -92,9 +93,6 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
.AsNoTracking() .AsNoTracking()
.ToList(); .ToList();
if (!operations.Any())
return null;
var timezone = wellService.GetTimezone(idWell); var timezone = wellService.GetTimezone(idWell);
return MakeExelFileStream(operations, timezone.Hours); return MakeExelFileStream(operations, timezone.Hours);
@ -102,8 +100,12 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
public Stream GetExcelTemplateStream() public Stream GetExcelTemplateStream()
{ {
var resourceName = System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceNames()
.FirstOrDefault(n => n.EndsWith("WellOperationImportTemplate.xlsx"))!;
var stream = System.Reflection.Assembly.GetExecutingAssembly() var stream = System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream("AsbCloudInfrastructure.Services.WellOperationService.WellOperationImportTemplate.xlsx"); .GetManifestResourceStream(resourceName)!;
return stream; return stream;
} }
@ -126,14 +128,16 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
if (planOperations.Any()) if (planOperations.Any())
{ {
var sheetPlan = workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetNamePlan); 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); var factOperations = operations.Where(o => o.IdType == 1);
if (factOperations.Any()) if (factOperations.Any())
{ {
var sheetFact = workbook.Worksheets.FirstOrDefault(ws => ws.Name == sheetNameFact); 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; return operation;
} }
} }
#nullable disable
} }