From 0dd34f939e4279612a59fbda77283efc5ed0727d Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Sun, 5 May 2024 00:28:40 +0400 Subject: [PATCH 1/5] ******** --- .../AbstractLawFirmBusinessLogic.csproj | 1 + .../BusinessLogic/ClientLogic.cs | 11 +- .../BusinessLogic/MessageInfoLogic.cs | 51 +++ .../BusinessLogic/OrderLogic.cs | 65 +++- .../MailWorker/AbstractMailWorker.cs | 81 +++++ .../MailWorker/MailKitWorker.cs | 85 +++++ .../AbstractLawFirmClientApp.csproj | 16 + .../Controllers/HomeController.cs | 13 +- .../Views/Home/Mails.cshtml | 51 +++ .../Views/Shared/_Layout.cshtml | 3 + .../BindingModels/MailConfigBindingModel.cs | 17 + .../BindingModels/MailSendInfoBindingModel.cs | 15 + .../BindingModels/MessageInfoBindingModel.cs | 24 ++ .../IMessageInfoLogic.cs | 17 + .../SearchModels/MessageInfoSearchModel.cs | 14 + .../StoragesContracts/IMessageInfoStorage.cs | 19 + .../ViewModels/MessageInfoViewModel.cs | 25 ++ .../Models/IMessageInfoModel.cs | 18 + .../AbstractLawFirmDatabase.cs | 3 +- .../AbstractLawFirmDatabaseImplement.csproj | 2 + .../Implements/ClientStorage.cs | 2 +- .../Implements/MessageInfoStorage.cs | 59 ++++ .../20240503221938_ForLabSeven.Designer.cs | 298 ++++++++++++++++ .../Migrations/20240503221938_ForLabSeven.cs | 48 +++ .../AbstractLawFirmDatabaseModelSnapshot.cs | 41 +++ .../Models/Client.cs | 4 +- .../Models/MessageInfo.cs | 54 +++ .../DataFileSingleton.cs | 12 +- .../Implements/MessageInfoStorage.cs | 58 ++++ .../Models/MessageInfo.cs | 80 +++++ .../DataListSingleton.cs | 6 +- .../Implements/MessageInfoStorage.cs | 73 ++++ .../Models/MessageInfo.cs | 51 +++ .../Controllers/ClientController.cs | 24 +- LawFirm/AbstractLawFirmRestApi/Program.cs | 19 +- .../AbstractLawFirmRestApi/appsettings.json | 8 +- LawFirm/LawFirmView/App.config | 11 + LawFirm/LawFirmView/FormMail.Designer.cs | 62 ++++ LawFirm/LawFirmView/FormMail.cs | 51 +++ LawFirm/LawFirmView/FormMail.resx | 60 ++++ LawFirm/LawFirmView/FormMain.Designer.cs | 328 +++++++++--------- LawFirm/LawFirmView/FormMain.cs | 11 +- LawFirm/LawFirmView/LawFirmView.csproj | 3 + LawFirm/LawFirmView/Program.cs | 46 ++- 44 files changed, 1746 insertions(+), 194 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/AbstractLawFirmClientApp/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/AbstractLawFirmDataModels/AbstractLawFirmDataModels/Models/IMessageInfoModel.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240503221938_ForLabSeven.Designer.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240503221938_ForLabSeven.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/MessageInfo.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Implements/MessageInfoStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Models/MessageInfo.cs create mode 100644 LawFirm/AbstractLawFirmListImplement/Implements/MessageInfoStorage.cs create mode 100644 LawFirm/AbstractLawFirmListImplement/Models/MessageInfo.cs create mode 100644 LawFirm/LawFirmView/App.config 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 e013934..606e0fc 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 2d97545..810f38f 100644 --- a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ClientLogic.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace AbstractLawFirmBusinessLogic.BusinessLogic @@ -99,11 +100,15 @@ 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 { Email = model.Email, diff --git a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/MessageInfoLogic.cs b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/MessageInfoLogic.cs new file mode 100644 index 0000000..5db2411 --- /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 aca7bdc..2d4adb6 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,13 +18,17 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IClientStorage _clientStorage; + private readonly AbstractMailWorker _mailLogic; static readonly object locker = new object(); - 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) { @@ -60,12 +65,16 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic CheckModel(model); if (model.Status != OrderStatus.Неизвестен) return false; model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - return true; + 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; } public bool ChangeStatus(OrderBindingModel model, OrderStatus status) @@ -86,7 +95,10 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; if (element.ImplementerId.HasValue) model.ImplementerId = element.ImplementerId; _orderStorage.Update(model); - return true; + var clientView = _clientStorage.GetElement(new() { Id = element.ClientId }); + + SendEmail(clientView, element); + return true; } public bool TakeOrderInWork(OrderBindingModel model) @@ -128,5 +140,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..a16bf14 --- /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..c847622 --- /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.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/AbstractLawFirmClientApp/AbstractLawFirmClientApp.csproj b/LawFirm/AbstractLawFirmClientApp/AbstractLawFirmClientApp.csproj index 8aed5be..0bfc956 100644 --- a/LawFirm/AbstractLawFirmClientApp/AbstractLawFirmClientApp.csproj +++ b/LawFirm/AbstractLawFirmClientApp/AbstractLawFirmClientApp.csproj @@ -6,6 +6,10 @@ enable + + + + @@ -14,4 +18,16 @@ + + + + + + <_ContentIncludedByDefault Remove="Views\Home\Mails.cshtml" /> + + + + + + diff --git a/LawFirm/AbstractLawFirmClientApp/Controllers/HomeController.cs b/LawFirm/AbstractLawFirmClientApp/Controllers/HomeController.cs index 7a7a4a6..2c0da6b 100644 --- a/LawFirm/AbstractLawFirmClientApp/Controllers/HomeController.cs +++ b/LawFirm/AbstractLawFirmClientApp/Controllers/HomeController.cs @@ -2,6 +2,7 @@ using AbstractLawFirmContracts.BindingModels; using AbstractLawFirmContracts.ViewModels; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using System.Diagnostics; namespace AbstractLawFirmClientApp.Controllers @@ -134,6 +135,16 @@ namespace AbstractLawFirmClientApp.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}")); +} - } + } } \ No newline at end of file diff --git a/LawFirm/AbstractLawFirmClientApp/Views/Home/Mails.cshtml b/LawFirm/AbstractLawFirmClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..7b340d4 --- /dev/null +++ b/LawFirm/AbstractLawFirmClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,51 @@ +@using AbstractLawFirmContracts.ViewModels +@model List +@{ + ViewData["Title"] = "Mails"; +} +
+

Заказы

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

Авторизируйтесь

+ 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/AbstractLawFirmClientApp/Views/Shared/_Layout.cshtml b/LawFirm/AbstractLawFirmClientApp/Views/Shared/_Layout.cshtml index 367efd0..5d7a49f 100644 --- a/LawFirm/AbstractLawFirmClientApp/Views/Shared/_Layout.cshtml +++ b/LawFirm/AbstractLawFirmClientApp/Views/Shared/_Layout.cshtml @@ -27,6 +27,9 @@ Личные данные +