From abd33d7f4973aa3617b668240e627a9f1081e6d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9F=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BF=D0=BE=D0=B2?= Date: Wed, 20 Nov 2024 01:02:13 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20?= =?UTF-8?q?=D0=B4=D0=BE=D1=8F=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StorageContracts/IProductStorage.cs | 2 +- .../InternetShopLogics/Logics/ProductLogic.cs | 67 +++++++++++++++++-- 2 files changed, 63 insertions(+), 6 deletions(-) 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("Товар с таким названием уже есть"); + } } }