From 3e2d8d74a5f3426c1ce3d1d698cb4ef0b0713509 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Sun, 28 Apr 2024 20:59:05 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20=D0=BF=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D1=83=D0=BA=D1=82=D0=B0=20=D0=B8=20=D0=BA=D0=BE=D0=BC=D0=BF?= =?UTF-8?q?=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/ComponentLogic.cs | 113 +++++++++++++++++ .../BusinessLogic/ProductLogic.cs | 115 ++++++++++++++++++ .../ComputerHardwareStoreBusinessLogic.csproj | 4 + .../SearchModels/ComponentSearchModel.cs | 2 +- .../SearchModels/ProductSearchModel.cs | 2 +- 5 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ComponentLogic.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ProductLogic.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ComponentLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ComponentLogic.cs new file mode 100644 index 0000000..a0ebdf7 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ComponentLogic.cs @@ -0,0 +1,113 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + + public List? ReadList(ComponentSearchModel? model) + { + _logger.LogInformation("ReadList Component. Name:{Name}. Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : + _componentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList Component return null list"); + return null; + } + _logger.LogInformation("ReadList Component. Count:{Count}", list.Count); + return list; + } + + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement Component. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _componentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement Component element not found"); + return null; + } + _logger.LogInformation("ReadElement Component find. Id:{Id}", element.Id); + return element; + } + + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert Component operation failed"); + return false; + } + return true; + } + + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update Component operation failed"); + return false; + } + return true; + } + + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete Component operation failed"); + return false; + } + return true; + } + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия компонента", nameof(model.Name)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + var element = _componentStorage.GetElement(new ComponentSearchModel + { + Name = model.Name + }); + if (element != null) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ProductLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ProductLogic.cs new file mode 100644 index 0000000..8dec42b --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/ProductLogic.cs @@ -0,0 +1,115 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class ProductLogic : IProductLogic + { + private readonly ILogger _logger; + private readonly IProductStorage _productStorage; + + public ProductLogic(ILogger logger, IProductStorage productStorage) + { + _logger = logger; + _productStorage = productStorage; + } + + public List? ReadList(ProductSearchModel? model) + { + _logger.LogInformation("ReadList Product. Name:{Name}. Id:{ Id}", model?.Name, model?.Id); + var list = model == null ? + _productStorage.GetFullList() : + _productStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList Product return null list"); + return null; + } + _logger.LogInformation("ReadList Product. Count:{Count}", list.Count); + return list; + } + + public ProductViewModel? ReadElement(ProductSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement Product. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _productStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement Product element not found"); + return null; + } + _logger.LogInformation("ReadElement Product find. Id:{Id}", element.Id); + return element; + } + + public bool Create(ProductBindingModel model) + { + CheckModel(model); + if (_productStorage.Insert(model) == null) + { + _logger.LogWarning("Insert Product operation failed"); + return false; + } + return true; + } + + public bool Update(ProductBindingModel model) + { + CheckModel(model); + if (_productStorage.Update(model) == null) + { + _logger.LogWarning("Update Product Component operation failed"); + return false; + } + return true; + } + + public bool Delete(ProductBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_productStorage.Delete(model) == null) + { + _logger.LogWarning("Delete Product operation failed"); + 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.Name)) + { + throw new ArgumentException("Нет названия продукта", nameof(model.Name)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price)); + } + var element = _productStorage.GetElement(new ProductSearchModel + { + Name = model.Name + }); + if (element != null) + { + throw new InvalidOperationException("Продукт с таким названием уже есть"); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj index 9e09697..47a15a0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ComponentSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ComponentSearchModel.cs index 08a0f78..560a31d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ComponentSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ComponentSearchModel.cs @@ -3,6 +3,6 @@ public class ComponentSearchModel { public int? Id { get; set; } - public string? ComponentName { get; set; } + public string? Name { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 3a42376..7944de9 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -3,6 +3,6 @@ public class ProductSearchModel { public int? Id { get; set; } - public string? ProductName { get; set; } + public string? Name { get; set; } } }