diff --git a/Confectionery/ConfectionaryContracts/BindingModels/MailConfigBindingModel.cs b/Confectionery/ConfectionaryContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..70714b7 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,17 @@ +namespace ConfectioneryContracts.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; } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectionaryContracts/BindingModels/MailSendInfoBindingModel.cs b/Confectionery/ConfectionaryContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..1695cc1 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,11 @@ +namespace ConfectioneryContracts.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; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectionaryContracts/BindingModels/MessageInfoBindingModel.cs b/Confectionery/ConfectionaryContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..a1066df --- /dev/null +++ b/Confectionery/ConfectionaryContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,19 @@ +using ConfectioneryDataModels.Models; + +namespace ConfectioneryContracts.BindingModels +{ + public class MessageInfoBindingModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public string Subject { get; set; } = string.Empty; + + public string Body { get; set; } = string.Empty; + + public DateTime DateDelivery { get; set; } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectionaryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/Confectionery/ConfectionaryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..7051bcc --- /dev/null +++ b/Confectionery/ConfectionaryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,12 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + bool Create(MessageInfoBindingModel model); + } +} \ No newline at end of file diff --git a/Confectionery/ConfectionaryContracts/SearchModels/MessageInfoSearchModel.cs b/Confectionery/ConfectionaryContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..7aa68e7 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,9 @@ +namespace ConfectioneryContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + + public string? MessageId { get; set; } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectionaryContracts/StoragesContracts/IMessageInfoStorage.cs b/Confectionery/ConfectionaryContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..1d27823 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,17 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + + List GetFilteredList(MessageInfoSearchModel model); + + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} \ No newline at end of file diff --git a/Confectionery/ConfectionaryContracts/ViewModels/MessageInfoViewModel.cs b/Confectionery/ConfectionaryContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..0924d66 --- /dev/null +++ b/Confectionery/ConfectionaryContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,24 @@ +using ConfectioneryDataModels.Models; +using System.ComponentModel; + +namespace ConfectioneryContracts.ViewModels +{ + public class MessageInfoViewModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + [DisplayName("Отправитель")] + public string SenderName { get; set; } = string.Empty; + + [DisplayName("Дата письма")] + public DateTime DateDelivery { get; set; } + + [DisplayName("Заголовок")] + public string Subject { get; set; } = string.Empty; + + [DisplayName("Текст")] + public string Body { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs b/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs index 2a2ef0c..a21727a 100644 --- a/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs +++ b/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs @@ -20,6 +20,7 @@ namespace ConfectioneryContracts.ViewModels [DisplayName("ФИО клиента")] public string ClientFIO { get; set; } = string.Empty; + public string ClientEmail { get; set; } = string.Empty; public int? ImplementerId { get; set; } [DisplayName("ФИО исполнителя")] diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs index da92bdd..cbe2602 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs @@ -1,14 +1,10 @@ -using ConfectioneryContracts.ViewModels; -using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Text.RegularExpressions; namespace ConfectioneryBusinessLogic.BusinessLogics { @@ -102,13 +98,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO)); } - if (string.IsNullOrEmpty(model.Email)) + if (string.IsNullOrEmpty(model.Email) || !Regex.IsMatch(model.Email, @"^[a-z0-9._%+-]+\@([a-z0-9-]+\.)+[a-z]{2,4}$")) { - throw new ArgumentNullException("Нет логина(почты) клиента", nameof(model.Email)); + throw new ArgumentNullException("Некорректно введен логин(почта) клиента", nameof(model.Email)); } - if (string.IsNullOrEmpty(model.Password)) + if (string.IsNullOrEmpty(model.Password) || !Regex.IsMatch(model.Password, @"^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z0-9\n]).{10,50}$")) { - throw new ArgumentNullException("Нет пароля клиента", nameof(model.Password)); + throw new ArgumentNullException("Некорректно введен пароль клиента", nameof(model.Password)); } _logger.LogInformation("Client. Id: {id}, FIO: {fio}, email: {email}, password: {password}", model.Id, model.ClientFIO, model.Email, model.Password); diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs index 2e96873..e6e4dba 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; -using ConfectioneryContracts.SearchModels; -using ConfectioneryContracts.StoragesContracts; using ConfectioneryContracts.ViewModels; using Microsoft.Extensions.Logging; diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs index fbf61e1..a822df7 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -1,16 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ConfectioneryBusinessLogic.MailWorker; -using ConfectioneryContracts.BindingModels; -using ConfectioneryContracts.SearchModels; -using ConfectioneryContracts.ViewModels; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContracts; -using Microsoft.Extensions.Logging; +using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels.Enums; +using ConfectioneryBusinessLogic.MailWorker; +using Microsoft.Extensions.Logging; using System.Xml.Linq; namespace ConfectioneryBusinessLogic.BusinessLogics @@ -21,12 +16,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; + static readonly object locker = new(); - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker) { _logger = logger; _orderStorage = orderStorage; + _mailWorker = mailWorker; } public bool CreateOrder(OrderBindingModel model) @@ -38,11 +36,18 @@ namespace ConfectioneryBusinessLogic.BusinessLogics return false; } model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) + var element = _orderStorage.Insert(model); + if (element == null) { _logger.LogWarning("Insert operation failed"); return false; } + Task.Run(() => _mailWorker.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = element.ClientEmail, + Subject = $"Новый заказ создан. Номер заказа - {element.Id}", + Text = $"Заказ №{element.Id} от {element.DateCreate} на сумму {element.Sum} принят." + })); return true; } @@ -58,6 +63,7 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } + public OrderViewModel? ReadElement(OrderSearchModel model) { if (model == null) @@ -156,6 +162,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogWarning("Change status operation failed"); return false; } + string DateInfo = model.DateImplement.HasValue ? $"Дата выполнения {model.DateImplement}" : ""; + Task.Run(() => _mailWorker.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = order.ClientEmail, + Subject = $"Заказ №{order.Id}", + Text = $"Заказ №{order.Id} изменен статус на {model.Status}. {DateInfo}" + })); return true; } } diff --git a/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj b/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj index a7ef9c7..0fb9741 100644 --- a/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj +++ b/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/Confectionery/ConfectioneryBusinessLogic/MailWorker/AbstractMailWorker.cs b/Confectionery/ConfectioneryBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..41dc4d6 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,91 @@ +using ConfectioneryBusinessLogic.BusinessLogics; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace ConfectioneryBusinessLogic.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; + } + + 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(); + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryBusinessLogic/MailWorker/MailKitWorker.cs b/Confectionery/ConfectioneryBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..91fd7b7 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,78 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using MailKit.Net.Pop3; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using System.Net; +using System.Net.Mail; +using System.Text; + +namespace ConfectioneryBusinessLogic.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; + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs b/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs index 56fc337..e69b162 100644 --- a/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs +++ b/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs @@ -142,5 +142,14 @@ namespace ConfectioneryClientApp.Controllers ); return count * (prod?.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/Confectionery/ConfectioneryClientApp/Views/Home/Mails.cshtml b/Confectionery/ConfectioneryClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..45afc00 --- /dev/null +++ b/Confectionery/ConfectioneryClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using ConfectioneryContracts.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) +
+ } +
\ No newline at end of file diff --git a/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml b/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml index 4513d75..0848540 100644 --- a/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml +++ b/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml @@ -27,6 +27,9 @@ Личные данные +