diff --git a/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj b/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj index 6bdaf411..873bdfb4 100644 --- a/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj +++ b/AsbCloudInfrastructure/AsbCloudInfrastructure.csproj @@ -62,6 +62,7 @@ + diff --git a/AsbCloudInfrastructure/MemoryCacheExtentions.cs b/AsbCloudInfrastructure/MemoryCacheExtentions.cs index eb57af3f..85956230 100644 --- a/AsbCloudInfrastructure/MemoryCacheExtentions.cs +++ b/AsbCloudInfrastructure/MemoryCacheExtentions.cs @@ -46,10 +46,7 @@ namespace AsbCloudInfrastructure /// public static Task> GetOrCreateBasicAsync(this IMemoryCache memoryCache, Func>> getterAsync, CancellationToken token) { - var key = typeof(T).FullName; - if (key == null) - return Task.FromResult(Enumerable.Empty()); - + var key = typeof(T).FullName!; var cache = memoryCache.GetOrCreateAsync(key, async (cacheEntry) => { cacheEntry.AbsoluteExpirationRelativeToNow = CacheOlescence; @@ -87,7 +84,7 @@ namespace AsbCloudInfrastructure /// public static IEnumerable GetOrCreateBasic(this IMemoryCache memoryCache, Func> getter) { - var key = typeof(T).FullName; + var key = typeof(T).FullName!; var cache = memoryCache.GetOrCreate(key, cacheEntry => { cacheEntry.AbsoluteExpirationRelativeToNow = CacheOlescence; @@ -106,10 +103,7 @@ namespace AsbCloudInfrastructure public static void DropBasic(this IMemoryCache memoryCache) where T : class { - var key = typeof(T).FullName; - - if (key == null) - return; + var key = typeof(T).FullName!; memoryCache.Remove(key); } diff --git a/AsbCloudInfrastructure/Services/Email/BaseFactory.cs b/AsbCloudInfrastructure/Services/Email/BaseFactory.cs index c0431016..b48d77ed 100644 --- a/AsbCloudInfrastructure/Services/Email/BaseFactory.cs +++ b/AsbCloudInfrastructure/Services/Email/BaseFactory.cs @@ -2,6 +2,7 @@ using AsbCloudApp.Exceptions; using Microsoft.Extensions.Configuration; using System; +using System.Configuration; using System.IO; namespace AsbCloudInfrastructure.Services.Email @@ -9,17 +10,24 @@ namespace AsbCloudInfrastructure.Services.Email public class BaseFactory { - private readonly string platformName; - private readonly string platformUrl; - private readonly string companyName; - private readonly string supportMail; + protected readonly string platformName; + protected readonly string platformUrl; + protected readonly string companyName; + protected readonly string supportMail; public BaseFactory(IConfiguration configuration) { - platformName = configuration.GetValue("email:platformName", "Цифровое бурение") ?? string.Empty; - platformUrl = configuration.GetValue("email:platformUrl", "https://cloud.digitaldrilling.ru/") ?? string.Empty; - companyName = configuration.GetValue("email:companyName", "ООО \"Цифровое бурение\"") ?? string.Empty; - supportMail = configuration.GetValue("email:supportMail", "support@digitaldrilling.ru") ?? string.Empty; + platformName = configuration.GetValue("email:platformName") + ?? throw new ConfigurationErrorsException("email:platformName не определен"); + + platformUrl = configuration.GetValue("email:platformUrl") + ?? throw new ConfigurationErrorsException("email:platformUrl не определен"); + + companyName = configuration.GetValue("email:companyName") + ?? throw new ConfigurationErrorsException("email:companyName не определен"); + + supportMail = configuration.GetValue("email:supportMail") + ?? throw new ConfigurationErrorsException("email:supportMail не определен"); } public static string GetOrEmptyImageBase64(string resourceFileName) @@ -37,7 +45,7 @@ namespace AsbCloudInfrastructure.Services.Email { System.Diagnostics.Trace.TraceWarning($"GetOrEmptyImageBase64(). File {logoFilePath} not found."); return string.Empty; - } + } var imageBytes = File.ReadAllBytes(logoFilePath); var format = Path.GetExtension(resourceFileName).Trim('.'); diff --git a/AsbCloudInfrastructure/Services/Email/DrillingMailBodyFactory.cs b/AsbCloudInfrastructure/Services/Email/DrillingMailBodyFactory.cs index d1371110..ac1d4771 100644 --- a/AsbCloudInfrastructure/Services/Email/DrillingMailBodyFactory.cs +++ b/AsbCloudInfrastructure/Services/Email/DrillingMailBodyFactory.cs @@ -4,83 +4,79 @@ using Microsoft.Extensions.Configuration; namespace AsbCloudInfrastructure.Services.Email { - class DrillingMailBodyFactory : BaseFactory + class DrillingMailBodyFactory : BaseFactory { - private readonly string platformName; - private readonly string platformUrl; public DrillingMailBodyFactory(IConfiguration configuration) - : base(configuration) + : base(configuration) { - platformName = configuration.GetValue("email:platformName", "Цифровое бурение") ?? string.Empty; - platformUrl = configuration.GetValue("email:platformUrl", "https://cloud.digitaldrilling.ru/") ?? string.Empty; - } + } - public override string MakeSubject(WellDto well, string action) + public override string MakeSubject(WellDto well, string action) { - var subj = $"{well.Deposit}, {well.Cluster}, {well.Caption}. Программа бурения. {action}"; - return subj; - } + var subj = $"{well.Deposit}, {well.Cluster}, {well.Caption}. Программа бурения. {action}"; + return subj; + } - public string MakeMailBodyForNewPublisher(WellDto well, string publisherName, string documentCategory) - { - var drillingProgramHref = MakeDrillingProgramHref(well); - - var body = $"

Здравствуйте, {publisherName}.

" + - $"На портале {platformName} началось создание программы бурения скважины {drillingProgramHref}," + - $" куст {well.Cluster}, месторождение {well.Deposit}." + - $"

От вас ожидается загрузка на портал документа «{documentCategory}» в формате excel (*.xlsx)." + - MakeSignatue() + - $""; - return body; - } - - public string MakeMailBodyForApproverNewFile(WellDto well, string approverName, int idFile, string fileName) + public string MakeMailBodyForNewPublisher(WellDto well, string publisherName, string documentCategory) { - var drillingProgramHref = MakeDrillingProgramHref(well); + var drillingProgramHref = MakeDrillingProgramHref(well); - var body = $"

Здравствуйте, {approverName}.

" + - $"На портал {platformName} загружен документ {fileName}" + - $" для согласования при создании программы бурения скважины {drillingProgramHref}, куст ({well.Cluster})" + - $", месторождение ({well.Deposit}).
" + + var body = $"

Здравствуйте, {publisherName}.

" + + $"На портале {platformName} началось создание программы бурения скважины {drillingProgramHref}," + + $" куст {well.Cluster}, месторождение {well.Deposit}." + + $"

От вас ожидается загрузка на портал документа «{documentCategory}» в формате excel (*.xlsx)." + MakeSignatue() + $""; - return body; - } + return body; + } - public string MakeMailBodyForPublisherOnReject(WellDto well, string publisherName, int idFile, string fileName, FileMarkDto fileMark) - { - var drillingProgramHref = MakeDrillingProgramHref(well); - - var body = $"

Здравствуйте, {publisherName}.

" + - $"На портале {platformName} отклонен загруженный вами документ {fileName} " + - $" по программе бурения скважины {drillingProgramHref}," + - $" куст {well.Cluster}, месторождение {well.Deposit}." + - $" Комментарий согласующего ({fileMark.User?.Name} {fileMark.User?.Surname}):
{fileMark.Comment}" + - MakeSignatue() + - $""; - return body; - } - - public string MakeMailBodyForPublisherOnFullAccept(WellDto well, string publisherName, int idFile, string fileName) - { - var drillingProgramHref = MakeDrillingProgramHref(well); - - var body = $"

Здравствуйте, {publisherName}.

" + - $"На портале {platformName} полностью согласован документ {fileName} " + - $" по программе бурения скважины {drillingProgramHref}," + - $" куст {well.Cluster}, месторождение {well.Deposit}." + - MakeSignatue() + - $""; - return body; - } - - private string MakeDrillingProgramHref(WellDto well) + public string MakeMailBodyForApproverNewFile(WellDto well, string approverName, int idFile, string fileName) { - var drillingProgramUrl = $"{platformUrl}/well/{well.Id}/drillingProgram"; - var drillingProgramHref = MakeHref(drillingProgramUrl, well.Caption); - return drillingProgramHref; - } - } + var drillingProgramHref = MakeDrillingProgramHref(well); + + var body = $"

Здравствуйте, {approverName}.

" + + $"На портал {platformName} загружен документ {fileName}" + + $" для согласования при создании программы бурения скважины {drillingProgramHref}, куст ({well.Cluster})" + + $", месторождение ({well.Deposit}).
" + + MakeSignatue() + + $""; + return body; + } + + public string MakeMailBodyForPublisherOnReject(WellDto well, string publisherName, int idFile, string fileName, FileMarkDto fileMark) + { + var drillingProgramHref = MakeDrillingProgramHref(well); + + var body = $"

Здравствуйте, {publisherName}.

" + + $"На портале {platformName} отклонен загруженный вами документ {fileName} " + + $" по программе бурения скважины {drillingProgramHref}," + + $" куст {well.Cluster}, месторождение {well.Deposit}." + + $" Комментарий согласующего ({fileMark.User?.Name} {fileMark.User?.Surname}):
{fileMark.Comment}" + + MakeSignatue() + + $""; + return body; + } + + public string MakeMailBodyForPublisherOnFullAccept(WellDto well, string publisherName, int idFile, string fileName) + { + var drillingProgramHref = MakeDrillingProgramHref(well); + + var body = $"

Здравствуйте, {publisherName}.

