From 3b3b9343f3642c2fd78a74b744f11b544f4f7151 Mon Sep 17 00:00:00 2001 From: malimova Date: Tue, 4 Jun 2024 00:46:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D0=B8?= =?UTF-8?q?=20=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=BA=D0=BE=D0=BD=D0=B5?= =?UTF-8?q?=D1=86=207=20=D0=BB=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractMailWorker.cs | 85 ++++++++ .../ConfectioneryBusinessLogic/ClientLogic.cs | 205 +++++++++--------- .../ConfectioneryBusinessLogic.csproj | 1 + .../MailKitWorker.cs | 82 +++++++ .../MessageInfoLogic.cs | 90 ++++++++ .../ConfectioneryBusinessLogic/OrderLogic.cs | 74 ++++--- .../Controllers/HomeController.cs | 12 + .../Views/Home/Mails.cshtml | 51 +++++ .../Views/Shared/_Layout.cshtml | 3 + .../BindingModels/MailConfigBindingModel.cs | 18 ++ .../BindingModels/MailSendInfoBindingModel.cs | 15 ++ .../BindingModels/MessageInfoBindingModel.cs | 19 ++ .../IMessageInfoLogic.cs | 18 ++ .../SearchModels/MessageInfoSearchModel.cs | 14 ++ .../StoragesContracts/IMessageInfoStorage.cs | 19 ++ .../ViewModels/ClientViewModel.cs | 23 +- .../ViewModels/MessageInfoViewModel.cs | 29 +++ .../ViewModels/OrderViewModel.cs | 1 + .../ConfectioneryDataModels/IClientModel.cs | 2 +- .../IMessageInfoModel.cs | 18 ++ .../ConfectioneryDatabaseImplement/Client.cs | 109 +++++----- .../ClientStorage.cs | 129 ++++++----- .../ConfectioneryDatabase.cs | 31 +-- .../MessageInfo.cs | 64 ++++++ .../MessageInfoStorage.cs | 52 +++++ .../ConfectioneryDatabaseImplement/Order.cs | 1 + .../DataFileSingleton.cs | 103 ++++----- .../ConfectioneryFileImplement/MessageInfo.cs | 80 +++++++ .../MessageInfoStorage.cs | 59 +++++ .../DataListSingleton.cs | 2 + .../ConfectioneryListImplement/MessageInfo.cs | 53 +++++ .../MessageInfoStorage.cs | 68 ++++++ .../Controllers/ClientController.cs | 29 ++- Confectionery/ConfectioneryRestApi/Program.cs | 34 ++- .../ConfectioneryRestApi/appsettings.json | 8 +- Confectionery/ConfectioneryView/App.config | 11 + 36 files changed, 1268 insertions(+), 344 deletions(-) create mode 100644 Confectionery/ConfectioneryBusinessLogic/AbstractMailWorker.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/MailKitWorker.cs create mode 100644 Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs create mode 100644 Confectionery/ConfectioneryClientApp/Views/Home/Mails.cshtml create mode 100644 Confectionery/ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 Confectionery/ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 Confectionery/ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 Confectionery/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs create mode 100644 Confectionery/ConfectioneryContracts/StoragesContracts/IMessageInfoStorage.cs create mode 100644 Confectionery/ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs create mode 100644 Confectionery/ConfectioneryDataModels/IMessageInfoModel.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/MessageInfo.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/MessageInfoStorage.cs create mode 100644 Confectionery/ConfectioneryFileImplement/MessageInfo.cs create mode 100644 Confectionery/ConfectioneryFileImplement/MessageInfoStorage.cs create mode 100644 Confectionery/ConfectioneryListImplement/MessageInfo.cs create mode 100644 Confectionery/ConfectioneryListImplement/MessageInfoStorage.cs create mode 100644 Confectionery/ConfectioneryView/App.config diff --git a/Confectionery/ConfectioneryBusinessLogic/AbstractMailWorker.cs b/Confectionery/ConfectioneryBusinessLogic/AbstractMailWorker.cs new file mode 100644 index 0000000..ae7d120 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/AbstractMailWorker.cs @@ -0,0 +1,85 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using ConfectioneryContracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic +{ + 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.Length, _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/Confectionery/ConfectioneryBusinessLogic/ClientLogic.cs b/Confectionery/ConfectioneryBusinessLogic/ClientLogic.cs index 0ebb028..f214814 100644 --- a/Confectionery/ConfectioneryBusinessLogic/ClientLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/ClientLogic.cs @@ -4,6 +4,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContracts; using ConfectioneryContracts.ViewModels; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; using System; using System.Collections.Generic; using System.Linq; @@ -12,113 +13,113 @@ using System.Threading.Tasks; namespace ConfectioneryBusinessLogic { - public class ClientLogic : IClientLogic - { - private readonly ILogger _logger; - private readonly IClientStorage _clientStorage; + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; - public ClientLogic(ILogger logger, IClientStorage clientStorage) - { - _logger = logger; - _clientStorage = clientStorage; - } + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } - public List? ReadList(ClientSearchModel? model) - { - _logger.LogInformation("ReadList. ClientId:{Id}", model?.Id); - var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } - _logger.LogInformation("ReadList. Count:{Count}", list.Count); - return list; - } + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. ClientId:{Id}", model?.Id); + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } - public ClientViewModel? ReadElement(ClientSearchModel model) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - _logger.LogInformation("ReadElement. ClientFio:{ClientFio}.Id:{ Id}", model.ClientFIO, model.Id); - var element = _clientStorage.GetElement(model); - if (element == null) - { - _logger.LogWarning("ReadElement element not found"); - return null; - } - _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); - return element; - } + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ClientFio:{ClientFio}.Id:{ Id}", model.ClientFIO, model.Id); + var element = _clientStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } - public bool Create(ClientBindingModel model) - { - CheckModel(model); - if (_clientStorage.Insert(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - return true; - } + public bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } - public bool Update(ClientBindingModel model) - { - CheckModel(model); - if (_clientStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } - public bool Delete(ClientBindingModel model) - { - CheckModel(model, false); - _logger.LogInformation("Delete. Id:{Id}", model.Id); - if (_clientStorage.Delete(model) == null) - { - _logger.LogWarning("Delete operation failed"); - return false; - } - return true; - } + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } - private void CheckModel(ClientBindingModel model, bool withParams = true) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - if (!withParams) - { - return; - } - if (string.IsNullOrEmpty(model.ClientFIO)) - { - throw new ArgumentNullException("Нет ФИО пользователя", nameof(model.ClientFIO)); - } - if (string.IsNullOrEmpty(model.Email)) - { - throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email)); - } - if (string.IsNullOrEmpty(model.Password)) - { - throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); - } - _logger.LogInformation("Client. ClientFIO:{ClientFIO}.Email:{Email}.Password:{Password}.Id:{Id}", - model.ClientFIO, model.Email, model.Password, model.Id); - var element = _clientStorage.GetElement(new ClientSearchModel - { - ClientFIO = model.ClientFIO - }); - if (element != null && element.Id != model.Id) - { - throw new InvalidOperationException("Клиент с таким именем уже есть"); - } - } - } + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Нет ФИО пользователя", nameof(model.ClientFIO)); + } + 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)); + } + 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)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}.Email:{Email}.Id:{Id}", + model.ClientFIO, model.Email, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel + { + ClientFIO = model.ClientFIO + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким именем уже есть"); + } + } + } } diff --git a/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj b/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj index 06e699f..112e5d5 100644 --- a/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj +++ b/Confectionery/ConfectioneryBusinessLogic/ConfectioneryBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/Confectionery/ConfectioneryBusinessLogic/MailKitWorker.cs b/Confectionery/ConfectioneryBusinessLogic/MailKitWorker.cs new file mode 100644 index 0000000..1474cb3 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/MailKitWorker.cs @@ -0,0 +1,82 @@ +using MailKit.Net.Pop3; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic +{ + 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 (MailKit.Security.AuthenticationException) + { } + finally + { + client.Disconnect(true); + } + }); + return list; + } + } +} diff --git a/Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs b/Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs new file mode 100644 index 0000000..68eaf1d --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs @@ -0,0 +1,90 @@ +using Microsoft.Extensions.Logging; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + private readonly IMessageInfoStorage _messageInfoStorage; + private readonly IClientStorage _clientStorage; + public MessageInfoLogic(ILogger logger, IMessageInfoStorage messageInfoStorage, IClientStorage clientStorage) + { + _logger = logger; + _messageInfoStorage = messageInfoStorage; + _clientStorage = clientStorage; + } + public List? ReadList(MessageInfoSearchModel? model) + { + _logger.LogInformation("ReadList. MessageId:{MessageId}.ClientId:{ClientId}", model?.MessageId, model?.ClientId); + var list = model == null ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public bool Create(MessageInfoBindingModel model) + { + CheckModel(model); + if (_messageInfoStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + private void CheckModel(MessageInfoBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.MessageId)) + { + throw new ArgumentNullException("Не указан id сообщения", nameof(model.MessageId)); + } + if (string.IsNullOrEmpty(model.SenderName)) + { + throw new ArgumentNullException("Не указана почта", nameof(model.SenderName)); + } + if (string.IsNullOrEmpty(model.Subject)) + { + throw new ArgumentNullException("Не указана тема", nameof(model.Subject)); + } + if (string.IsNullOrEmpty(model.Body)) + { + throw new ArgumentNullException("Не указан текст сообщения", nameof(model.Subject)); + } + + _logger.LogInformation("MessageInfo. MessageId:{MessageId}.SenderName:{SenderName}.Subject:{Subject}.Body:{Body}", model.MessageId, model.SenderName, model.Subject, model.Body); + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.SenderName + }); + if (element == null) + { + _logger.LogWarning("Не удалось найти клиента, отправившего письмо с адреса Email:{Email}", model.SenderName); + } + else + { + model.ClientId = element.Id; + } + } + } +} diff --git a/Confectionery/ConfectioneryBusinessLogic/OrderLogic.cs b/Confectionery/ConfectioneryBusinessLogic/OrderLogic.cs index 65049ef..b114f76 100644 --- a/Confectionery/ConfectioneryBusinessLogic/OrderLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/OrderLogic.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace ConfectioneryBusinessLogic { @@ -17,16 +18,35 @@ namespace ConfectioneryBusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; static readonly object _locker = new object(); - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker) { _logger = logger; _orderStorage = orderStorage; + _mailWorker = mailWorker; + } + public OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", + model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id); + var element = _orderStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; } public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", - model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id); + model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { @@ -42,22 +62,24 @@ namespace ConfectioneryBusinessLogic if (model.Status != OrderStatus.Неизвестен) 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.PastryName} от {element.DateCreate} на сумму {element.Sum} принят." + })); return true; } - public bool TakeOrderInWork(OrderBindingModel model) { - lock (_locker) - { - return ChangeStatus(model, OrderStatus.Выполняется); - } + return ChangeStatus(model, OrderStatus.Выполняется); } - public bool FinishOrder(OrderBindingModel model) { return ChangeStatus(model, OrderStatus.Готов); @@ -80,7 +102,7 @@ namespace ConfectioneryBusinessLogic } if (model.Count <= 0) { - throw new ArgumentException("Колличество суши в заказе не может быть меньше 1", nameof(model.Count)); + throw new ArgumentException("Колличество кондитерских изделий в заказе не может быть меньше 1", nameof(model.Count)); } if (model.Sum <= 0) { @@ -90,7 +112,7 @@ namespace ConfectioneryBusinessLogic { throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}"); } - _logger.LogInformation("Sushi. SushiId:{SushiId}.Count:{Count}.Sum:{Sum}Id:{Id}", + _logger.LogInformation("Pastry. PastryId:{PastryId}.Count:{Count}.Sum:{Sum}Id:{Id}", model.PastryId, model.Count, model.Sum, model.Id); } @@ -103,7 +125,7 @@ namespace ConfectioneryBusinessLogic }); if (element == null) { - throw new InvalidOperationException(nameof(element)); + throw new ArgumentNullException(nameof(element)); } model.DateCreate = element.DateCreate; model.PastryId = element.PastryId; @@ -128,29 +150,17 @@ namespace ConfectioneryBusinessLogic _logger.LogWarning("Update operation failed"); return false; } + string DateInfo = model.DateImplement.HasValue ? $"Дата выполнения {model.DateImplement}" : ""; + Task.Run(() => _mailWorker.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = element.ClientEmail, + Subject = $"Изменение статуса заказа №{element.Id}", + Text = $"Заказ №{element.Id} на кондитерское изделие {element.PastryName} от {element.DateCreate} на сумму {element.Sum} {model.Status}. {DateInfo}" + })); return true; } _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); - throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); - - } - - public OrderViewModel? ReadElement(OrderSearchModel model) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - _logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}", - model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id); - var element = _orderStorage.GetElement(model); - if (element == null) - { - _logger.LogWarning("ReadElement element not found"); - return null; - } - _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); - return element; + throw new ArgumentException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); } } } diff --git a/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs b/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs index e40a17a..82d6012 100644 --- a/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs +++ b/Confectionery/ConfectioneryClientApp/Controllers/HomeController.cs @@ -4,6 +4,7 @@ using ConfectioneryClientApp.Models; using ConfectioneryClientApp; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using Microsoft.Extensions.Logging; namespace ConfectioneryClientApp.Controllers { @@ -140,5 +141,16 @@ namespace ConfectioneryClientApp.Controllers var prod = APIClient.GetRequest($"api/main/getpastry?pastryId={pastry}"); 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}")); + } } } diff --git a/Confectionery/ConfectioneryClientApp/Views/Home/Mails.cshtml b/Confectionery/ConfectioneryClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..cfc5f7c --- /dev/null +++ b/Confectionery/ConfectioneryClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,51 @@ +@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 fc0c10f..c65aa11 100644 --- a/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml +++ b/Confectionery/ConfectioneryClientApp/Views/Shared/_Layout.cshtml @@ -26,6 +26,9 @@ + diff --git a/Confectionery/ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..0d2aa83 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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; } + } +} diff --git a/Confectionery/ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..8fd8e76 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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; + } +} diff --git a/Confectionery/ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..e101807 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,19 @@ +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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; } + } +} diff --git a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..be28a6a --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; + + +namespace ConfectioneryContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + bool Create(MessageInfoBindingModel model); + } +} diff --git a/Confectionery/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs b/Confectionery/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..68df87e --- /dev/null +++ b/Confectionery/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + public string? MessageId { get; set; } + } +} diff --git a/Confectionery/ConfectioneryContracts/StoragesContracts/IMessageInfoStorage.cs b/Confectionery/ConfectioneryContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..3ca260e --- /dev/null +++ b/Confectionery/ConfectioneryContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,19 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + List GetFilteredList(MessageInfoSearchModel model); + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs index a8faf87..7b837cb 100644 --- a/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs +++ b/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs @@ -1,4 +1,5 @@ -using System; +using ConfectioneryDataModels.Models; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -7,14 +8,14 @@ using System.Threading.Tasks; namespace ConfectioneryContracts.ViewModels { - public class ClientViewModel - { - public int Id { get; set; } - [DisplayName("ФИО клиента")] - public string ClientFIO { get; set; } = string.Empty; - [DisplayName("Логин (эл. почта)")] - public string Email { get; set; } = string.Empty; - [DisplayName("Пароль")] - public string Password { get; set; } = string.Empty; - } + public class ClientViewModel : IClientModel + { + public int Id { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Логин (эл. почта)")] + public string Email { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } } diff --git a/Confectionery/ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..6f8e090 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,29 @@ +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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; + } +} diff --git a/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs index ebdcf7d..e1dbe9f 100644 --- a/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs +++ b/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs @@ -17,6 +17,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("Исполнитель")] public string? ImplementerFIO { get; set; } = null; diff --git a/Confectionery/ConfectioneryDataModels/IClientModel.cs b/Confectionery/ConfectioneryDataModels/IClientModel.cs index ddad9f3..004fd69 100644 --- a/Confectionery/ConfectioneryDataModels/IClientModel.cs +++ b/Confectionery/ConfectioneryDataModels/IClientModel.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ConfectioneryDataModels.Models { - public interface IClientModel + public interface IClientModel : IId { string ClientFIO { get; } string Email { get; } diff --git a/Confectionery/ConfectioneryDataModels/IMessageInfoModel.cs b/Confectionery/ConfectioneryDataModels/IMessageInfoModel.cs new file mode 100644 index 0000000..ac8025d --- /dev/null +++ b/Confectionery/ConfectioneryDataModels/IMessageInfoModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataModels.Models +{ + public interface IMessageInfoModel + { + string MessageId { get; } + int? ClientId { get; } + string SenderName { get; } + DateTime DateDelivery { get; } + string Subject { get; } + string Body { get; } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Client.cs b/Confectionery/ConfectioneryDatabaseImplement/Client.cs index 5d63231..f5eda43 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Client.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Client.cs @@ -11,63 +11,66 @@ using System.Threading.Tasks; namespace ConfectioneryDatabaseImplement.Models { - public class Client : IClientModel - { - public int Id { get; private set; } + public class Client : IClientModel + { + public int Id { get; private set; } - [Required] - public string ClientFIO { get; set; } = string.Empty; - [Required] - public string Email { get; set; } = string.Empty; - [Required] - public string Password { get; set; } = string.Empty; + [Required] + public string ClientFIO { get; set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; - [ForeignKey("ClientId")] - public virtual List Orders { get; set; } = new(); + [ForeignKey("ClientId")] + public virtual List ClientOrders { get; set; } = new(); - public static Client? Create(ClientBindingModel model) - { - if (model == null) - { - return null; - } - return new Client() - { - Id = model.Id, - ClientFIO = model.ClientFIO, - Email = model.Email, - Password = model.Password - }; - } + [ForeignKey("ClientId")] + public virtual List ClientMessages { get; set; } = new(); - public static Client Create(ClientViewModel model) - { - return new Client - { - Id = model.Id, - ClientFIO = model.ClientFIO, - Email = model.Email, - Password = model.Password - }; - } + public static Client? Create(ClientBindingModel model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } - public void Update(ClientBindingModel model) - { - if (model == null) - { - return; - } - ClientFIO = model.ClientFIO; - Email = model.Email; - Password = model.Password; - } + public static Client Create(ClientViewModel model) + { + return new Client + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } - public ClientViewModel GetViewModel => new() - { - Id = Id, - ClientFIO = ClientFIO, - Email = Email, - Password = Password - }; - } + public void Update(ClientBindingModel model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs index 6a4be8b..6508960 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs @@ -11,74 +11,73 @@ using ConfectioneryDatabaseImplement.Models; namespace ConfectioneryDatabaseImplement.Implements { - public class ClientStorage : IClientStorage - { - public List GetFullList() - { - using var context = new ConfectioneryDatabase(); - return context.Clients.Select(x => x.GetViewModel).ToList(); - } + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); + } - public List GetFilteredList(ClientSearchModel model) - { - if (string.IsNullOrEmpty(model.ClientFIO)) - { - return new(); - } - using var context = new ConfectioneryDatabase(); - return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList(); - } + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList(); + } - public ClientViewModel? GetElement(ClientSearchModel model) - { - if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) - { - return null; - } - using var context = new ConfectioneryDatabase(); - return context.Clients.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Clients.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO == model.ClientFIO) || + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && (string.IsNullOrEmpty(model.Password) || x.Password == model.Password)))?.GetViewModel; + } - public ClientViewModel? Insert(ClientBindingModel model) - { - var newComponent = Client.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new ConfectioneryDatabase(); - context.Clients.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } + public ClientViewModel? Insert(ClientBindingModel model) + { + var newComponent = Client.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Clients.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } - public ClientViewModel? Update(ClientBindingModel model) - { - using var context = new ConfectioneryDatabase(); - var component = context.Clients.FirstOrDefault(x => x.Id == model.Id); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; - } + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var component = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } - public ClientViewModel? Delete(ClientBindingModel model) - { - using var context = new ConfectioneryDatabase(); - var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Clients.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - } + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 80437d2..b5a69f4 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -9,21 +9,22 @@ using Microsoft.EntityFrameworkCore; namespace ConfectioneryDatabaseImplement { - public class ConfectioneryDatabase : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase11;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); - } - public virtual DbSet Components { set; get; } - public virtual DbSet Pastrys { set; get; } - public virtual DbSet PastryComponents { set; get; } - public virtual DbSet Orders { set; get; } - public virtual DbSet Clients { set; get; } + public class ConfectioneryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase7New;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pastrys { set; get; } + public virtual DbSet PastryComponents { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet MessageInfos { set; get; } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/MessageInfo.cs b/Confectionery/ConfectioneryDatabaseImplement/MessageInfo.cs new file mode 100644 index 0000000..926aa49 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/MessageInfo.cs @@ -0,0 +1,64 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.None)] + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + public virtual Client? Client { get; set; } + + [Required] + public string SenderName { get; set; } = string.Empty; + + [Required] + public DateTime DateDelivery { get; set; } + + [Required] + public string Subject { get; set; } = string.Empty; + + [Required] + public string Body { get; set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel? model) + { + if (model == null) + { + return null; + } + return new() + { + MessageId = model.MessageId, + ClientId = model.ClientId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + Body = model.Body, + + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + ClientId = ClientId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body + }; + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/MessageInfoStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..2ca6ba4 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/MessageInfoStorage.cs @@ -0,0 +1,52 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.MessageInfos.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.MessageInfos.Where(x => x.ClientId.HasValue && x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); + } + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.MessageInfos.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.MessageInfos.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Order.cs index 50544dd..9f70fa3 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Order.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Order.cs @@ -76,6 +76,7 @@ namespace ConfectioneryDatabaseImplement.Models Id = Id, ClientId = ClientId, ClientFIO = Client.ClientFIO, + ClientEmail = Client.Email, PastryId = PastryId, PastryName = Pastry.PastryName, ImplementerId = ImplementerId, diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs index d190ed2..992693d 100644 --- a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs +++ b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs @@ -9,60 +9,65 @@ using ConfectioneryDatabaseImplement; namespace ConfectioneryFileImplement { - public class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string ComponentFileName = "Component.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string PastryFileName = "Pastry.xml"; - private readonly string ClientFileName = "Client.xml"; + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string PastryFileName = "Pastry.xml"; + private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageInfoFileName = "MessageInfo.xml"; public List Components { get; private set; } - public List Orders { get; private set; } - public List Pastrys { get; private set; } - public List Clients { get; private set; } + public List Orders { get; private set; } + public List Pastrys { get; private set; } + public List Clients { get; private set; } public List Implementers { get; private set; } + public List Messages { get; private set; } public static DataFileSingleton GetInstance() - { - if (instance == null) - { - instance = new DataFileSingleton(); - } - return instance; - } - public void SaveComponents() => SaveData(Components, ComponentFileName, - "Components", x => x.GetXElement); - public void SavePastrys() => SaveData(Pastrys, PastryFileName, - "Pastrys", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, - "Orders", x => x.GetXElement); - public void SaveClients() => SaveData(Clients, ClientFileName, - "Clients", x => x.GetXElement); - public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, - "Implementers", x => x.GetXElement); + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, + "Components", x => x.GetXElement); + public void SavePastrys() => SaveData(Pastrys, PastryFileName, + "Pastrys", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, + "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, + "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, + "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Orders, ImplementerFileName, + "Messages", x => x.GetXElement); private DataFileSingleton() - { - Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - Pastrys = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Pastrys = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + Messages = LoadData(MessageInfoFileName, "MessageInfo", x => MessageInfo.Create(x)!)!; } - private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) - { - if (File.Exists(filename)) - { - return - XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); - } - return new List(); - } - private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) - { - if (data != null) - { - new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); - } - } - } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } } diff --git a/Confectionery/ConfectioneryFileImplement/MessageInfo.cs b/Confectionery/ConfectioneryFileImplement/MessageInfo.cs new file mode 100644 index 0000000..6889771 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/MessageInfo.cs @@ -0,0 +1,80 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public static MessageInfo? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Body = element.Attribute("Body")!.Value, + Subject = element.Attribute("Subject")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + MessageId = element.Attribute("MessageId")!.Value, + SenderName = element.Attribute("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("Body", Body), + new XAttribute("Subject", Subject), + new XAttribute("ClientId", ClientId), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } +} diff --git a/Confectionery/ConfectioneryFileImplement/MessageInfoStorage.cs b/Confectionery/ConfectioneryFileImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..87ae8e6 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/MessageInfoStorage.cs @@ -0,0 +1,59 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +using ConfectioneryDatabaseImplement; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId != null) + { + return _source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + return _source.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return _source.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; + } + } +} diff --git a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs index daa9f84..2dcce4c 100644 --- a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs +++ b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace ConfectioneryListImplement public List Pastrys { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { Components = new List(); @@ -22,6 +23,7 @@ namespace ConfectioneryListImplement Pastrys = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/Confectionery/ConfectioneryListImplement/MessageInfo.cs b/Confectionery/ConfectioneryListImplement/MessageInfo.cs new file mode 100644 index 0000000..81d7bad --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/MessageInfo.cs @@ -0,0 +1,53 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryListImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + } +} diff --git a/Confectionery/ConfectioneryListImplement/MessageInfoStorage.cs b/Confectionery/ConfectioneryListImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..0d1874b --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/MessageInfoStorage.cs @@ -0,0 +1,68 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + List result = new(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + List result = new(); + foreach (var item in _source.Messages) + { + if (item.ClientId.HasValue && item.ClientId == model.ClientId) + { + result.Add(item.GetViewModel); + } + } + return result; + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + foreach (var message in _source.Messages) + { + if (model.MessageId != null && model.MessageId.Equals(message.MessageId)) + { + return message.GetViewModel; + } + } + return null; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} diff --git a/Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs b/Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs index ca55286..c69c8b0 100644 --- a/Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs +++ b/Confectionery/ConfectioneryRestApi/Controllers/ClientController.cs @@ -3,6 +3,7 @@ using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.ViewModels; using Microsoft.AspNetCore.Mvc; +using System.Net; namespace ConfectioneryRestApi.Controllers { @@ -11,15 +12,15 @@ namespace ConfectioneryRestApi.Controllers public class ClientController : Controller { private readonly ILogger _logger; - private readonly IClientLogic _logic; + private readonly IMessageInfoLogic _mailLogic; - public ClientController(IClientLogic logic, ILogger logger) + public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } - [HttpGet] public ClientViewModel? Login(string login, string password) { @@ -37,7 +38,6 @@ namespace ConfectioneryRestApi.Controllers throw; } } - [HttpPost] public void Register(ClientBindingModel model) { @@ -48,10 +48,9 @@ namespace ConfectioneryRestApi.Controllers catch (Exception ex) { _logger.LogError(ex, "Ошибка регистрации"); - throw; + Response.StatusCode = (int)HttpStatusCode.NotAcceptable; } } - [HttpPost] public void UpdateData(ClientBindingModel model) { @@ -65,5 +64,23 @@ namespace ConfectioneryRestApi.Controllers throw; } } + + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } + } } diff --git a/Confectionery/ConfectioneryRestApi/Program.cs b/Confectionery/ConfectioneryRestApi/Program.cs index d785dee..ee9d801 100644 --- a/Confectionery/ConfectioneryRestApi/Program.cs +++ b/Confectionery/ConfectioneryRestApi/Program.cs @@ -1,4 +1,5 @@ using ConfectioneryBusinessLogic; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; using ConfectioneryDatabaseImplement.Implements; @@ -17,25 +18,40 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddSingleton(); + builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at -https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { - c.SwaggerDoc("v1", new OpenApiInfo - { - Title = "ConfectioneryRestApi", - Version = "v1" - }); + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "ConfectioneryRestApi", + Version = "v1" + }); }); var app = builder.Build(); +var mailSender = app.Services.GetService(); +mailSender?.MailConfig(new MailConfigBindingModel +{ + MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty, + MailPassword = builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? string.Empty, + SmtpClientHost = builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? string.Empty, + SmtpClientPort = Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()), + PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? string.Empty, + PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString()) +}); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ConfectioneryRestApi v1")); } app.UseHttpsRedirection(); diff --git a/Confectionery/ConfectioneryRestApi/appsettings.json b/Confectionery/ConfectioneryRestApi/appsettings.json index 10f68b8..461ebb9 100644 --- a/Confectionery/ConfectioneryRestApi/appsettings.json +++ b/Confectionery/ConfectioneryRestApi/appsettings.json @@ -5,5 +5,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "labworkalimova@gmail.com", + "MailPassword": "mrwi jpal mwmk gwgl" } diff --git a/Confectionery/ConfectioneryView/App.config b/Confectionery/ConfectioneryView/App.config new file mode 100644 index 0000000..f3ceef9 --- /dev/null +++ b/Confectionery/ConfectioneryView/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + +