From d43bff822ff3516ea0f00895eeca1e697f70939d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Thu, 18 May 2023 19:02:48 +0400 Subject: [PATCH] send message --- .../Worker/WorkerReportLogic.cs | 24 +++++++++---------- .../MailWorker/MailKitWorker.cs | 2 ++ .../BindingModels/MailSendInfoBindingModel.cs | 2 ++ .../BindingModels/ReportBindingModel.cs | 2 ++ HardwareShop/HardwareShopRestApi/Program.cs | 4 ++-- .../Views/Home/WorkerReport.cshtml | 4 ++-- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs index bc55a6a..1460932 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/WorkerReportLogic.cs @@ -18,19 +18,17 @@ namespace HardwareShopContracts.BusinessLogicsContracts private readonly IPurchaseStorage _purchaseStorage; - private readonly IBuildStorage _buildStorage; - private readonly AbstractSaveToExcel _saveToExcel; private readonly AbstractSaveToWord _saveToWord; private readonly AbstractSaveToPdf _saveToPdf; - private readonly MailKitWorker _mailKitWorker; - public WorkerReportLogic(IPurchaseStorage purchaseStorage, MailKitWorker mailKitWorker, IBuildStorage buildStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord) + private readonly AbstractMailWorker _mailKitWorker; + + public WorkerReportLogic(IPurchaseStorage purchaseStorage, AbstractMailWorker mailKitWorker, AbstractSaveToPdf saveToPdf, AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord) { _purchaseStorage = purchaseStorage; - _buildStorage = buildStorage; _saveToExcel = saveToExcel; _saveToWord = saveToWord; _saveToPdf = saveToPdf; @@ -162,14 +160,14 @@ namespace HardwareShopContracts.BusinessLogicsContracts DateTo = model.DateTo, ReportPurchases = GetPurchase(model) }); - // _mailKitWorker.SendMailAsync(new() - // { - // MailAddress = reportModel.UserEmail, - // Subject = "Отчет по покупкам", - // Text = $"За период с {reportModel.DateFrom.ToShortDateString()} " + - //$"по {reportModel.DateTo.ToShortDateString()}.", - // File = file - // }); + _mailKitWorker.MailSendAsync(new() + { + MailAddress = model.UserEmail, + Subject = "Отчет по покупкам", + Text = $"За период с {model.DateFrom.ToShortDateString()} " + + $"по {model.DateTo.ToShortDateString()}.", + File = file + }); } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopBusinessLogic/MailWorker/MailKitWorker.cs b/HardwareShop/HardwareShopBusinessLogic/MailWorker/MailKitWorker.cs index 96a2d20..1951330 100644 --- a/HardwareShop/HardwareShopBusinessLogic/MailWorker/MailKitWorker.cs +++ b/HardwareShop/HardwareShopBusinessLogic/MailWorker/MailKitWorker.cs @@ -25,6 +25,8 @@ namespace HardwareShopBusinessLogic.MailWorker objMailMessage.Body = info.Text; objMailMessage.SubjectEncoding = Encoding.UTF8; objMailMessage.BodyEncoding = Encoding.UTF8; + MemoryStream ms = new(info.File); + objMailMessage.Attachments.Add(new Attachment(ms, "report.pdf", "application/pdf")); objSmtpClient.UseDefaultCredentials = false; objSmtpClient.EnableSsl = true; diff --git a/HardwareShop/HardwareShopContracts/BindingModels/MailSendInfoBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/MailSendInfoBindingModel.cs index 11301ec..0054e7c 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/MailSendInfoBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -7,5 +7,7 @@ public string Subject { get; set; } = string.Empty; public string Text { get; set; } = string.Empty; + + public byte[] File { get; set; } = Array.Empty(); } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs index f501c97..ff2da2e 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/ReportBindingModel.cs @@ -2,6 +2,8 @@ { public class ReportBindingModel { + public string UserEmail { get; set; } = string.Empty; + public string FileName { get; set; } = string.Empty; public DateTime DateFrom { get; set; } diff --git a/HardwareShop/HardwareShopRestApi/Program.cs b/HardwareShop/HardwareShopRestApi/Program.cs index 181d0ca..479002d 100644 --- a/HardwareShop/HardwareShopRestApi/Program.cs +++ b/HardwareShop/HardwareShopRestApi/Program.cs @@ -37,8 +37,8 @@ builder.Services.AddTransient() builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); - -builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml index 1dbb85c..5c263c2 100644 --- a/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/WorkerReport.cshtml @@ -98,7 +98,7 @@ return; } $.ajax({ - url: "/home/sendbymailepurchasereport", + url: "/home/SendByMailPurchaseReport", type: "POST", contentType: "application/json", data: JSON.stringify(reportModel) @@ -129,7 +129,7 @@ let cellsData = ["", "", "", "", ""]; if (i === 0) { cellsData[0] = record.id; - cellsData[1] = record.purchaseDate; + cellsData[1] = getDate(record.purchaseDate); cellsData[2] = record.purchaseSum; createTableRow(cellsData); continue;