лаба 7 все ещеще не работает

This commit is contained in:
Полина Чубыкина 2024-05-06 15:50:03 +04:00
parent 164ec5ca90
commit 680de6f191
3 changed files with 59 additions and 3 deletions

View File

@ -10,6 +10,7 @@ using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.StoragesContracts; using ConfectioneryContracts.StoragesContracts;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using ConfectioneryDataModels.Enums; using ConfectioneryDataModels.Enums;
using ConfectioneryBusinessLogic.MailWorker;
namespace ConfectioneryBusinessLogic.BusinessLogics namespace ConfectioneryBusinessLogic.BusinessLogics
{ {
@ -19,12 +20,18 @@ namespace ConfectioneryBusinessLogic.BusinessLogics
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
private readonly IClientStorage _clientStorage;
private readonly AbstractMailWorker _mailLogic;
static readonly object locker = new(); static readonly object locker = new();
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IClientStorage clientStorage, AbstractMailWorker mailLogic)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
_clientStorage = clientStorage;
_mailLogic = mailLogic;
} }
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
@ -36,11 +43,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics
return false; return false;
} }
model.Status = OrderStatus.Принят; model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
var order = _orderStorage.Insert(model);
if (order == null)
{ {
_logger.LogWarning("Insert operation failed"); _logger.LogWarning("Insert operation failed");
return false; return false;
} }
SendEmail(order);
return true; return true;
} }
@ -149,12 +160,50 @@ namespace ConfectioneryBusinessLogic.BusinessLogics
{ {
model.DateImplement = order.DateImplement; model.DateImplement = order.DateImplement;
} }
if (_orderStorage.Update(model) == null)
order = _orderStorage.Update(model);
if (order == null)
{ {
_logger.LogWarning("Change status operation failed"); _logger.LogWarning("Change status operation failed");
return false; return false;
} }
SendEmail(order);
return true; 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);
}
} }
} }

View File

@ -22,6 +22,8 @@ namespace ConfectioneryDatabaseImplement.Models
public string Password { get; private set; } = string.Empty; public string Password { get; private set; } = string.Empty;
[ForeignKey("ClientId")] [ForeignKey("ClientId")]
public virtual List<Order> Orders { get; set; } = new(); public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("ClientId")]
public virtual List<MessageInfo> MessagesInfo { get; set; } = new();
public static Client? Create(ClientBindingModel model) public static Client? Create(ClientBindingModel model)
{ {
if (model == null) if (model == null)

View File

@ -36,4 +36,9 @@
<ProjectReference Include="..\ConfectioneryListImplement\ConfectioneryListImplement.csproj" /> <ProjectReference Include="..\ConfectioneryListImplement\ConfectioneryListImplement.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="App.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>