diff --git a/Bank/BankBusinessLogic/MailWorker/AbstractMailWorker.cs b/Bank/BankBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..b8c61bf --- /dev/null +++ b/Bank/BankBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,51 @@ +using BankContracts.BindingModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankBusinessLogic.MailWorker +{ + public abstract class AbstractMailWorker + { + protected string _mailLogin = string.Empty; + protected string _mailPassword = string.Empty; + protected string _smtpClientHost = string.Empty; + protected int _smtpClientPort; + protected string _popHost = string.Empty; + protected int _popPort; + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger) + { + _logger = logger; + } + + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.MailLogin; + _mailPassword = config.MailPassword; + _smtpClientHost = config.SmtpClientHost; + _smtpClientPort = config.SmtpClientPort; + _popHost = config.PopHost; + _popPort = config.PopPort; + _logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPOrt}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); + } + + public async void MailSendAsync(MailSendInfoBindingModel info) + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + return; + if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) + return; + if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + return; + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + await SendMailAsync(info); + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + } +} diff --git a/Bank/BankBusinessLogic/MailWorker/MailKitWorker.cs b/Bank/BankBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..9d42b54 --- /dev/null +++ b/Bank/BankBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,45 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net.Mime; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using BankContracts.BindingModels; + +namespace BankBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger) + : base(logger) { } + + protected override async Task SendMailAsync(MailSendInfoBindingModel info) + { + using var objMailMessage = new MailMessage(); + using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort); + try + { + objMailMessage.From = new MailAddress(_mailLogin); + objMailMessage.To.Add(new MailAddress(info.MailAddress)); + objMailMessage.Subject = info.Subject; + objMailMessage.Body = info.Text; + objMailMessage.SubjectEncoding = Encoding.UTF8; + objMailMessage.BodyEncoding = Encoding.UTF8; + Attachment attachment = new Attachment("C:\\Users\\user\\Downloads\\TransfersWithdrawalsList.pdf", new ContentType(MediaTypeNames.Application.Pdf)); + objMailMessage.Attachments.Add(attachment); + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword); + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch + { + throw; + } + } + } +} diff --git a/Bank/BankContracts/BindingModels/MailConfigBindingModel.cs b/Bank/BankContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..55a5891 --- /dev/null +++ b/Bank/BankContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankContracts.BindingModels +{ + public class MailConfigBindingModel + { + public string MailLogin { get; set; } = string.Empty; + public string MailPassword { get; set; } = string.Empty; + public string SmtpClientHost { get; set; } = string.Empty; + public int SmtpClientPort { get; set; } + public string PopHost { get; set; } = string.Empty; + public int PopPort { get; set; } + } +} diff --git a/Bank/BankContracts/BindingModels/MailSendInfoBindingModel.cs b/Bank/BankContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..e708adc --- /dev/null +++ b/Bank/BankContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +} diff --git a/Bank/BankContracts/BindingModels/ReportBindingModel.cs b/Bank/BankContracts/BindingModels/ReportBindingModel.cs index a9fb8cd..c962348 100644 --- a/Bank/BankContracts/BindingModels/ReportBindingModel.cs +++ b/Bank/BankContracts/BindingModels/ReportBindingModel.cs @@ -13,6 +13,6 @@ namespace BankContracts.BindingModels public DateTime? DateTo { get; set; } public List? SelectedAccountIds { get; set; } public List? SelectedCardIds { get; set; } - public string Email { get; set; } = string.Empty; + public string? Email { get; set; } } } diff --git a/Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs b/Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs index 3f65fa6..d74a657 100644 --- a/Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs +++ b/Bank/BankContracts/BusinessLogicsContracts/IReportLogic.cs @@ -22,6 +22,7 @@ namespace BankContracts.BusinessLogicsContracts void SaveTransfersToExcelFile(ReportBindingModel model); void SaveOperationsRequestsToPdfFile(ReportBindingModel model); void SaveTransfersWithdrawalsToPdfFile(ReportBindingModel model); + void SendTransfersWithdrawalsToEmail(ReportBindingModel model); void SandOperationsRequestsToEmail(ReportBindingModel model); } } diff --git a/Bank/BankContracts/BusinessLogicsContracts/IRequestLogic.cs b/Bank/BankContracts/BusinessLogicsContracts/IRequestLogic.cs index 9be7e47..7a9a5b3 100644 --- a/Bank/BankContracts/BusinessLogicsContracts/IRequestLogic.cs +++ b/Bank/BankContracts/BusinessLogicsContracts/IRequestLogic.cs @@ -16,8 +16,6 @@ namespace BankContracts.BusinessLogicsContracts bool Create(RequestBindingModel model); bool Update(RequestBindingModel model); bool Delete(RequestBindingModel model); - //todo методы смены статуса - //bool DeclineRequest(RequestBindingModel model); - //bool SatisfyRequest(RequestBindingModel model); + } } diff --git a/Bank/BankManagersClientApp/Controllers/HomeController.cs b/Bank/BankManagersClientApp/Controllers/HomeController.cs index b0fea54..1555cdf 100644 --- a/Bank/BankManagersClientApp/Controllers/HomeController.cs +++ b/Bank/BankManagersClientApp/Controllers/HomeController.cs @@ -512,10 +512,27 @@ namespace BankManagersClientApp.Controllers } return View(); } - #endregion - #region//Error - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + [HttpPost] + public void TransfersWithdrawalsListReport(DateTime dateFrom, DateTime dateTo) + { + if (APIClient.Client == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + APIClient.PostRequest("api/report/sendtransferswithdrawalstoemail", new ReportBindingModel + { + FileName = "C:\\Users\\user\\Downloads\\TransfersWithdrawalsList.pdf", + DateFrom = dateFrom, + DateTo = dateTo, + Email = APIClient.Client.Email, + }); + Response.Redirect("TransfersWithdrawalsListReport"); + } + #endregion + + #region//Error + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); diff --git a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml index 42df840..8422020 100644 --- a/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml +++ b/Bank/BankManagersClientApp/Views/Home/TransfersWithdrawalsListReport.cshtml @@ -2,22 +2,30 @@ ViewData["Title"] = "TransfersWithdrawalsListReport"; }
-

