PIbd-23_Sergunov_M.A._GiftShop/GiftShop/GiftShopBusinessLogic/BusinessLogics/GiftLogic.cs

138 lines
3.9 KiB
C#
Raw Normal View History

2023-03-31 19:55:55 +04:00
using GiftShopContracts.BindingModels;
using GiftShopContracts.BusinessLogicsContracts;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace GiftShopBusinessLogic.BusinessLogics
{
public class GiftLogic : IGiftLogic
{
private readonly ILogger _logger;
private readonly IGiftStorage _giftStorage;
public GiftLogic(ILogger<GiftLogic> logger, IGiftStorage giftStorage)
{
_logger = logger;
_giftStorage = giftStorage;
}
public bool Create(GiftBindingModel model)
{
CheckModel(model);
if (_giftStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(GiftBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_giftStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public GiftViewModel? ReadElement(GiftSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. GiftName:{GiftName}.Id:{ Id}", model.GiftName, model.Id);
var element = _giftStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<GiftViewModel>? ReadList(GiftSearchModel? model)
{
_logger.LogInformation("ReadList. GiftName:{GiftName}.Id:{ Id}", model?.GiftName, model?.Id);
var list = model == null ? _giftStorage.GetFullList() : _giftStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(GiftBindingModel model)
{
CheckModel(model);
if (_giftStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(GiftBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.GiftName))
{
throw new ArgumentNullException("Нет названия изделия", nameof(model.GiftName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Gift. GiftName:{GiftName}.Price:{ Cost}. Id: { Id}", model.GiftName, model.Price, model.Id);
var element = _giftStorage.GetElement(new GiftSearchModel
{
GiftName = model.GiftName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}