diff --git a/InternetShop/InternetShopContracts/StorageContracts/IProductStorage.cs b/InternetShop/InternetShopContracts/StorageContracts/IProductStorage.cs index 6750516..3ba8ad0 100644 --- a/InternetShop/InternetShopContracts/StorageContracts/IProductStorage.cs +++ b/InternetShop/InternetShopContracts/StorageContracts/IProductStorage.cs @@ -11,6 +11,6 @@ namespace InternetShopContracts.StorageContracts ProductViewModel? GetElement(ProductSearchModel model); ProductViewModel? Insert(ProductBindingModel model); ProductViewModel? Update(ProductBindingModel model); - ProductViewModel? Delete(OrderBindingModel model); + ProductViewModel? Delete(ProductBindingModel model); } } diff --git a/InternetShop/InternetShopLogics/Logics/ProductLogic.cs b/InternetShop/InternetShopLogics/Logics/ProductLogic.cs index 6bdaf2d..4c0bbcf 100644 --- a/InternetShop/InternetShopLogics/Logics/ProductLogic.cs +++ b/InternetShop/InternetShopLogics/Logics/ProductLogic.cs @@ -2,34 +2,91 @@ using InternetShopContracts.DataSearchModels; using InternetShopContracts.DataViewModels; using InternetShopContracts.LogicsContracts; +using InternetShopContracts.StorageContracts; namespace InternetShopLogics.Logics { public class ProductLogic : IProductLogic { + private IProductStorage _productStorage; + + public ProductLogic(IProductStorage productStorage) + { + _productStorage = productStorage; + } + public bool Create(ProductBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + if (_productStorage.Insert(model) == null) + { + return false; + } + return true; } public bool Delete(ProductBindingModel model) { - throw new NotImplementedException(); + CheckModel(model, false); + if (_productStorage.Delete(model) == null) + { + return false; + } + return true; } public ProductViewModel? ReadElement(ProductSearchModel model) { - throw new NotImplementedException(); + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + return _productStorage.GetElement(model); } public List ReadList(ProductSearchModel? model = null) { - throw new NotImplementedException(); + List? list = null; + if (model == null) + { + list = _productStorage.GetFullList(); + } + else + { + list = _productStorage.GetFilteredList(model); + } + if (list == null) + { + return new List(); + } + return list; } public bool Update(ProductBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + if (_productStorage.Update(model) == null) + { + return false; + } + return true; } + + private void CheckModel(ProductBindingModel model, bool checkParams = true) + { + if (model == null) throw new ArgumentNullException(nameof(model)); + if (!checkParams) return; + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия товара", nameof(model.Name)); + } + var item = _productStorage.GetElement(new ProductSearchModel + { + Name = model.Name + }); + if (item != null && item.Id != model.Id) + { + throw new InvalidOperationException("Товар с таким названием уже есть"); + } } }