все короче, исправила, теперь весь функционал вроде работает...
письма отправляются
This commit is contained in:
parent
0c65969374
commit
9e1cdab044
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -35,8 +35,8 @@ namespace PolyclinicBusinessLogic.OfficePackage
|
||||
Texts = new List<string>
|
||||
{
|
||||
procedure.Id.ToString(),
|
||||
procedure.DateStartProcedure.ToShortTimeString(),
|
||||
procedure.DateStopProcedure?.ToShortTimeString() ?? "нет даты окончания процедуры",
|
||||
procedure.DateStartProcedure.ToShortDateString(),
|
||||
procedure.DateStopProcedure?.ToShortDateString() ?? "нет даты окончания процедуры",
|
||||
procedure.ProcedureName,
|
||||
"",
|
||||
""
|
||||
|
@ -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<HomeController> 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<RecipeViewModel> 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<MedicamentViewModel> 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<ProcedureViewModel> procedure = _procedureLogic.ReadList(null);
|
||||
List<ProcedureViewModel> 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<ReportProceduresViewModel>());
|
||||
}
|
||||
@ -591,10 +594,9 @@ namespace PolyclinicWebAppSuretor.Controllers
|
||||
var currentUser = LoginManager.LogginedUser;
|
||||
if (currentUser == null)
|
||||
{
|
||||
return RedirectToAction("Login", "User");
|
||||
return RedirectToAction("Login", "Home");
|
||||
}
|
||||
if (reportType == "form")
|
||||
{
|
||||
|
||||
var proceduresReport = _suretorReportLogic.GetProceduresByMedicametsAndSymptoms();
|
||||
|
||||
// Ôèëüòðàöèÿ äàííûõ ïî äàòå (åñëè íóæíî)
|
||||
@ -607,6 +609,8 @@ namespace PolyclinicWebAppSuretor.Controllers
|
||||
proceduresReport = proceduresReport.Where(p => p.DateStopProcedure <= dateTo.Value).ToList();
|
||||
}
|
||||
|
||||
if (reportType == "form")
|
||||
{
|
||||
return View(proceduresReport);
|
||||
}
|
||||
else if (reportType == "email")
|
||||
@ -620,13 +624,33 @@ namespace PolyclinicWebAppSuretor.Controllers
|
||||
|
||||
_suretorReportLogic.SaveProceduresToPdfFile(reportBindingModel);
|
||||
|
||||
// ×òåíèå ñãåíåðèðîâàííîãî ôàéëà
|
||||
// òóò ÿ êîðî÷å çàäàþ èìÿ ôàéëó äåôîëòíîå è ñîõðàíÿþ åãî â òåêóùóþ äèðåêòîðèþ (íó è â çàãðóçêè åñòåññíà)
|
||||
var fileName = "ProceduresReport.pdf";
|
||||
var filePath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
|
||||
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
|
||||
// Âîçâðàùàåì ôàéë â âèäå ñêà÷èâàåìîãî êîíòåíòà
|
||||
|
||||
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
|
||||
{
|
||||
return View(new List<ReportProceduresViewModel>());
|
||||
|
@ -25,6 +25,7 @@ builder.Services.AddTransient<AbstractSaveToWordCoursesByProcedures, SaveToWordC
|
||||
builder.Services.AddTransient<AbstractSaveToExcelCoursesByProcedure, SaveToExcelCoursesByProcedure>();
|
||||
builder.Services.AddTransient<AbstractSaveToPdfProcedures, SaveToPdfProcedures>();
|
||||
builder.Services.AddTransient<ISuretorReportLogic, SuretorReportLogic>();
|
||||
builder.Services.AddTransient<SendMailLogic>();
|
||||
|
||||
builder.Services.AddTransient<IDiagnoseLogic, DiagnoseLogic>();
|
||||
builder.Services.AddTransient<ICourseLogic, CourseLogic>();
|
||||
|
@ -54,7 +54,7 @@
|
||||
@if (LoginManager.LogginedUser != null)
|
||||
{
|
||||
<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">
|
||||
<button class="btn btn-secondary" type="submit">Выйти</button>
|
||||
</form>
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user