все короче, исправила, теперь весь функционал вроде работает...

письма отправляются
This commit is contained in:
Елена Бакальская 2024-05-30 01:42:23 +04:00
parent 0c65969374
commit 9e1cdab044
6 changed files with 114 additions and 38 deletions

View File

@ -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);
}
}
}

View File

@ -35,8 +35,8 @@ namespace PolyclinicBusinessLogic.OfficePackage
Texts = new List<string> Texts = new List<string>
{ {
procedure.Id.ToString(), procedure.Id.ToString(),
procedure.DateStartProcedure.ToShortTimeString(), procedure.DateStartProcedure.ToShortDateString(),
procedure.DateStopProcedure?.ToShortTimeString() ?? "нет даты окончания процедуры", procedure.DateStopProcedure?.ToShortDateString() ?? "нет даты окончания процедуры",
procedure.ProcedureName, procedure.ProcedureName,
"", "",
"" ""

View File

@ -23,6 +23,7 @@ namespace PolyclinicWebAppSuretor.Controllers
private readonly ICourseLogic _courseLogic; private readonly ICourseLogic _courseLogic;
private readonly ISuretorReportLogic _suretorReportLogic; private readonly ISuretorReportLogic _suretorReportLogic;
private readonly IUserLogic _userLogic; private readonly IUserLogic _userLogic;
private readonly SendMailLogic _sendMailLogic;
public HomeController(ILogger<HomeController> logger, public HomeController(ILogger<HomeController> logger,
IProcedureLogic procedureLogic, IProcedureLogic procedureLogic,
@ -31,7 +32,8 @@ namespace PolyclinicWebAppSuretor.Controllers
ISymptomLogic symptomLogic, ISymptomLogic symptomLogic,
ICourseLogic courseLogic, ICourseLogic courseLogic,
ISuretorReportLogic suretorReportLogic, ISuretorReportLogic suretorReportLogic,
IUserLogic userLogic) IUserLogic userLogic,
SendMailLogic sendMailLogic)
{ {
_logger = logger; _logger = logger;
_procedureLogic = procedureLogic; _procedureLogic = procedureLogic;
@ -41,6 +43,7 @@ namespace PolyclinicWebAppSuretor.Controllers
_courseLogic = courseLogic; _courseLogic = courseLogic;
_suretorReportLogic = suretorReportLogic; _suretorReportLogic = suretorReportLogic;
_userLogic = userLogic; _userLogic = userLogic;
_sendMailLogic = sendMailLogic;
} }
public IActionResult Index() public IActionResult Index()
@ -197,7 +200,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
List<RecipeViewModel> recipes = _recipeLogic.ReadList(null); List<RecipeViewModel> recipes = _recipeLogic.ReadList(null);
if (recipes == null) if (recipes == null)
@ -214,7 +217,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
ViewBag.Courses = _courseLogic.ReadList(); ViewBag.Courses = _courseLogic.ReadList();
if (HttpContext.Request.Method == "GET") if (HttpContext.Request.Method == "GET")
@ -253,7 +256,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
ViewBag.Courses = _courseLogic.ReadList(); ViewBag.Courses = _courseLogic.ReadList();
@ -294,7 +297,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
var obj = _recipeLogic.ReadElement(new RecipeSearchModel { Id = id }); var obj = _recipeLogic.ReadElement(new RecipeSearchModel { Id = id });
if (obj != null) if (obj != null)
@ -314,7 +317,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
List<MedicamentViewModel> medicaments = _medicamentLogic.ReadList(null); List<MedicamentViewModel> medicaments = _medicamentLogic.ReadList(null);
if (medicaments == null) if (medicaments == null)
@ -331,7 +334,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
ViewBag.Procedures = _procedureLogic.ReadList(null); ViewBag.Procedures = _procedureLogic.ReadList(null);
ViewBag.Symptomes = _symptomLogic.ReadList(null); ViewBag.Symptomes = _symptomLogic.ReadList(null);
@ -393,7 +396,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
var obj = _medicamentLogic.ReadElement(new MedicamentSearchModel { Id = id }); var obj = _medicamentLogic.ReadElement(new MedicamentSearchModel { Id = id });
if (obj != null) if (obj != null)
@ -414,9 +417,9 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
List<ProcedureViewModel> procedure = _procedureLogic.ReadList(null); List<ProcedureViewModel> procedure = _procedureLogic.ReadList(new ProcedureSearchModel { UserId = currentUser.Id});
if (procedure == null) if (procedure == null)
{ {
procedure = new(); procedure = new();
@ -444,7 +447,7 @@ namespace PolyclinicWebAppSuretor.Controllers
ProcedureBindingModel procedure = new ProcedureBindingModel ProcedureBindingModel procedure = new ProcedureBindingModel
{ {
Name = model.Name, Name = model.Name,
UserId = model.UserId, UserId = currentUser.Id,
Comment = model.Comment ?? string.Empty, Comment = model.Comment ?? string.Empty,
DateStartProcedure = model.DateStartProcedure, DateStartProcedure = model.DateStartProcedure,
DateStopProcedure = model.DateStopProcedure DateStopProcedure = model.DateStopProcedure
@ -461,7 +464,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
if (HttpContext.Request.Method == "GET") if (HttpContext.Request.Method == "GET")
{ {
@ -476,7 +479,7 @@ namespace PolyclinicWebAppSuretor.Controllers
Id = model.Id, Id = model.Id,
Name = model.Name, Name = model.Name,
Comment = model.Comment, Comment = model.Comment,
UserId = model.UserId, UserId = currentUser.Id,
DateStartProcedure = model.DateStartProcedure, DateStartProcedure = model.DateStartProcedure,
DateStopProcedure = model.DateStopProcedure DateStopProcedure = model.DateStopProcedure
}; };
@ -491,7 +494,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
var obj = _procedureLogic.ReadElement(new ProcedureSearchModel { Id = id }); var obj = _procedureLogic.ReadElement(new ProcedureSearchModel { Id = id });
if (obj != null) if (obj != null)
@ -506,7 +509,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
model = new() model = new()
{ {
@ -521,7 +524,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
ViewBag.Procedures = _procedureLogic.ReadList(null); ViewBag.Procedures = _procedureLogic.ReadList(null);
return View(); return View();
@ -533,7 +536,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
var procedure = _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId }); var procedure = _procedureLogic.ReadElement(new ProcedureSearchModel { Id = procedureId });
ViewBag.Procedures = procedure; ViewBag.Procedures = procedure;
@ -580,7 +583,7 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) if (currentUser == null)
{ {
return RedirectToAction("Login", "User"); return RedirectToAction("Login", "Home");
} }
return View(new List<ReportProceduresViewModel>()); return View(new List<ReportProceduresViewModel>());
} }
@ -591,22 +594,23 @@ namespace PolyclinicWebAppSuretor.Controllers
var currentUser = LoginManager.LogginedUser; var currentUser = LoginManager.LogginedUser;
if (currentUser == null) 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") 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); return View(proceduresReport);
} }
else if (reportType == "email") else if (reportType == "email")
@ -620,12 +624,32 @@ namespace PolyclinicWebAppSuretor.Controllers
_suretorReportLogic.SaveProceduresToPdfFile(reportBindingModel); _suretorReportLogic.SaveProceduresToPdfFile(reportBindingModel);
// ×òåíèå ñãåíåðèðîâàííîãî ôàéëà // òóò ÿ êîðî÷å çàäàþ èìÿ ôàéëó äåôîëòíîå è ñîõðàíÿþ åãî â òåêóùóþ äèðåêòîðèþ (íó è â çàãðóçêè åñòåññíà)
var fileName = "ProceduresReport.pdf"; var fileName = "ProceduresReport.pdf";
var filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName); var filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); 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<ReportProceduresViewModel>());
} }
else else
{ {

View File

@ -25,6 +25,7 @@ builder.Services.AddTransient<AbstractSaveToWordCoursesByProcedures, SaveToWordC
builder.Services.AddTransient<AbstractSaveToExcelCoursesByProcedure, SaveToExcelCoursesByProcedure>(); builder.Services.AddTransient<AbstractSaveToExcelCoursesByProcedure, SaveToExcelCoursesByProcedure>();
builder.Services.AddTransient<AbstractSaveToPdfProcedures, SaveToPdfProcedures>(); builder.Services.AddTransient<AbstractSaveToPdfProcedures, SaveToPdfProcedures>();
builder.Services.AddTransient<ISuretorReportLogic, SuretorReportLogic>(); builder.Services.AddTransient<ISuretorReportLogic, SuretorReportLogic>();
builder.Services.AddTransient<SendMailLogic>();
builder.Services.AddTransient<IDiagnoseLogic, DiagnoseLogic>(); builder.Services.AddTransient<IDiagnoseLogic, DiagnoseLogic>();
builder.Services.AddTransient<ICourseLogic, CourseLogic>(); builder.Services.AddTransient<ICourseLogic, CourseLogic>();

View File

@ -54,7 +54,7 @@
@if (LoginManager.LogginedUser != null) @if (LoginManager.LogginedUser != null)
{ {
<div class="d-flex align-items-center"> <div class="d-flex align-items-center">
@Html.RouteLink(LoginManager.LogginedUser.FIO, new { controller = "User", action = "Privacy" }, new { @title = "Личный кабинет" }) @Html.RouteLink(LoginManager.LogginedUser.FIO, new { controller = "Home", action = "Privacy" }, new { @title = "Личный кабинет" })
<form class="ms-3" method="post" asp-action="Logout" asp-controller="User"> <form class="ms-3" method="post" asp-action="Logout" asp-controller="User">
<button class="btn btn-secondary" type="submit">Выйти</button> <button class="btn btn-secondary" type="submit">Выйти</button>
</form> </form>

View File

@ -5,5 +5,12 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"Smtp": {
"Host": "smtp.beget.com",
"Port": "2525",
"Username": "sushishop@ekallin.ru",
"Password": "8j#HWiCBiI*I",
"EnableSsl": "false"
}
} }