Список счетов с расшифровкой по переводам и выдачам

+

Список счетов с расшифровкой по переводам и выдачам по периоду

@{
Начальная дата:
- +
Конечная дата:
- +
+
+
+
+
+
+
+
+
@@ -30,13 +38,5 @@
-
-
-
-
-
-
-
-
}
\ No newline at end of file diff --git a/Bank/BankRestApi/Controllers/ReportController.cs b/Bank/BankRestApi/Controllers/ReportController.cs index e0f5077..4d1d2a9 100644 --- a/Bank/BankRestApi/Controllers/ReportController.cs +++ b/Bank/BankRestApi/Controllers/ReportController.cs @@ -70,6 +70,19 @@ namespace BankRestApi.Controllers throw; } } + [HttpPost] + public void SendTransfersWithdrawalsToEmail(ReportBindingModel model) + { + try + { + _logic.SendTransfersWithdrawalsToEmail(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения переводов в ворд"); + throw; + } + } [HttpPost] public void SendOperationsRequestsToEmail(ReportBindingModel model) diff --git a/Bank/BankRestApi/Program.cs b/Bank/BankRestApi/Program.cs index a550dd8..6785dd2 100644 --- a/Bank/BankRestApi/Program.cs +++ b/Bank/BankRestApi/Program.cs @@ -1,6 +1,8 @@ using BankBusinessLogic.BusinessLogic; +using BankBusinessLogic.MailWorker; using BankBusinessLogic.OfficePackage; using BankBusinessLogic.OfficePackage.Implements; +using BankContracts.BindingModels; using BankContracts.BusinessLogicsContracts; using BankContracts.StoragesContracts; using BankDatabaseImplement.Implements; @@ -43,6 +45,8 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddSingleton(); + builder.Services.AddLogging(option => { option.SetMinimumLevel(LogLevel.Information); @@ -63,6 +67,17 @@ builder.Services.AddSwaggerGen(c => }); var app = builder.Build(); +var mailSender = app.Services.GetService(); +mailSender?.MailConfig(new MailConfigBindingModel +{ + MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty, + MailPassword = builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? string.Empty, + SmtpClientHost = builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? string.Empty, + SmtpClientPort = Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()), + PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? string.Empty, + PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString()) +}); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/Bank/BankRestApi/appsettings.json b/Bank/BankRestApi/appsettings.json index 4183f61..805e230 100644 --- a/Bank/BankRestApi/appsettings.json +++ b/Bank/BankRestApi/appsettings.json @@ -10,7 +10,6 @@ "SmtpClientPort": "587", "PopHost": "pop.gmail.com", "PopPort": "995", - "MailLogin": "forlabspibd23@gmail.com", "MailPassword": "euby honk jvrl rrjj" }