diff --git a/.gitignore b/.gitignore index ca1c7a3..c6b950f 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/FoodOrders/AbstractFoodOrdersListImplement diff --git a/FoodOrders/AbstractFoodOrdersBusinessLogic/AbstractFoodOrdersBusinessLogic.csproj b/FoodOrders/AbstractFoodOrdersBusinessLogic/AbstractFoodOrdersBusinessLogic.csproj new file mode 100644 index 0000000..9fc4037 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersBusinessLogic/AbstractFoodOrdersBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..f64f450 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,114 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.BusinessLogicsContracts; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; + + +namespace AbstractFoodOrdersBusinessLogic.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) + { + _logger.LogInformation("ReadList. ComponentName:{ComponentName}.Id:{ Id}", model?.ComponentName, model?.Id); + 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)); + } + _logger.LogInformation("ReadElement. ComponentName:{ComponentName}.Id:{ Id}", model.ComponentName, model.Id); + var element = _componentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. 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) == 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 ArgumentNullException("Нет названия компонента", + nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Component. ComponentName:{ComponentName}.Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id); + var element = _componentStorage.GetElement(new ComponentSearchModel{ ComponentName = model.ComponentName }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } + +} diff --git a/FoodOrders/AbstractFoodOrdersContracts/AbstractFoodOrdersContracts.csproj b/FoodOrders/AbstractFoodOrdersContracts/AbstractFoodOrdersContracts.csproj index e562052..8791d49 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/AbstractFoodOrdersContracts.csproj +++ b/FoodOrders/AbstractFoodOrdersContracts/AbstractFoodOrdersContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/FoodOrders/AbstractFoodOrdersDataModels/AbstractFoodOrdersDataModels.csproj b/FoodOrders/AbstractFoodOrdersDataModels/AbstractFoodOrdersDataModels.csproj index 132c02c..6ea4cf5 100644 --- a/FoodOrders/AbstractFoodOrdersDataModels/AbstractFoodOrdersDataModels.csproj +++ b/FoodOrders/AbstractFoodOrdersDataModels/AbstractFoodOrdersDataModels.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/FoodOrders/FoodOrders.sln b/FoodOrders/FoodOrders.sln index e8ea3d4..e27e831 100644 --- a/FoodOrders/FoodOrders.sln +++ b/FoodOrders/FoodOrders.sln @@ -5,9 +5,13 @@ VisualStudioVersion = 17.3.32901.215 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrders", "FoodOrders\FoodOrders.csproj", "{4DC7A9CF-C994-4010-B23B-F4DB4D964D1D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersDataModels", "AbstractFoodOrdersDataModels\AbstractFoodOrdersDataModels.csproj", "{797093A6-6C7B-414E-874F-1BB48A6468C7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersDataModels", "AbstractFoodOrdersDataModels\AbstractFoodOrdersDataModels.csproj", "{797093A6-6C7B-414E-874F-1BB48A6468C7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersContracts", "AbstractFoodOrdersContracts\AbstractFoodOrdersContracts.csproj", "{926B4BA4-42BC-4D7B-AF27-01B16493D883}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersContracts", "AbstractFoodOrdersContracts\AbstractFoodOrdersContracts.csproj", "{926B4BA4-42BC-4D7B-AF27-01B16493D883}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersBusinessLogic", "AbstractFoodOrdersBusinessLogic\AbstractFoodOrdersBusinessLogic.csproj", "{A595D2B1-2193-4688-BECD-6D815AEE1142}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersListImplement", "AbstractFoodOrdersListImplement\AbstractFoodOrdersListImplement.csproj", "{275ED134-536F-4025-BE10-2718F66FBD10}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +31,14 @@ Global {926B4BA4-42BC-4D7B-AF27-01B16493D883}.Debug|Any CPU.Build.0 = Debug|Any CPU {926B4BA4-42BC-4D7B-AF27-01B16493D883}.Release|Any CPU.ActiveCfg = Release|Any CPU {926B4BA4-42BC-4D7B-AF27-01B16493D883}.Release|Any CPU.Build.0 = Release|Any CPU + {A595D2B1-2193-4688-BECD-6D815AEE1142}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A595D2B1-2193-4688-BECD-6D815AEE1142}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A595D2B1-2193-4688-BECD-6D815AEE1142}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A595D2B1-2193-4688-BECD-6D815AEE1142}.Release|Any CPU.Build.0 = Release|Any CPU + {275ED134-536F-4025-BE10-2718F66FBD10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {275ED134-536F-4025-BE10-2718F66FBD10}.Debug|Any CPU.Build.0 = Debug|Any CPU + {275ED134-536F-4025-BE10-2718F66FBD10}.Release|Any CPU.ActiveCfg = Release|Any CPU + {275ED134-536F-4025-BE10-2718F66FBD10}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FoodOrders/FoodOrders/FoodOrders.csproj b/FoodOrders/FoodOrders/FoodOrders.csproj index b57c89e..7173d64 100644 --- a/FoodOrders/FoodOrders/FoodOrders.csproj +++ b/FoodOrders/FoodOrders/FoodOrders.csproj @@ -8,4 +8,8 @@ enable + + + + \ No newline at end of file