103 lines
3.4 KiB
C#
103 lines
3.4 KiB
C#
using ElectronicsShopContracts.BindingModels;
|
||
using ElectronicsShopContracts.BusinessLogicContracts;
|
||
using ElectronicsShopContracts.SearchModels;
|
||
using ElectronicsShopContracts.StorageContracts;
|
||
using ElectronicsShopContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
|
||
namespace ElectronicsShopBusinessLogic.BusinessLogic
|
||
{
|
||
public class CostItemLogic : ICostItemLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly ICostItemStorage _storage;
|
||
|
||
public bool Create(CostItemBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_storage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Add operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(CostItemBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation($"Delete.ID:{model.ID}");
|
||
if (_storage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public CostItemViewModel? ReadElement(CostItemSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation($"ReadElement.Name:{model.Name}.ID:{model.ID}");
|
||
var element = _storage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement. element not fount");
|
||
return null;
|
||
}
|
||
_logger.LogInformation($"ReadElement.find.ID:{element.ID}");
|
||
return element;
|
||
}
|
||
|
||
public List<CostItemViewModel>? ReadList(CostItemSearchModel model)
|
||
{
|
||
_logger.LogInformation($"ReadList. ClientID:{model?.ID}");
|
||
var list = model == null ? _storage.GetFullList() : _storage.GetFillteredList(model); ;
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList. return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation($"ReadList.Count:{list.Count}");
|
||
return list;
|
||
}
|
||
|
||
public bool Update(CostItemBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_storage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(CostItemBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (model.Price <= 0)
|
||
{
|
||
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
|
||
}
|
||
_logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}");
|
||
var element = _storage.GetElement(new CostItemSearchModel { ID = model.ID });
|
||
if (element != null && element.ID != model.EmployeeID)
|
||
{
|
||
throw new InvalidOperationException("Продукт с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|