diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index 2548654..2a8f463 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -1,4 +1,5 @@ -using ConfectioneryContracts.BindingModels; +using ConfectioneryBusinessLogic.MailWorker; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; @@ -14,13 +15,17 @@ namespace ConfectioneryBusinessLogic.BusinessLogics private readonly IOrderStorage _orderStorage; private readonly IPastryStorage _pastryStorage; private readonly IShopLogic _shopLogic; + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic, IClientLogic clientLogic, AbstractMailWorker mailWorker) { _logger = logger; _shopLogic = shopLogic; _pastryStorage = pastryStorage; _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; } public bool CreateOrder(OrderBindingModel model) @@ -33,11 +38,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics } model.Status = OrderStatus.Принят; model.DateCreate = DateTime.Now; - if (_orderStorage.Insert(model) == null) + var result = _orderStorage.Insert(model); + if (result == null) { _logger.LogWarning("Insert operation failed"); return false; } + SendOrderStatusMail(result.ClientId, $"Новый заказ создан. Номер заказа #{result.Id}", $"Заказ #{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят"); return true; } @@ -122,11 +129,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics model.PastryId = vmodel.PastryId; model.Sum = vmodel.Sum; model.Count= vmodel.Count; - if (_orderStorage.Update(model) == null) + var result = _orderStorage.Update(model); + if (result == null) { _logger.LogWarning("Update operation failed"); return false; } + SendOrderStatusMail(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{model.Id} изменен статус на {result.Status}"); return true; } @@ -146,5 +155,21 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } + + private bool SendOrderStatusMail(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + if (client == null) + { + return false; + } + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + return true; + } } }