From 9e1cdab044d7757e6140e1afe78c66ddeffa4fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=91=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D1=81=D0=BA=D0=B0=D1=8F?= Date: Thu, 30 May 2024 01:42:23 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D0=BA=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D1=87=D0=B5,=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0,=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=81=D1=8C=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=B0=D0=BB=20=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82...=20=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D1=8C=D0=BC=D0=B0=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D1=8F=D1=8E=D1=82=D1=81=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/SendMailLogic.cs | 44 +++++++++ .../AbstractSaveToPdfProcedures.cs | 4 +- .../Controllers/HomeController.cs | 92 ++++++++++++------- Polyclinic/PolyclinicWebAppSuretor/Program.cs | 1 + .../Views/Shared/_Layout.cshtml | 2 +- .../PolyclinicWebAppSuretor/appsettings.json | 9 +- 6 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SendMailLogic.cs diff --git a/Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SendMailLogic.cs b/Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SendMailLogic.cs new file mode 100644 index 0000000..cc8e021 --- /dev/null +++ b/Polyclinic/PolyclinicBusinessLogic/BusinessLogics/SendMailLogic.cs @@ -0,0 +1,44 @@ +using Microsoft.Extensions.Configuration; +using System.Net.Mail; +using System.Net; + +namespace PolyclinicBusinessLogic.BusinessLogics +{ + public class SendMailLogic + { + private readonly IConfiguration _configuration; + + public SendMailLogic(IConfiguration configuration) + { + _configuration = configuration; + } + + public void SendEmail(string toEmail, string subject, string attachmentPath) + { + var smtpSection = _configuration.GetSection("Smtp"); + + var client = new SmtpClient(smtpSection["Host"]) + { + Port = int.Parse(smtpSection["Port"]), + Credentials = new NetworkCredential(smtpSection["Username"], smtpSection["Password"]), + EnableSsl = bool.Parse(smtpSection["EnableSsl"]), + }; + + var mailMessage = new MailMessage + { + From = new MailAddress(smtpSection["Username"]), + Subject = subject, + }; + + mailMessage.To.Add(toEmail); + + if (!string.IsNullOrEmpty(attachmentPath)) + { + Attachment attachment = new Attachment(attachmentPath); + mailMessage.Attachments.Add(attachment); + } + + client.Send(mailMessage); + } + } +} diff --git a/Polyclinic/PolyclinicBusinessLogic/OfficePackage/AbstractSaveToPdfProcedures.cs b/Polyclinic/PolyclinicBusinessLogic/OfficePackage/AbstractSaveToPdfProcedures.cs index d4b3873..dd731c6 100644 --- a/Polyclinic/PolyclinicBusinessLogic/OfficePackage/AbstractSaveToPdfProcedures.cs +++ b/Polyclinic/PolyclinicBusinessLogic/OfficePackage/AbstractSaveToPdfProcedures.cs @@ -35,8 +35,8 @@ namespace PolyclinicBusinessLogic.OfficePackage Texts = new List { procedure.Id.ToString(), - procedure.DateStartProcedure.ToShortTimeString(), - procedure.DateStopProcedure?.ToShortTimeString() ?? "нет даты окончания процедуры", + procedure.DateStartProcedure.ToShortDateString(), + procedure.DateStopProcedure?.ToShortDateString() ?? "нет даты окончания процедуры", procedure.ProcedureName, "", "" diff --git a/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs b/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs index ea8300d..da7d6b2 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs +++ b/Polyclinic/PolyclinicWebAppSuretor/Controllers/HomeController.cs @@ -23,6 +23,7 @@ namespace PolyclinicWebAppSuretor.Controllers private readonly ICourseLogic _courseLogic; private readonly ISuretorReportLogic _suretorReportLogic; private readonly IUserLogic _userLogic; + private readonly SendMailLogic _sendMailLogic; public HomeController(ILogger logger, IProcedureLogic procedureLogic, @@ -31,7 +32,8 @@ namespace PolyclinicWebAppSuretor.Controllers ISymptomLogic symptomLogic, ICourseLogic courseLogic, ISuretorReportLogic suretorReportLogic, - IUserLogic userLogic) + IUserLogic userLogic, + SendMailLogic sendMailLogic) { _logger = logger; _procedureLogic = procedureLogic; @@ -41,6 +43,7 @@ namespace PolyclinicWebAppSuretor.Controllers _courseLogic = courseLogic; _suretorReportLogic = suretorReportLogic; _userLogic = userLogic; + _sendMailLogic = sendMailLogic; } public IActionResult Index() @@ -197,7 +200,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } List recipes = _recipeLogic.ReadList(null); if (recipes == null) @@ -214,7 +217,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } ViewBag.Courses = _courseLogic.ReadList(); if (HttpContext.Request.Method == "GET") @@ -253,7 +256,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } ViewBag.Courses = _courseLogic.ReadList(); @@ -294,7 +297,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } var obj = _recipeLogic.ReadElement(new RecipeSearchModel { Id = id }); if (obj != null) @@ -314,7 +317,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } List medicaments = _medicamentLogic.ReadList(null); if (medicaments == null) @@ -331,7 +334,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } ViewBag.Procedures = _procedureLogic.ReadList(null); ViewBag.Symptomes = _symptomLogic.ReadList(null); @@ -393,7 +396,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } var obj = _medicamentLogic.ReadElement(new MedicamentSearchModel { Id = id }); if (obj != null) @@ -414,9 +417,9 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } - List procedure = _procedureLogic.ReadList(null); + List procedure = _procedureLogic.ReadList(new ProcedureSearchModel { UserId = currentUser.Id}); if (procedure == null) { procedure = new(); @@ -444,7 +447,7 @@ namespace PolyclinicWebAppSuretor.Controllers ProcedureBindingModel procedure = new ProcedureBindingModel { Name = model.Name, - UserId = model.UserId, + UserId = currentUser.Id, Comment = model.Comment ?? string.Empty, DateStartProcedure = model.DateStartProcedure, DateStopProcedure = model.DateStopProcedure @@ -461,7 +464,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } if (HttpContext.Request.Method == "GET") { @@ -476,7 +479,7 @@ namespace PolyclinicWebAppSuretor.Controllers Id = model.Id, Name = model.Name, Comment = model.Comment, - UserId = model.UserId, + UserId = currentUser.Id, DateStartProcedure = model.DateStartProcedure, DateStopProcedure = model.DateStopProcedure }; @@ -491,7 +494,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } var obj = _procedureLogic.ReadElement(new ProcedureSearchModel { Id = id }); if (obj != null) @@ -506,7 +509,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } model = new() { @@ -521,7 +524,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } ViewBag.Procedures = _procedureLogic.ReadList(null); return View(); @@ -533,7 +536,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } var procedure = _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId }); ViewBag.Procedures = procedure; @@ -580,7 +583,7 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } return View(new List()); } @@ -591,22 +594,23 @@ namespace PolyclinicWebAppSuretor.Controllers var currentUser = LoginManager.LogginedUser; if (currentUser == null) { - return RedirectToAction("Login", "User"); + return RedirectToAction("Login", "Home"); } + + var proceduresReport = _suretorReportLogic.GetProceduresByMedicametsAndSymptoms(); + + // ( ) + if (dateFrom.HasValue) + { + proceduresReport = proceduresReport.Where(p => p.DateStartProcedure >= dateFrom.Value).ToList(); + } + if (dateTo.HasValue) + { + proceduresReport = proceduresReport.Where(p => p.DateStopProcedure <= dateTo.Value).ToList(); + } + if (reportType == "form") { - var proceduresReport = _suretorReportLogic.GetProceduresByMedicametsAndSymptoms(); - - // ( ) - if (dateFrom.HasValue) - { - proceduresReport = proceduresReport.Where(p => p.DateStartProcedure >= dateFrom.Value).ToList(); - } - if (dateTo.HasValue) - { - proceduresReport = proceduresReport.Where(p => p.DateStopProcedure <= dateTo.Value).ToList(); - } - return View(proceduresReport); } else if (reportType == "email") @@ -620,12 +624,32 @@ namespace PolyclinicWebAppSuretor.Controllers _suretorReportLogic.SaveProceduresToPdfFile(reportBindingModel); - // + // ( ) var fileName = "ProceduresReport.pdf"; var filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName); byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); - // - return File(fileBytes, "application/pdf", fileName); + + if (System.IO.File.Exists(filePath)) + { + try + { + var userEmail = currentUser.Email; + _sendMailLogic.SendEmail(userEmail, $", {currentUser.FIO}! !", filePath); + + TempData["Message"] = $" .pdf '{currentUser.Email}'"; + return File(fileBytes, "application/pdf", fileName); + } + catch (Exception ex) + { + TempData["Error"] = $"Error while sending email: {ex.Message}"; + } + } + else + { + TempData["Error"] = "Failed to generate the report."; + } + + return View(new List()); } else { diff --git a/Polyclinic/PolyclinicWebAppSuretor/Program.cs b/Polyclinic/PolyclinicWebAppSuretor/Program.cs index bedfa0a..b01cccb 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Program.cs +++ b/Polyclinic/PolyclinicWebAppSuretor/Program.cs @@ -25,6 +25,7 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/Polyclinic/PolyclinicWebAppSuretor/Views/Shared/_Layout.cshtml b/Polyclinic/PolyclinicWebAppSuretor/Views/Shared/_Layout.cshtml index a288b32..c142834 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/Views/Shared/_Layout.cshtml +++ b/Polyclinic/PolyclinicWebAppSuretor/Views/Shared/_Layout.cshtml @@ -54,7 +54,7 @@ @if (LoginManager.LogginedUser != null) {
- @Html.RouteLink(LoginManager.LogginedUser.FIO, new { controller = "User", action = "Privacy" }, new { @title = "Личный кабинет" }) + @Html.RouteLink(LoginManager.LogginedUser.FIO, new { controller = "Home", action = "Privacy" }, new { @title = "Личный кабинет" })
diff --git a/Polyclinic/PolyclinicWebAppSuretor/appsettings.json b/Polyclinic/PolyclinicWebAppSuretor/appsettings.json index 10f68b8..9a3f365 100644 --- a/Polyclinic/PolyclinicWebAppSuretor/appsettings.json +++ b/Polyclinic/PolyclinicWebAppSuretor/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Smtp": { + "Host": "smtp.beget.com", + "Port": "2525", + "Username": "sushishop@ekallin.ru", + "Password": "8j#HWiCBiI*I", + "EnableSsl": "false" + } }