From 5f9b1f4a2fe264c2ff7d8c50e7e363ad0090e915 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Wed, 1 May 2024 13:56:07 +0400 Subject: [PATCH] Start implementing BusinessLogic --- .../BusinessLogics/AssemblyLogic.cs | 117 ++++++++++++++++++ .../BusinessLogics/ComponentLogic.cs | 2 +- .../BusinessLogics/ProductLogic.cs | 117 ++++++++++++++++++ .../BindingModels/AssemblyBindingModel.cs | 2 +- .../BindingModels/ProductBindingModel.cs | 2 +- .../ViewModels/AssemblyViewModel.cs | 2 +- .../ViewModels/ProductViewModel.cs | 2 +- .../Models/IAssemblyModel.cs | 2 +- .../Models/IProductModel.cs | 2 +- .../Models/Assembly.cs | 8 +- .../Models/Product.cs | 8 +- .../Models/Request.cs | 6 +- 12 files changed, 252 insertions(+), 18 deletions(-) create mode 100644 ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs create mode 100644 ComputerShopBusinessLogic/BusinessLogics/ProductLogic.cs diff --git a/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs new file mode 100644 index 0000000..43a1e7a --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs @@ -0,0 +1,117 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class AssemblyLogic : IAssemblyLogic + { + private readonly ILogger _logger; + private readonly IAssemblyStorage _assemblyStorage; + + public AssemblyLogic(ILogger Logger, IAssemblyStorage AssemblyStorage) + { + _logger = Logger; + _assemblyStorage = AssemblyStorage; + } + + public List? ReadList(AssemblySearchModel? Model) + { + var List = (Model == null) ? _assemblyStorage.GetFullList() : _assemblyStorage.GetFilteredList(Model); + + if (List == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", List.Count); + return List; + } + + public AssemblyViewModel? ReadElement(AssemblySearchModel Model) + { + if (Model == null) + throw new ArgumentNullException(nameof(Model)); + + var Element = _assemblyStorage.GetElement(Model); + if (Element == null) + { + _logger.LogWarning("ReadElement Assembly not found"); + return null; + } + + _logger.LogInformation("ReadElement Assembly found. Id: {Id}", Element.Id); + return Element; + } + + public bool Create(AssemblyBindingModel Model) + { + CheckModel(Model); + + if (_assemblyStorage.Insert(Model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool Update(AssemblyBindingModel Model) + { + CheckModel(Model); + + if (_assemblyStorage.Update(Model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + public bool Delete(AssemblyBindingModel Model) + { + CheckModel(Model, false); + _logger.LogInformation("Delete. Id:{Id}", Model.Id); + + if (_assemblyStorage.Delete(Model) is null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + + return true; + } + + private void CheckModel(AssemblyBindingModel Model, bool WithParams = true) + { + if (Model == null) + throw new ArgumentNullException(nameof(Model)); + + if (!WithParams) + return; + + if (string.IsNullOrEmpty(Model.AssemblyName)) + throw new ArgumentException($"У сборки отсутствует название"); + + if (string.IsNullOrEmpty(Model.Category)) + throw new ArgumentException($"У сборки отсутствует категория"); + + if (Model.Price <= 0) + throw new ArgumentException("Цена сборки должна быть больше 0", nameof(Model.Price)); + + var Element = _assemblyStorage.GetElement(new AssemblySearchModel + { + AssemblyName = Model.AssemblyName + }); + + if (Element != null && Element.Id != Model.Id) + throw new InvalidOperationException("Товар с таким названием уже есть"); + } + } +} diff --git a/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs index c0b094c..2f8fc59 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -12,7 +12,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IComponentStorage _componentStorage; - public ComponentLogic(ILogger Logger, IComponentStorage ComponentStorage) + public ComponentLogic(ILogger Logger, IComponentStorage ComponentStorage) { _logger = Logger; _componentStorage = ComponentStorage; diff --git a/ComputerShopBusinessLogic/BusinessLogics/ProductLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/ProductLogic.cs new file mode 100644 index 0000000..8497091 --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/ProductLogic.cs @@ -0,0 +1,117 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + 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) + { + var List = (Model == null) ? _productStorage.GetFullList() : _productStorage.GetFilteredList(Model); + + if (List == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", List.Count); + return List; + } + + public ProductViewModel? ReadElement(ProductSearchModel Model) + { + if (Model == null) + throw new ArgumentNullException(nameof(Model)); + + var Element = _productStorage.GetElement(Model); + if (Element == null) + { + _logger.LogWarning("ReadElement Product not found"); + return null; + } + + _logger.LogInformation("ReadElement Product found. Id: {Id}", Element.Id); + return Element; + } + + public bool Create(ProductBindingModel Model) + { + CheckModel(Model); + + if (_productStorage.Insert(Model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool Update(ProductBindingModel Model) + { + CheckModel(Model); + + if (_productStorage.Update(Model) == null) + { + _logger.LogWarning("Update 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) is null) + { + _logger.LogWarning("Delete 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.ProductName)) + throw new ArgumentException($"У товара отсутствует название"); + + if (Model.Price <= 0) + throw new ArgumentException("Цена товара должна быть больше 0", nameof(Model.Price)); + + if (Model.Warranty <= 0) + throw new ArgumentException("Гарантия на товар должна быть больше 0", nameof(Model.Warranty)); + + var Element = _productStorage.GetElement(new ProductSearchModel + { + ProductName = Model.ProductName + }); + + if (Element != null && Element.Id != Model.Id) + throw new InvalidOperationException("Товар с таким названием уже есть"); + } + } +} diff --git a/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs b/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs index 956cc7d..6ca738d 100644 --- a/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs +++ b/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs @@ -10,7 +10,7 @@ namespace ComputerShopContracts.BindingModels public string AssemblyName { get; set; } = string.Empty; - public double Cost { get; set; } + public double Price { get; set; } public string Category { get; set; } = string.Empty; diff --git a/ComputerShopContracts/BindingModels/ProductBindingModel.cs b/ComputerShopContracts/BindingModels/ProductBindingModel.cs index bbfca57..9991cb2 100644 --- a/ComputerShopContracts/BindingModels/ProductBindingModel.cs +++ b/ComputerShopContracts/BindingModels/ProductBindingModel.cs @@ -12,7 +12,7 @@ namespace ComputerShopContracts.BindingModels public string ProductName { get; set; } = string.Empty; - public double Cost { get; set; } + public double Price { get; set; } public int Warranty { get; set; } diff --git a/ComputerShopContracts/ViewModels/AssemblyViewModel.cs b/ComputerShopContracts/ViewModels/AssemblyViewModel.cs index 27ef688..4a10b30 100644 --- a/ComputerShopContracts/ViewModels/AssemblyViewModel.cs +++ b/ComputerShopContracts/ViewModels/AssemblyViewModel.cs @@ -13,7 +13,7 @@ namespace ComputerShopContracts.ViewModels public string AssemblyName { get; set; } = string.Empty; [DisplayName("Стоимость")] - public double Cost { get; set; } + public double Price { get; set; } [DisplayName("Категория")] public string Category { get; set; } = string.Empty; diff --git a/ComputerShopContracts/ViewModels/ProductViewModel.cs b/ComputerShopContracts/ViewModels/ProductViewModel.cs index 5f63b73..6853f23 100644 --- a/ComputerShopContracts/ViewModels/ProductViewModel.cs +++ b/ComputerShopContracts/ViewModels/ProductViewModel.cs @@ -18,7 +18,7 @@ namespace ComputerShopContracts.ViewModels public string ProductName { get; set; } = string.Empty; [DisplayName("Стоимость")] - public double Cost { get; set; } + public double Price { get; set; } [DisplayName("Гарантия (мес.)")] public int Warranty { get; set; } diff --git a/ComputerShopDataModels/Models/IAssemblyModel.cs b/ComputerShopDataModels/Models/IAssemblyModel.cs index fa6bdcb..3a0b933 100644 --- a/ComputerShopDataModels/Models/IAssemblyModel.cs +++ b/ComputerShopDataModels/Models/IAssemblyModel.cs @@ -18,7 +18,7 @@ /// /// Стоимость /// - double Cost { get; } + double Price { get; } /// /// Категория diff --git a/ComputerShopDataModels/Models/IProductModel.cs b/ComputerShopDataModels/Models/IProductModel.cs index 65fa951..d15baca 100644 --- a/ComputerShopDataModels/Models/IProductModel.cs +++ b/ComputerShopDataModels/Models/IProductModel.cs @@ -18,7 +18,7 @@ /// /// Стоимость товара /// - double Cost { get; } + double Price { get; } /// /// Гарантия diff --git a/ComputerShopDatabaseImplement/Models/Assembly.cs b/ComputerShopDatabaseImplement/Models/Assembly.cs index f3ace2b..7b470ae 100644 --- a/ComputerShopDatabaseImplement/Models/Assembly.cs +++ b/ComputerShopDatabaseImplement/Models/Assembly.cs @@ -17,7 +17,7 @@ namespace ComputerShopDatabaseImplement.Models public string AssemblyName { get; private set; } = string.Empty; [Required] - public double Cost { get; private set; } + public double Price { get; private set; } [Required] public string Category { get; private set; } = string.Empty; @@ -54,7 +54,7 @@ namespace ComputerShopDatabaseImplement.Models Id = Model.Id, UserId = Model.UserId, AssemblyName = Model.AssemblyName, - Cost = Model.Cost, + Price = Model.Price, Category = Model.Category, Components = Model.AssemblyComponents.Select(x => new AssemblyComponent { @@ -76,7 +76,7 @@ namespace ComputerShopDatabaseImplement.Models Category = Model.Category; } - Cost = Model.Cost; + Price = Model.Price; } public AssemblyViewModel ViewModel => new() @@ -84,7 +84,7 @@ namespace ComputerShopDatabaseImplement.Models Id = Id, UserId = UserId, AssemblyName = AssemblyName, - Cost = Cost, + Price = Price, Category = Category, AssemblyComponents = AssemblyComponents, }; diff --git a/ComputerShopDatabaseImplement/Models/Product.cs b/ComputerShopDatabaseImplement/Models/Product.cs index 1a7084e..924a19d 100644 --- a/ComputerShopDatabaseImplement/Models/Product.cs +++ b/ComputerShopDatabaseImplement/Models/Product.cs @@ -21,7 +21,7 @@ namespace ComputerShopDatabaseImplement.Models public string ProductName { get; set; } = string.Empty; [Required] - public double Cost { get; set; } + public double Price { get; set; } [Required] public int Warranty { get; set; } @@ -56,7 +56,7 @@ namespace ComputerShopDatabaseImplement.Models UserId = Model.UserId, ShipmentId = Model.ShipmentId, ProductName = Model.ProductName, - Cost = Model.Cost, + Price = Model.Price, Warranty = Model.Warranty, Components = Model.ProductComponents.Select(x => new ProductComponent { @@ -74,7 +74,7 @@ namespace ComputerShopDatabaseImplement.Models } ShipmentId = Model.ShipmentId; - Cost = Model.Cost; + Price = Model.Price; Warranty = Model.Warranty; } @@ -84,7 +84,7 @@ namespace ComputerShopDatabaseImplement.Models UserId = UserId, ShipmentId = ShipmentId, ProviderName = Shipment?.ProviderName, - Cost = Cost, + Price = Price, Warranty = Warranty, ProductComponents = ProductComponents, }; diff --git a/ComputerShopDatabaseImplement/Models/Request.cs b/ComputerShopDatabaseImplement/Models/Request.cs index e9ab601..84cd5c6 100644 --- a/ComputerShopDatabaseImplement/Models/Request.cs +++ b/ComputerShopDatabaseImplement/Models/Request.cs @@ -95,7 +95,7 @@ namespace ComputerShopDatabaseImplement.Models { var currentRequest = context.Requests.First(x => x.Id == Id); //стоимость сборки, связанной с заявкой (или 0, если заявка не связана со сборкой) - double cost_of_assembly = (currentRequest.Assembly.Cost != null) ? currentRequest.Assembly.Cost : 0; + double cost_of_assembly = (currentRequest.Assembly.Price != null) ? currentRequest.Assembly.Price : 0; var requestOrders = context.RequestOrders.Where(x => x.RequestId == model.Id).ToList(); @@ -134,7 +134,7 @@ namespace ComputerShopDatabaseImplement.Models public void ConnectAssembly(ComputerShopDatabase context, RequestBindingModel model) { //стоимость старой сборки (или 0, если её не было) - double cost_of_old_assembly = (Assembly.Cost != null) ? Assembly.Cost : 0; + double cost_of_old_assembly = (Assembly.Price != null) ? Assembly.Price : 0; AssemblyId = model.AssemblyId; Assembly = context.Assemblies.First(x => x.Id == model.AssemblyId); @@ -145,7 +145,7 @@ namespace ComputerShopDatabaseImplement.Models //вычитание из стоимости заказа старой сборки connectedOrder.ChangeSum(-cost_of_old_assembly); //прибавление стоимости новой сборки - connectedOrder.ChangeSum(Assembly.Cost); + connectedOrder.ChangeSum(Assembly.Price); context.SaveChanges(); } }