95 lines
2.7 KiB
C#
95 lines
2.7 KiB
C#
using GiftShopContracts.BindingModels;
|
||
using GiftShopContracts.BusinessLogicsContracts;
|
||
using GiftShopContracts.SearchModels;
|
||
using GiftShopContracts.StoragesContracts;
|
||
using GiftShopContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace GiftShopBusinessLogic.BusinessLogics
|
||
{
|
||
public class MessageInfoLogic : IMessageInfoLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IClientStorage _clientStorage;
|
||
private readonly IMessageInfoStorage _messageStorage;
|
||
|
||
public MessageInfoLogic(ILogger<MessageInfoLogic> logger, IMessageInfoStorage messageStorage, IClientStorage clientStorage)
|
||
{
|
||
_logger = logger;
|
||
_messageStorage = messageStorage;
|
||
_clientStorage = clientStorage;
|
||
}
|
||
|
||
public bool Create(MessageInfoBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_messageStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. MessageId:{MessageId}.ClientId:{ClientId} ", model?.MessageId, model?.ClientId);
|
||
var list = model == null ? _messageStorage.GetFullList() : _messageStorage.GetFilteredList(model);
|
||
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
|
||
return null;
|
||
}
|
||
|
||
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
||
|
||
return list;
|
||
}
|
||
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:{Email}", model.SenderName);
|
||
}
|
||
else
|
||
{
|
||
model.ClientId = element.Id;
|
||
}
|
||
}
|
||
}
|
||
}
|