using AccountingWarehouseProductsContracts.BindingModels; using AccountingWarehouseProductsContracts.BusinessLogicsContracts; using AccountingWarehouseProductsContracts.SearchModels; using AccountingWarehouseProductsContracts.StoragesContracts; using AccountingWarehouseProductsContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AccountingWarehouseProductsBusinessLogic.BusinessLogic { public class ProductLogic : IProductLogic { private readonly IProductStorage _productStorage; public ProductLogic(IProductStorage buyerStorage) { _productStorage = buyerStorage; } public ProductViewModel? ReadElement(ProductSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _productStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(ProductSearchModel? model) { var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Create(ProductBindingModel model) { CheckModel(model); if (_productStorage.Insert(model) == null) { return false; } return true; } public bool Delete(ProductBindingModel model) { CheckModel(model, false); if (_productStorage.Delete(model) == null) { return false; } return true; } public bool Update(ProductBindingModel model) { CheckModel(model); if (_productStorage.Update(model) == null) { return false; } return true; } private void CheckModel(ProductBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.ProductName)) { throw new ArgumentNullException("Нет названия", nameof(model.ProductName)); } } } }