" + + $"На портале {platformName} полностью согласован документ {fileName} " + + $" по программе бурения скважины {drillingProgramHref}," + + $" куст {well.Cluster}, месторождение {well.Deposit}." + + MakeSignatue() + + $""; + return body; + } + + private string MakeDrillingProgramHref(WellDto well) + { + var drillingProgramUrl = $"{platformUrl}/well/{well.Id}/drillingProgram"; + var drillingProgramHref = MakeHref(drillingProgramUrl, well.Caption); + return drillingProgramHref; + } + } } diff --git a/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs b/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs index 88fbe691..6812e068 100644 --- a/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs +++ b/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs @@ -4,6 +4,7 @@ using AsbCloudApp.Repositories; using AsbCloudApp.Services.Notifications; using Microsoft.Extensions.Configuration; using System.Collections.Generic; +using System.Configuration; using System.Diagnostics; using System.Linq; using System.Net.Mail; @@ -29,9 +30,14 @@ namespace AsbCloudInfrastructure.Services.Email { this.userRepository = userRepository; - this.sender = configuration.GetValue("email:sender", string.Empty) ?? string.Empty; - this.smtpPassword = configuration.GetValue("email:password", string.Empty) ?? string.Empty; - this.smtpServer = configuration.GetValue("email:smtpServer", string.Empty) ?? string.Empty; + this.sender = configuration.GetValue("email:sender") + ?? throw new ConfigurationErrorsException("email:sender не определен"); + + this.smtpPassword = configuration.GetValue("email:password") + ?? throw new ConfigurationErrorsException("email:password не определен"); + + this.smtpServer = configuration.GetValue("email:smtpServer") + ?? throw new ConfigurationErrorsException("email:smtpServer не определен"); var configError = string.IsNullOrEmpty(this.sender) || string.IsNullOrEmpty(this.smtpPassword) || diff --git a/AsbCloudInfrastructure/Services/HelpPageService.cs b/AsbCloudInfrastructure/Services/HelpPageService.cs index 45d4d18d..012ce5bc 100644 --- a/AsbCloudInfrastructure/Services/HelpPageService.cs +++ b/AsbCloudInfrastructure/Services/HelpPageService.cs @@ -1,11 +1,11 @@ using AsbCloudApp.Data; using AsbCloudApp.Repositories; using AsbCloudApp.Services; +using Microsoft.Extensions.Configuration; using System.IO; using System.Net; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; namespace AsbCloudInfrastructure.Services; @@ -14,7 +14,7 @@ namespace AsbCloudInfrastructure.Services; /// public class HelpPageService : IHelpPageService { - private readonly string directoryNameHelpPageFiles; + private readonly string directoryNameHelpPageFiles; private readonly IHelpPageRepository helpPageRepository; private readonly IFileStorageRepository fileStorageRepository; @@ -27,13 +27,10 @@ public class HelpPageService : IHelpPageService public HelpPageService(IHelpPageRepository helpPageRepository, IFileStorageRepository fileStorageRepository, IConfiguration configuration) - { + { this.helpPageRepository = helpPageRepository; this.fileStorageRepository = fileStorageRepository; - directoryNameHelpPageFiles = configuration.GetValue("DirectoryNameHelpPageFiles") ?? string.Empty; - - if (string.IsNullOrWhiteSpace(directoryNameHelpPageFiles)) - directoryNameHelpPageFiles = "helpPages"; + directoryNameHelpPageFiles = configuration.GetValue("DirectoryNameHelpPageFiles", "helpPages")!; } /// @@ -45,17 +42,17 @@ public class HelpPageService : IHelpPageService /// /// /// - public async Task AddOrUpdateAsync(string urlPage, - int idCategory, - string fileName, - Stream fileStream, + public async Task AddOrUpdateAsync(string urlPage, + int idCategory, + string fileName, + Stream fileStream, CancellationToken cancellationToken) { var helpPage = await helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage, idCategory, cancellationToken); - if(helpPage is not null) + if (helpPage is not null) { await UpdateFileAsync(helpPage, idCategory, @@ -86,12 +83,12 @@ public class HelpPageService : IHelpPageService CancellationToken cancellationToken) { pageKey = WebUtility.UrlDecode(pageKey); - + var helpPage = await helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(pageKey, idCategory, cancellationToken); - - if(helpPage is null) + + if (helpPage is null) return null; string filePath = fileStorageRepository.GetFilePath(directoryNameHelpPageFiles, diff --git a/AsbCloudInfrastructure/Services/ManualCatalogService.cs b/AsbCloudInfrastructure/Services/ManualCatalogService.cs index 7776a4ef..83d24f4b 100644 --- a/AsbCloudInfrastructure/Services/ManualCatalogService.cs +++ b/AsbCloudInfrastructure/Services/ManualCatalogService.cs @@ -35,10 +35,7 @@ public class ManualCatalogService : IManualCatalogService this.fileStorageRepository = fileStorageRepository; this.manualDirectoryRepository = manualDirectoryRepository; this.manualRepository = manualRepository; - directoryFiles = configuration.GetValue("DirectoryManualFiles") ?? string.Empty; - - if (string.IsNullOrWhiteSpace(directoryFiles)) - directoryFiles = "manuals"; + directoryFiles = configuration.GetValue("DirectoryManualFiles", "manuals")!; } public async Task SaveFileAsync(int idDirectory, int idAuthor, string name, Stream stream, CancellationToken cancellationToken) diff --git a/AsbCloudInfrastructure/Services/ReduceSamplingService.cs b/AsbCloudInfrastructure/Services/ReduceSamplingService.cs index 7fc7e19c..38d6a03e 100644 --- a/AsbCloudInfrastructure/Services/ReduceSamplingService.cs +++ b/AsbCloudInfrastructure/Services/ReduceSamplingService.cs @@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; +using System.Configuration; using System.Diagnostics; using System.Linq; using System.Threading; @@ -36,7 +37,8 @@ namespace AsbCloudInfrastructure.Services private ReduceSamplingService(IConfiguration configuration) { - connectionString = configuration.GetConnectionString("DefaultConnection") ?? string.Empty; + connectionString = configuration.GetConnectionString("DefaultConnection") + ?? throw new ConfigurationErrorsException("DefaultConnection не определен"); } ~ReduceSamplingService()