diff --git a/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/ClientLogic.cs b/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/ClientLogic.cs index 9fd4cb8..df32ffa 100644 --- a/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/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 JewelryStoreBusinessLogic.BusinessLogics @@ -119,6 +120,18 @@ namespace JewelryStoreBusinessLogic.BusinessLogics { throw new InvalidOperationException("Клиент с такой почтой уже есть"); } - } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля", nameof(model.Password)); + } + if (!Regex.IsMatch(model.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный email", nameof(model.Email)); + } + if (!Regex.IsMatch(model.Password, @"^^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))[\w\d\W]*$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный пароль", nameof(model.Password)); + } + } } } diff --git a/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs b/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs index 7e375ca..a669208 100644 --- a/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/JewelryStore/JewelryStoreBusinessLogic/BusinessLogics/OrderLogic.cs @@ -1,4 +1,5 @@ -using JewelryStoreContracts.BindingModels; +using JewelryStoreBusinessLogic.MailWorker; +using JewelryStoreContracts.BindingModels; using JewelryStoreContracts.BusinessLogicsContracts; using JewelryStoreContracts.SearchModels; using JewelryStoreContracts.StoragesContracts; @@ -19,12 +20,16 @@ namespace JewelryStoreBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; static readonly object locker = new object(); + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; // СПУСТЯ 4 ЛАБЫ ПРИШЛОСЬ ПЕРЕДЕЛЫВАТЬ ПОЛНОСТЬЮ И ДЕЛАТЬ АДЕКВАТНО ((( - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic) { _logger = logger; _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; } public OrderViewModel? ReadElement(OrderSearchModel model) @@ -85,11 +90,13 @@ namespace JewelryStoreBusinessLogic.BusinessLogics 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; } + SendOrderMessage(order.ClientId, $"DNS, Заказ №{order.Id}", $"Заказ №{order.Id} от {order.DateCreate} на сумму {order.Sum:0.00} принят"); return true; } @@ -111,7 +118,13 @@ namespace JewelryStoreBusinessLogic.BusinessLogics } model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - _orderStorage.Update(model); + var order = _orderStorage.Update(model); + if (order == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + SendOrderMessage(order.ClientId, $"DNS, Заказ №{order.Id}", $"Заказ №{order.Id} от {order.DateCreate} на сумму {order.Sum:0.00} принят"); return true; } @@ -132,6 +145,22 @@ namespace JewelryStoreBusinessLogic.BusinessLogics { return ChangeStatus(model, OrderStatus.Выдан); } + + private bool SendOrderMessage(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + if (client == null) + { + return false; + } + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + return true; + } } } diff --git a/JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj b/JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj index cf737eb..d5fee90 100644 --- a/JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj +++ b/JewelryStore/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj @@ -8,6 +8,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/JewelryStore/JewelryStoreBusinessLogic/MailWorker/AbstractMailWorker.cs b/JewelryStore/JewelryStoreBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..18a6e28 --- /dev/null +++ b/JewelryStore/JewelryStoreBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,80 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreBusinessLogic.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/JewelryStore/JewelryStoreBusinessLogic/MailWorker/MailKitWorker.cs b/JewelryStore/JewelryStoreBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..cdd92bd --- /dev/null +++ b/JewelryStore/JewelryStoreBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,79 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.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 JewelryStoreBusinessLogic.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 ex) + { + 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/JewelryStore/JewelryStoreContracts/BindingModels/MailConfigBindingModel.cs b/JewelryStore/JewelryStoreContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..c3b0412 --- /dev/null +++ b/JewelryStore/JewelryStoreContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreContracts.BindingModels +{ + public class MailConfigBindingModel + { + public string MailLogin { get; set; } = string.Empty; + public string MailPassword { get; set; } = string.Empty; + public string SmtpClientHost { get; set; } = string.Empty; + public int SmtpClientPort { get; set; } + public string PopHost { get; set; } = string.Empty; + public int PopPort { get; set; } + } + +} diff --git a/JewelryStore/JewelryStoreContracts/BindingModels/MailSendInfoBindingModel.cs b/JewelryStore/JewelryStoreContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..85328be --- /dev/null +++ b/JewelryStore/JewelryStoreContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +}