OrderLogic edited: added sending msgs

This commit is contained in:
VictoriaPresnyakova 2023-04-30 19:27:47 +04:00
parent 53b8b71f89
commit e61380ff6b

View File

@ -1,4 +1,5 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreBusinessLogic.MailWorker;
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.BusinessLogicsContracts;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
@ -17,12 +18,16 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
private readonly AbstractMailWorker _mailWorker;
private readonly IClientLogic _clientLogic;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic)
{
_logger = logger;
_orderStorage = orderStorage;
}
_mailWorker = mailWorker;
_clientLogic = clientLogic;
}
public bool CreateOrder(OrderBindingModel model)
{
@ -35,15 +40,16 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
}
model.Status = OrderStatus.Принят;
var result = _orderStorage.Update(model);
if (_orderStorage.Insert(model) == null)
{
if (result == null)
{
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
SendOrderMessage(result.ClientId, $"Установка ПО, Заказ №{result.Id}", $"Заказ №{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят");
return true;
}
public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus)
@ -75,18 +81,38 @@ namespace JewelryStoreBusinessLogic.BusinessLogics
model.JewelId = vmodel.JewelId;
model.Sum = vmodel.Sum;
model.Count = vmodel.Count;
var result = _orderStorage.Update(model);
if (_orderStorage.Update(model) == null)
if (result == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
SendOrderMessage(result.ClientId, $"Установка ПО, Заказ №{result.Id}", $"Заказ №{model.Id} изменен статус на {result.Status}");
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
private bool SendOrderMessage(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;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выполняется);
}