forked from ddrilling/AsbCloudServer
nullable enable (часть 3)
This commit is contained in:
parent
5100a221e0
commit
cb9e8dd672
@ -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<TimeDto, TimeOnly>()
|
||||
.MapWith((source) => source == default ? default : source.MakeTimeOnly());
|
||||
.MapWith((source) => source.MakeTimeOnly());
|
||||
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<TimeOnly, TimeDto>()
|
||||
@ -59,6 +60,7 @@ namespace AsbCloudInfrastructure
|
||||
.ForType<TimeOnly, TimeDto>()
|
||||
.MapWith((source) => new(source));
|
||||
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
TypeAdapterConfig.GlobalSettings.Default.Config
|
||||
.ForType<WellDto, Well>()
|
||||
.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<ClusterDto, Cluster>()
|
||||
@ -93,7 +96,7 @@ namespace AsbCloudInfrastructure
|
||||
|
||||
|
||||
services.AddMemoryCache();
|
||||
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>());
|
||||
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetRequiredService<AsbCloudDbContext>());
|
||||
services.AddScoped<IEmailService, EmailService>();
|
||||
|
||||
services.AddSingleton(new WitsInfoService());
|
||||
@ -140,25 +143,25 @@ namespace AsbCloudInfrastructure
|
||||
// admin crud services:
|
||||
services.AddTransient<ICrudRepository<TelemetryDto>, CrudCacheRepositoryBase<TelemetryDto, Telemetry>>(s =>
|
||||
new CrudCacheRepositoryBase<TelemetryDto, Telemetry>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
s.GetRequiredService<IAsbCloudDbContext>(),
|
||||
s.GetRequiredService<IMemoryCache>(),
|
||||
dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService
|
||||
services.AddTransient<ICrudRepository<DepositDto>, CrudCacheRepositoryBase<DepositDto, Deposit>>(s =>
|
||||
new CrudCacheRepositoryBase<DepositDto, Deposit>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
s.GetRequiredService<IAsbCloudDbContext>(),
|
||||
s.GetRequiredService<IMemoryCache>(),
|
||||
dbSet => dbSet.Include(d => d.Clusters)));
|
||||
services.AddTransient<ICrudRepository<CompanyDto>, CrudCacheRepositoryBase<CompanyDto, Company>>(s =>
|
||||
new CrudCacheRepositoryBase<CompanyDto, Company>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
s.GetRequiredService<IAsbCloudDbContext>(),
|
||||
s.GetRequiredService<IMemoryCache>(),
|
||||
dbSet => dbSet.Include(c => c.CompanyType)));
|
||||
|
||||
services.AddTransient<ICrudRepository<CompanyTypeDto>, CrudCacheRepositoryBase<CompanyTypeDto, CompanyType>>();
|
||||
services.AddTransient<ICrudRepository<ClusterDto>, CrudCacheRepositoryBase<ClusterDto, Cluster>>(s =>
|
||||
new CrudCacheRepositoryBase<ClusterDto, Cluster>(
|
||||
s.GetService<IAsbCloudDbContext>(),
|
||||
s.GetService<IMemoryCache>(),
|
||||
s.GetRequiredService<IAsbCloudDbContext>(),
|
||||
s.GetRequiredService<IMemoryCache>(),
|
||||
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<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)
|
||||
where TService : class
|
||||
@ -209,4 +212,5 @@ namespace AsbCloudInfrastructure
|
||||
.AddTransient(provider => new Lazy<TService>(() => implementationFactory(provider)));
|
||||
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
|
@ -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<MeasureDto> GetLastAsync(int idWell, int idCategory, CancellationToken token)
|
||||
public async Task<MeasureDto?> 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<IEnumerable<MeasureDto>> 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<MeasureDto>();
|
||||
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<Measure>();
|
||||
entity.Category = null;
|
||||
entity.Timestamp = dto.Timestamp.ToUtcDateTimeOffset(hours);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
|
@ -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<WellOperationCategory> categories = null;
|
||||
private List<WellOperationCategory> categories = null!;
|
||||
public List<WellOperationCategory> Categories
|
||||
{
|
||||
get
|
||||
@ -52,7 +53,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
|
||||
}
|
||||
}
|
||||
|
||||
private List<WellSectionType> sections = null;
|
||||
private List<WellSectionType> sections = null!;
|
||||
public List<WellSectionType> 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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user