2024-05-25 15:04:18 +04:00
|
|
|
|
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;
|
|
|
|
|
|
2024-05-26 17:47:24 +04:00
|
|
|
|
public CostItemLogic(ILogger<CostItemLogic> logger, ICostItemStorage storage) {
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_storage = storage;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:04:18 +04:00
|
|
|
|
public bool Create(CostItemBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_storage.Insert(model) == null)
|
|
|
|
|
{
|
2024-05-26 17:47:24 +04:00
|
|
|
|
_logger.LogWarning("Create operation failed");
|
2024-05-25 15:04:18 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-05-26 17:47:24 +04:00
|
|
|
|
throw new ArgumentNullException("Затраты должны быть больше 0", nameof(model.Price));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name)) {
|
|
|
|
|
throw new ArgumentNullException("Отсутствует название статьи затрат", nameof(model.Price));
|
2024-05-25 15:04:18 +04:00
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}");
|
|
|
|
|
var element = _storage.GetElement(new CostItemSearchModel { ID = model.ID });
|
2024-05-25 17:47:14 +04:00
|
|
|
|
if (element != null && element.ID != model.EmployeeID)
|
2024-05-25 15:04:18 +04:00
|
|
|
|
{
|
2024-05-26 17:47:24 +04:00
|
|
|
|
throw new InvalidOperationException("Такая статья затрат уде есть");
|
2024-05-25 15:04:18 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|