ComputerHardwareStore_YouAr.../ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/CommentLogic.cs

102 lines
3.4 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 ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
public class CommentLogic : ICommentLogic
{
private readonly ILogger _logger;
private readonly ICommentStorage _commentStorage;
public CommentLogic(ILogger<CommentLogic> logger, ICommentStorage commentStorage)
{
_logger = logger;
_commentStorage = commentStorage;
}
public List<CommentViewModel>? ReadList(CommentSearchModel? model)
{
_logger.LogInformation("ReadList Comment. Date:{Date}. Id:{Id}", model?.Date, model?.Id);
var list = model == null ? _commentStorage.GetFullList() :
_commentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList Comment return null list");
return null;
}
_logger.LogInformation("ReadList Comment. Count:{Count}", list.Count);
return list;
}
public CommentViewModel? ReadElement(CommentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement Comment. Date:{Date}. Id:{ Id}", model.Date, model.Id);
var element = _commentStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement Comment element not found");
return null;
}
_logger.LogInformation("ReadElement Comment find. Id:{Id}", element.Id);
return element;
}
public bool Create(CommentBindingModel model)
{
CheckModel(model);
if (_commentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert Comment operation failed");
return false;
}
return true;
}
public bool Update(CommentBindingModel model)
{
CheckModel(model);
if (_commentStorage.Update(model) == null)
{
_logger.LogWarning("Update Comment operation failed");
return false;
}
return true;
}
public bool Delete(CommentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_commentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete Comment operation failed");
return false;
}
return true;
}
private void CheckModel(CommentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Date))
{
throw new ArgumentNullException("Нет даты у комментария", nameof(model.Date));
}
}
}
}