From c48da815bb77cb5bb8c545d0963ad9c76393bdaa Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Wed, 1 May 2024 13:38:47 +0400 Subject: [PATCH] Fix Name property in search models (because it's unique) --- .../BusinessLogics/ComponentLogic.cs | 114 ++++++++++++++++++ .../SearchModels/AssemblySearchModel.cs | 2 + .../SearchModels/ComponentSearchModel.cs | 2 + .../SearchModels/ProductSearchModel.cs | 2 + .../Implements/AssemblyStorage.cs | 10 ++ .../Implements/ComponentStorage.cs | 8 ++ .../Implements/ProductStorage.cs | 11 ++ 7 files changed, 149 insertions(+) create mode 100644 ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs diff --git a/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..c0b094c --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,114 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + 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) + { + var List = (Model == null) ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(Model); + + if (List == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", List.Count); + return List; + } + + public ComponentViewModel? ReadElement(ComponentSearchModel Model) + { + if (Model == null) + throw new ArgumentNullException(nameof(Model)); + + var Element = _componentStorage.GetElement(Model); + if (Element == null) + { + _logger.LogWarning("ReadElement component not found"); + return null; + } + + _logger.LogInformation("ReadElement component found. Id: {Id}", Element.Id); + return Element; + } + + public bool Create(ComponentBindingModel Model) + { + CheckModel(Model); + + if (_componentStorage.Insert(Model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool Update(ComponentBindingModel Model) + { + CheckModel(Model); + + if (_componentStorage.Update(Model) == null) + { + _logger.LogWarning("Update 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) is null) + { + _logger.LogWarning("Delete 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.ComponentName)) + throw new ArgumentException($"У комплектующей отсутствует название"); + + if (Model.Cost <= 0) + throw new ArgumentException("Цена комплектующей должна быть больше 0", nameof(Model.Cost)); + + var Element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = Model.ComponentName + }); + + if (Element != null && Element.Id != Model.Id) + throw new InvalidOperationException("Комплектующая с таким названием уже есть"); + } + } +} diff --git a/ComputerShopContracts/SearchModels/AssemblySearchModel.cs b/ComputerShopContracts/SearchModels/AssemblySearchModel.cs index c15871b..9fe6f40 100644 --- a/ComputerShopContracts/SearchModels/AssemblySearchModel.cs +++ b/ComputerShopContracts/SearchModels/AssemblySearchModel.cs @@ -6,6 +6,8 @@ public int? UserId { get; set; } + public string? AssemblyName { get; set; } + public string? Category { get; set; } } } diff --git a/ComputerShopContracts/SearchModels/ComponentSearchModel.cs b/ComputerShopContracts/SearchModels/ComponentSearchModel.cs index 3a4bb83..8ee445c 100644 --- a/ComputerShopContracts/SearchModels/ComponentSearchModel.cs +++ b/ComputerShopContracts/SearchModels/ComponentSearchModel.cs @@ -5,5 +5,7 @@ public int? Id { get; set; } public int? UserId { get; set; } + + public string? ComponentName { get; set; } } } diff --git a/ComputerShopContracts/SearchModels/ProductSearchModel.cs b/ComputerShopContracts/SearchModels/ProductSearchModel.cs index 60e81eb..4c7065a 100644 --- a/ComputerShopContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerShopContracts/SearchModels/ProductSearchModel.cs @@ -7,5 +7,7 @@ public int? UserId { get; set; } public int? ShipmentId { get; set; } + + public string? ProductName { get; set; } } } diff --git a/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs b/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs index 213ac4a..4fd4cde 100644 --- a/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs @@ -47,6 +47,16 @@ namespace ComputerShopDatabaseImplement.Implements { using var Context = new ComputerShopDatabase(); + // AssemblyName is unique + if (!string.IsNullOrEmpty(Model.AssemblyName)) + { + return Context.Assemblies + .Include(x => x.Components) + .ThenInclude(x => x.Assembly) + .FirstOrDefault(x => x.AssemblyName == Model.AssemblyName)? + .ViewModel; + } + return Context.Assemblies .Include(x => x.Components) .ThenInclude(x => x.Assembly) diff --git a/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs index cc1eaf6..0b0a5c2 100644 --- a/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs @@ -31,6 +31,14 @@ namespace ComputerShopDatabaseImplement.Implements { using var Context = new ComputerShopDatabase(); + // ComponentName is unique + if (!string.IsNullOrEmpty(Model.ComponentName)) + { + return Context.Components + .FirstOrDefault(x => x.ComponentName == Model.ComponentName)? + .ViewModel; + } + return Context.Components .FirstOrDefault(x => x.Id == Model.Id)? .ViewModel; diff --git a/ComputerShopDatabaseImplement/Implements/ProductStorage.cs b/ComputerShopDatabaseImplement/Implements/ProductStorage.cs index 58fea6c..68c8a73 100644 --- a/ComputerShopDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerShopDatabaseImplement/Implements/ProductStorage.cs @@ -50,6 +50,17 @@ namespace ComputerShopDatabaseImplement.Implements { using var Context = new ComputerShopDatabase(); + // ProductName is unique + if (!string.IsNullOrEmpty(Model.ProductName)) + { + return Context.Products + .Include(x => x.Shipment) + .Include(x => x.Components) + .ThenInclude(x => x.Product) + .FirstOrDefault(x => x.ProductName == Model.ProductName)? + .ViewModel; + } + return Context.Products .Include(x => x.Shipment) .Include(x => x.Components)