From 680de6f1910626de56700a7f975c48e7ab6d0beb 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: Mon, 6 May 2024 15:50:03 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=207=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5=20=D0=B5=D1=89=D0=B5=D1=89=D0=B5=20=D0=BD=D0=B5=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 55 ++++++++++++++++++- .../Models/Client.cs | 2 + .../ConfectioneryView.csproj | 5 ++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs index 558fd7a..95070e5 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -10,6 +10,7 @@ using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; using Microsoft.Extensions.Logging; using ConfectioneryDataModels.Enums; +using ConfectioneryBusinessLogic.MailWorker; namespace ConfectioneryBusinessLogic.BusinessLogics { @@ -19,12 +20,18 @@ namespace ConfectioneryBusinessLogic.BusinessLogics private readonly IOrderStorage _orderStorage; + private readonly IClientStorage _clientStorage; + + private readonly AbstractMailWorker _mailLogic; + static readonly object locker = new(); - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IClientStorage clientStorage, AbstractMailWorker mailLogic) { _logger = logger; _orderStorage = orderStorage; + _clientStorage = clientStorage; + _mailLogic = mailLogic; } public bool CreateOrder(OrderBindingModel model) @@ -36,11 +43,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics return false; } model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) + + var order = _orderStorage.Insert(model); + if (order == null) { _logger.LogWarning("Insert operation failed"); return false; } + + SendEmail(order); return true; } @@ -149,12 +160,50 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { model.DateImplement = order.DateImplement; } - if (_orderStorage.Update(model) == null) + + order = _orderStorage.Update(model); + if (order == null) { _logger.LogWarning("Change status operation failed"); return false; } + + SendEmail(order); 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/Client.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs index 6ddd8b2..7d24a30 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs @@ -22,6 +22,8 @@ namespace ConfectioneryDatabaseImplement.Models public string Password { get; private set; } = string.Empty; [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("ClientId")] + public virtual List MessagesInfo { get; set; } = new(); public static Client? Create(ClientBindingModel model) { if (model == null) diff --git a/Confectionery/ConfectioneryView/ConfectioneryView.csproj b/Confectionery/ConfectioneryView/ConfectioneryView.csproj index 1362fea..a51a89e 100644 --- a/Confectionery/ConfectioneryView/ConfectioneryView.csproj +++ b/Confectionery/ConfectioneryView/ConfectioneryView.csproj @@ -36,4 +36,9 @@ + + + Always + + \ No newline at end of file