From 4b880a78c700123b672c489ec24c217e62def5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Sat, 18 May 2024 16:05:37 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=20=D0=BF=D0=BE=D1=87=D0=B8=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D1=81=D1=8C=20:(?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/OrderViewModel.cs | 11 +++ .../BusinessLogics/MessageInfoLogic.cs | 66 +++++++++++++---- .../BusinessLogics/OrderLogic.cs | 70 ++++++------------- .../Models/Order.cs | 1 + Confectionery/ConfectioneryView/FormMain.cs | 1 + 5 files changed, 87 insertions(+), 62 deletions(-) diff --git a/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs b/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs index 2a2ef0c..05e373b 100644 --- a/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs +++ b/Confectionery/ConfectionaryContracts/ViewModels/OrderViewModel.cs @@ -13,25 +13,36 @@ namespace ConfectioneryContracts.ViewModels { [DisplayName("Номер")] public int Id { get; set; } + public int PastryId { get; set; } + [DisplayName("Выпечка")] public string PastryName { get; set; } = string.Empty; + public int ClientId { get; set; } [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; } = string.Empty; + [DisplayName("Количество")] public int Count { get; set; } + [DisplayName("Сумма")] public double Sum { get; set; } + [DisplayName("Статус")] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [DisplayName("Дата создания")] public DateTime DateCreate { get; set; } = DateTime.Now; + [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } } diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs index 475e02c..92f9494 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -15,39 +15,81 @@ namespace ConfectioneryBusinessLogic.BusinessLogics public class MessageInfoLogic : IMessageInfoLogic { private readonly ILogger _logger; - private readonly IMessageInfoStorage _messageStorage; - public MessageInfoLogic(ILogger logger, IMessageInfoStorage messageStorage) + + private readonly IMessageInfoStorage _messageInfoStorage; + + private readonly IClientStorage _clientStorage; + + public MessageInfoLogic(ILogger logger, IMessageInfoStorage messageInfoStorage, IClientStorage clientStorage) { _logger = logger; - _messageStorage = messageStorage; + _messageInfoStorage = messageInfoStorage; + _clientStorage = clientStorage; } + public List? ReadList(MessageInfoSearchModel? model) { _logger.LogInformation("ReadList. MessageId: {MessageId}. ClientId: {ClientId}", model?.MessageId, model?.ClientId); - - var list = model == null ? _messageStorage.GetFullList() : _messageStorage.GetFilteredList(model); + 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) { - if (model == null) - { - return false; - } - - if (_messageStorage.Insert(model) == null) + 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}. DateDelivery: {DateDelivery} Subject: {Subject}. Body: {Body}", model.MessageId, model.SenderName, model.DateDelivery, 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/BusinessLogics/OrderLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs index ff655a9..db06296 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -20,18 +20,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics private readonly IOrderStorage _orderStorage; - private readonly IClientStorage _clientStorage; - - private readonly AbstractMailWorker _mailLogic; + private readonly AbstractMailWorker _mailWorker; static readonly object locker = new(); - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IClientStorage clientStorage, AbstractMailWorker mailLogic) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker) { _logger = logger; _orderStorage = orderStorage; - _clientStorage = clientStorage; - _mailLogic = mailLogic; + _mailWorker = mailWorker; } public bool CreateOrder(OrderBindingModel model) @@ -43,15 +40,18 @@ namespace ConfectioneryBusinessLogic.BusinessLogics return false; } model.Status = OrderStatus.Принят; - - var order = _orderStorage.Insert(model); - if (order == null) + var element = _orderStorage.Insert(model); + if (element == null) { _logger.LogWarning("Insert operation failed"); return false; } - - SendEmail(order); + Task.Run(() => _mailWorker.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = element.ClientEmail, + Subject = $"Новый заказ создан. Номер заказа - {element.Id}", + Text = $"Заказ №{element.Id} от {element.DateCreate} на сумму {element.Sum} принят." + })); return true; } @@ -67,6 +67,7 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } + public OrderViewModel? ReadElement(OrderSearchModel model) { if (model == null) @@ -160,50 +161,19 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { model.DateImplement = order.DateImplement; } - - order = _orderStorage.Update(model); - if (order == null) + if (_orderStorage.Update(model) == null) { _logger.LogWarning("Change status operation failed"); return false; } - - SendEmail(order); + 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; } - public void SendEmail(OrderViewModel order) - { - if (order == null) - { - return; - } - - var client = _clientStorage.GetElement(new ClientSearchModel { Id = order.ClientId }); - if (client == null) - { - return; - } - - MailSendInfoBindingModel mailModel; - if (order.Status == OrderStatus.Выполняется) - { - mailModel = new MailSendInfoBindingModel - { - MailAddress = client.Email, - Subject = $"Order №{order.Id}", - Text = $"Your order №{order.Id} by {order.DateCreate} on {order.Sum} was accepted!" - }; - } - else - { - mailModel = new MailSendInfoBindingModel - { - MailAddress = client.Email, - Subject = $"Order №{order.Id}", - Text = $"Order №{order.Id} status was changed to {order.Status}" - }; - } - _mailLogic.MailSendAsync(mailModel); - } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs index 49d14a4..43ad75f 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs @@ -70,6 +70,7 @@ namespace ConfectioneryDatabaseImplement.Models DateImplement = DateImplement, PastryName = Pastry.PastryName, ClientFIO = Client.ClientFIO, + ClientEmail = Client.Email ?? string.Empty, ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty }; } diff --git a/Confectionery/ConfectioneryView/FormMain.cs b/Confectionery/ConfectioneryView/FormMain.cs index eceb563..e44836d 100644 --- a/Confectionery/ConfectioneryView/FormMain.cs +++ b/Confectionery/ConfectioneryView/FormMain.cs @@ -44,6 +44,7 @@ namespace ConfectioneryView dataGridView.Columns["PastryId"].Visible = false; dataGridView.Columns["PastryName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["ClientId"].Visible = false; + dataGridView.Columns["ClientEmail"].Visible = false; dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["ImplementerId"].Visible = false; dataGridView.Columns["ImplementerFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;