PIbd21_Makarov_DV_FlowerShop/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs
2024-05-02 19:55:52 +04:00

85 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FlowerShopContracts.BindingModels;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace FlowerShopBusinessLogic.BusinessLogics
{
public class MessageInfoLogic : IMessageInfoLogic
{
private readonly ILogger _logger;
private readonly IMessageInfoStorage _messageInfoStorage;
private readonly IClientStorage _clientStorage;
public MessageInfoLogic(ILogger<MessageInfoLogic> logger, IMessageInfoStorage messageInfoStorage, IClientStorage clientStorage)
{
_logger = logger;
_messageInfoStorage = messageInfoStorage;
_clientStorage = clientStorage;
}
public List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model)
{
_logger.LogDebug("ReadList. MessageId: {MessageId}, ClientId: {ClientId}", model?.MessageId, model?.ClientId);
var result = model == null ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model);
_logger.LogDebug("ReadList result. Count: {Count}", result.Count);
return result;
}
public bool Create(MessageInfoBindingModel model)
{
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}. Subject: {Subject}. Body: {Body}",
model.MessageId, model.SenderName, model.Subject, model.Body);
var Element = _clientStorage.GetElement(new ClientSearchModel
{
Email = model.SenderName
});
if (Element == null)
{
_logger.LogWarning("Не удалось найти клиента, отправившего письмо с адреса {Email}", model.SenderName);
}
else
{
model.ClientId = Element.Id;
}
}
}
}