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 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? 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)); } _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("Такая статья затрат уде есть"); } } } }