From d1bc1fe9ced82f29798f4d67b468c852d029334e Mon Sep 17 00:00:00 2001 From: GokaPek Date: Mon, 6 May 2024 11:56:25 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D1=81=D1=82=D0=B0=D1=80=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/AbstractLawFirmRestApi/appsettings.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/LawFirm/AbstractLawFirmRestApi/appsettings.json b/LawFirm/AbstractLawFirmRestApi/appsettings.json index 10f68b8..9ed609a 100644 --- a/LawFirm/AbstractLawFirmRestApi/appsettings.json +++ b/LawFirm/AbstractLawFirmRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "testfist24@gmail.com", + "MailPassword": "24test24" } + -- 2.25.1 From 5f69d317baf7feceb56402cc2b942156d4af56ca Mon Sep 17 00:00:00 2001 From: GokaPek Date: Mon, 6 May 2024 20:38:44 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=A7=D1=82=D0=BE-=D1=82=D0=BE=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B8=D0=B7=D0=BE=D1=88=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractLawFirmBusinessLogic.csproj | 1 + .../BusinessLogic/ClientLogic.cs | 10 +- .../BusinessLogic/MessageInfoLogic.cs | 51 +++ .../BusinessLogic/OrderLogic.cs | 56 ++- .../MailWorker/AbstractMailWorker.cs | 81 ++++ .../MailWorker/MailKitWorker.cs | 85 ++++ .../AbstractLawFirmClientAp.csproj | 16 + .../Controllers/HomeController.cs | 12 + LawFirm/AbstractLawFirmClientAp/Program.cs | 2 +- .../Views/Home/Mails.cshtml | 53 +++ .../Views/Shared/_Layout.cshtml | 4 + .../BindingModels/MailConfigBindingModel.cs | 18 + .../BindingModels/MailSendInfoBindingModel.cs | 15 + .../BindingModels/MessageInfoBindingModel.cs | 24 + .../IMessageInfoLogic.cs | 17 + .../SearchModels/MessageInfoSearchModel.cs | 14 + .../StoragesContracts/IMessageInfoStorage.cs | 19 + .../ViewModels/MessageInfoViewModel.cs | 25 ++ .../AbstractLawFirmDataBase.cs | 1 + .../AbstractLawFirmDataBaseImplement.csproj | 2 + .../Implements/ClientStorage.cs | 2 +- .../Implements/MessageInfoStorage.cs | 59 +++ .../Models/Client.cs | 4 +- .../Models/MessageInfo.cs | 54 +++ .../DataFileSingleton.cs | 6 +- .../Implements/MessageInfoStorage.cs | 58 +++ .../Models/MessageInfo.cs | 80 ++++ .../DataListSingleton.cs | 7 +- .../Implements/MessageInfoStorage .cs | 73 ++++ .../Models/MessageInfo.cs | 51 +++ .../Controllers/ClientController.cs | 27 +- LawFirm/AbstractLawFirmRestApi/Program.cs | 16 + .../LawFirmView/FormImplementer.Designer.cs | 2 +- LawFirm/LawFirmView/FormImplementer.cs | 4 +- LawFirm/LawFirmView/FormImplementers.cs | 8 +- LawFirm/LawFirmView/FormMail.Designer.cs | 62 +++ LawFirm/LawFirmView/FormMail.cs | 51 +++ LawFirm/LawFirmView/FormMail.resx | 120 +++++ LawFirm/LawFirmView/FormMain.cs | 10 +- LawFirm/LawFirmView/FormMain.designer.cs | 413 +++++++++--------- LawFirm/LawFirmView/LawFirmView.csproj | 3 + LawFirm/LawFirmView/Program.cs | 59 ++- 42 files changed, 1438 insertions(+), 237 deletions(-) create mode 100644 LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/MessageInfoLogic.cs create mode 100644 LawFirm/AbstractLawFirmBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 LawFirm/AbstractLawFirmBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 LawFirm/AbstractLawFirmClientAp/Views/Home/Mails.cshtml create mode 100644 LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/SearchModels/MessageInfoSearchModel.cs create mode 100644 LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/StoragesContracts/IMessageInfoStorage.cs create mode 100644 LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/ViewModels/MessageInfoViewModel.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/MessageInfo.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Implements/MessageInfoStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Models/MessageInfo.cs create mode 100644 LawFirm/AbstractLawFirmListImplement/Implements/MessageInfoStorage .cs create mode 100644 LawFirm/AbstractLawFirmListImplement/Models/MessageInfo.cs create mode 100644 LawFirm/LawFirmView/FormMail.Designer.cs create mode 100644 LawFirm/LawFirmView/FormMail.cs create mode 100644 LawFirm/LawFirmView/FormMail.resx diff --git a/LawFirm/AbstractLawFirmBusinessLogic/AbstractLawFirmBusinessLogic.csproj b/LawFirm/AbstractLawFirmBusinessLogic/AbstractLawFirmBusinessLogic.csproj index eea09e0..d16b4e2 100644 --- a/LawFirm/AbstractLawFirmBusinessLogic/AbstractLawFirmBusinessLogic.csproj +++ b/LawFirm/AbstractLawFirmBusinessLogic/AbstractLawFirmBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ClientLogic.cs b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ClientLogic.cs index dd43137..820a8cb 100644 --- a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ClientLogic.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Text.RegularExpressions; namespace AbstractLawFirmBusinessLogic.BusinessLogic { @@ -99,9 +100,14 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic { throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO)); } - if (string.IsNullOrEmpty(model.Email)) + if (string.IsNullOrEmpty(model.Email) || !Regex.IsMatch(model.Email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", RegexOptions.IgnoreCase)) { - throw new ArgumentNullException("Нет логина(почты) клиента", nameof(model.Email)); + throw new ArgumentNullException("Нет логина(почты) клиента или он введён неверно", nameof(model.Email)); + } + _logger.LogInformation("Client. Id: {Id}, FIO: {fio}, email: {email}", model.Id, model.ClientFIO, model.Email); + if (string.IsNullOrEmpty(model.Password) || !Regex.IsMatch(model.Password, @"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$")) + { + throw new ArgumentNullException("Нет пароля клиента или он введён неверно", nameof(model.Password)); } _logger.LogInformation("Client. Id: {Id}, FIO: {fio}, email: {email}", model.Id, model.ClientFIO, model.Email); var element = _clientStorage.GetElement(new ClientSearchModel diff --git a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/MessageInfoLogic.cs b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/MessageInfoLogic.cs new file mode 100644 index 0000000..b1e9cab --- /dev/null +++ b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/MessageInfoLogic.cs @@ -0,0 +1,51 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.BusinessLogicsContracts; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmBusinessLogic.BusinessLogic +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + + private readonly IMessageInfoStorage _messageStorage; + + public MessageInfoLogic(IMessageInfoStorage messageStorage, ILogger logger) + { + _messageStorage = messageStorage; + _logger = logger; + } + public bool Create(MessageInfoBindingModel model) + { + if (_messageStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(MessageInfoSearchModel? model) + { + _logger.LogInformation("ReadList. ClientID:{ClientID}. ID:{ ID}", model?.ClientId, model?.MessageId); + + var list = model == null ? _messageStorage.GetFullList() : _messageStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + } +} diff --git a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs index 3e48fea..7d4be3f 100644 --- a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs @@ -1,4 +1,5 @@ -using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmBusinessLogic.MailWorker; +using AbstractLawFirmContracts.BindingModels; using AbstractLawFirmContracts.BusinessLogicsContracts; using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.StoragesContracts; @@ -17,12 +18,16 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic { private readonly ILogger _logger; static readonly object locker = new object(); + private readonly IClientStorage _clientStorage; + private readonly AbstractMailWorker _mailLogic; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IClientStorage clientStorage, AbstractMailWorker worker) { _logger = logger; _orderStorage = orderStorage; + _clientStorage = clientStorage; + _mailLogic = worker; } public List? ReadList(OrderSearchModel? model) @@ -59,11 +64,15 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic CheckModel(model); if (model.Status != OrderStatus.Неизвестен) return false; model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) + var order = _orderStorage.Insert(model); + if (order == null) { _logger.LogWarning("Insert operation failed"); return false; } + var clientView = _clientStorage.GetElement(new() { Id = order!.ClientId }); + + SendEmail(clientView, order); return true; } @@ -87,7 +96,15 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic if (element.ImplementerId.HasValue) { model.ImplementerId = element.ImplementerId; } - _orderStorage.Update(model); + element = _orderStorage.Update(model); + if (element == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + var clientView = _clientStorage.GetElement(new() { Id = element.ClientId }); + + SendEmail(clientView, element); return true; } @@ -130,5 +147,36 @@ true) } _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); } + public void SendEmail(ClientViewModel clientModel, OrderViewModel orderModel) + { + if (clientModel == null && orderModel == null) + { + return; + } + + MailSendInfoBindingModel mailModel; + + if (orderModel.Status == OrderStatus.Принят) + { + mailModel = new MailSendInfoBindingModel + { + MailAddress = clientModel.Email, + Subject = $"Order №{orderModel.Id}", + Text = $"Your order №{orderModel.Id} by {orderModel.DateCreate} on {orderModel.Sum} was accepted!" + }; + } + else + { + mailModel = new MailSendInfoBindingModel + { + MailAddress = clientModel.Email, + Subject = $"Order №{orderModel.Id}", + Text = $"Order №{orderModel.Id} status was changed to {orderModel.Status}" + }; + } + + + _mailLogic.MailSendAsync(mailModel); + } } } diff --git a/LawFirm/AbstractLawFirmBusinessLogic/MailWorker/AbstractMailWorker.cs b/LawFirm/AbstractLawFirmBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..354bba7 --- /dev/null +++ b/LawFirm/AbstractLawFirmBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,81 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmBusinessLogic.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 IMessageInfoLogic _messageInfoLogic; + private readonly ILogger _logger; + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) + { + _logger = logger; + _messageInfoLogic = messageInfoLogic; + _logger.LogWarning("INIT ABSTRACT MAIL WORKER"); + } + 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); + } + public async void MailCheck() + { + if (string.IsNullOrEmpty(_mailLogin) || + string.IsNullOrEmpty(_mailPassword)) + { + return; + } + if (string.IsNullOrEmpty(_popHost) || _popPort == 0) + { + return; + } + if (_messageInfoLogic == null) + { + return; + } + var list = await ReceiveMailAsync(); + _logger.LogDebug("Check Mail: {Count} new mails", list.Count); + foreach (var mail in list) + { + _messageInfoLogic.Create(mail); + } + } + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + protected abstract Task> ReceiveMailAsync(); + + } +} diff --git a/LawFirm/AbstractLawFirmBusinessLogic/MailWorker/MailKitWorker.cs b/LawFirm/AbstractLawFirmBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..34c70e7 --- /dev/null +++ b/LawFirm/AbstractLawFirmBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,85 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.BusinessLogicsContracts; +using MailKit.Net.Pop3; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IMessageInfoLogic +messageInfoLogic) : base(logger, messageInfoLogic) { } + 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; + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, + _mailPassword); + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch (Exception) + { + throw; + } + } + protected override async Task> + ReceiveMailAsync() + { + var list = new List(); + using var client = new Pop3Client(); + await Task.Run(() => + { + try + { + client.CheckCertificateRevocation = false; + client.Connect(_popHost, _popPort, SecureSocketOptions.SslOnConnect); + client.Authenticate(_mailLogin, _mailPassword); + for (int i = 0; i < client.Count; i++) + { + var message = client.GetMessage(i); + foreach (var mail in message.From.Mailboxes) + { + list.Add(new MessageInfoBindingModel + { + DateDelivery = message.Date.DateTime, + MessageId = message.MessageId, + SenderName = mail.Address, + Subject = message.Subject, + Body = message.TextBody + }); + } + } + } + catch (AuthenticationException) + { } + finally + { + client.Disconnect(true); + } + }); + return list; + } + + } +} diff --git a/LawFirm/AbstractLawFirmClientAp/AbstractLawFirmClientAp.csproj b/LawFirm/AbstractLawFirmClientAp/AbstractLawFirmClientAp.csproj index ff4659a..84b7f04 100644 --- a/LawFirm/AbstractLawFirmClientAp/AbstractLawFirmClientAp.csproj +++ b/LawFirm/AbstractLawFirmClientAp/AbstractLawFirmClientAp.csproj @@ -7,11 +7,27 @@ + + + + + + + + + + + <_ContentIncludedByDefault Remove="Views\Home\Mails.cshtml" /> + + + + + diff --git a/LawFirm/AbstractLawFirmClientAp/Controllers/HomeController.cs b/LawFirm/AbstractLawFirmClientAp/Controllers/HomeController.cs index 13fe54c..c025943 100644 --- a/LawFirm/AbstractLawFirmClientAp/Controllers/HomeController.cs +++ b/LawFirm/AbstractLawFirmClientAp/Controllers/HomeController.cs @@ -3,6 +3,7 @@ using AbstractLawFirmClientAp; using AbstractLawFirmContracts.BindingModels; using AbstractLawFirmContracts.ViewModels; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using System.Diagnostics; namespace AbstractLawFirmClientAp.Controllers @@ -133,7 +134,18 @@ namespace AbstractLawFirmClientAp.Controllers { var doc = APIClient.GetRequest($"api/main/getdocument?documentId={document}"); return count * (doc?.Price ?? 1); + + } + [HttpGet] + public IActionResult Mails() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}")); + } } } diff --git a/LawFirm/AbstractLawFirmClientAp/Program.cs b/LawFirm/AbstractLawFirmClientAp/Program.cs index 31e0316..542ed06 100644 --- a/LawFirm/AbstractLawFirmClientAp/Program.cs +++ b/LawFirm/AbstractLawFirmClientAp/Program.cs @@ -3,7 +3,7 @@ using AbstractLawFirmClientAp; var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddControllersWithViews(); +builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(); var app = builder.Build(); APIClient.Connect(builder.Configuration); diff --git a/LawFirm/AbstractLawFirmClientAp/Views/Home/Mails.cshtml b/LawFirm/AbstractLawFirmClientAp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..6bb6631 --- /dev/null +++ b/LawFirm/AbstractLawFirmClientAp/Views/Home/Mails.cshtml @@ -0,0 +1,53 @@ +@using AbstractLawFirmContracts.ViewModels +@model List + +@{ + ViewData["Title"] = "Mails"; +} + +
+

Письма

+
+ + +
+ @{ + if (Model == null) + { +

Login bobr!

+ return; + } + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
+ Дата + + Заголовок + + Текст +
+ @Html.DisplayFor(modelItem => item.DateDelivery) + + @Html.DisplayFor(modelItem => item.Subject) + + @Html.DisplayFor(modelItem => item.Body) +
+ } +
diff --git a/LawFirm/AbstractLawFirmClientAp/Views/Shared/_Layout.cshtml b/LawFirm/AbstractLawFirmClientAp/Views/Shared/_Layout.cshtml index 367efd0..12597ff 100644 --- a/LawFirm/AbstractLawFirmClientAp/Views/Shared/_Layout.cshtml +++ b/LawFirm/AbstractLawFirmClientAp/Views/Shared/_Layout.cshtml @@ -24,9 +24,13 @@ Заказы +