CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/CostItemLogic.cs
Илья Федотов e78b0ffdbe Product create
2024-05-28 12:50:24 +04:00

111 lines
3.9 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 CostItemLogic(ILogger<CostItemLogic> logger, ICostItemStorage storage) {
_logger = logger;
_storage = storage;
}
public bool Create(CostItemBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Create 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 (!withParams)
{
return;
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Затраты должны быть больше 0", nameof(model.Price));
}
if (string.IsNullOrEmpty(model.Name)) {
throw new ArgumentNullException("Отсутствует название статьи затрат", nameof(model.Price));
}
if (model.CostNum <= 0) {
throw new ArgumentOutOfRangeException("Номер счета должен идти от 1");
}
_logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}" +
$"CostNum:{model.CostNum}");
var element = _storage.GetElement(new CostItemSearchModel { Name = model.Name });
if (element != null && element.Name == model.Name)
{
throw new InvalidOperationException("Такая статья затрат уде есть");
}
}
}
}