From 385252ecd2965f008edd92b0e3389e89d5106cf5 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 25 Feb 2024 21:33:22 +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=BD=D0=B0=20=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SecuritySystem/SecuritySystem.sln | 6 + .../BusinessLogics/ComponentLogic.cs | 109 ++++++++++++++++ .../BusinessLogics/OrderLogic.cs | 119 ++++++++++++++++++ .../BusinessLogics/SecureLogic.cs | 107 ++++++++++++++++ .../SecuritySystemBusinessLogic.csproj | 17 +++ .../StoragesContracts/IComponentStorage.cs | 1 + 6 files changed, 359 insertions(+) create mode 100644 SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ComponentLogic.cs create mode 100644 SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs create mode 100644 SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/SecureLogic.cs create mode 100644 SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj diff --git a/SecuritySystem/SecuritySystem.sln b/SecuritySystem/SecuritySystem.sln index c4ffe5b..7a9ed1d 100644 --- a/SecuritySystem/SecuritySystem.sln +++ b/SecuritySystem/SecuritySystem.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemDataModels", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemContracts", "SecuritySystemContracts\SecuritySystemContracts.csproj", "{0737BCB2-EEDB-44A4-8BD2-5B5EA689A7FF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecuritySystemBusinessLogic", "SecuritySystemBusinessLogic\SecuritySystemBusinessLogic.csproj", "{38C8A0AB-9363-4914-B967-02586827588D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {0737BCB2-EEDB-44A4-8BD2-5B5EA689A7FF}.Debug|Any CPU.Build.0 = Debug|Any CPU {0737BCB2-EEDB-44A4-8BD2-5B5EA689A7FF}.Release|Any CPU.ActiveCfg = Release|Any CPU {0737BCB2-EEDB-44A4-8BD2-5B5EA689A7FF}.Release|Any CPU.Build.0 = Release|Any CPU + {38C8A0AB-9363-4914-B967-02586827588D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {38C8A0AB-9363-4914-B967-02586827588D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {38C8A0AB-9363-4914-B967-02586827588D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {38C8A0AB-9363-4914-B967-02586827588D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ComponentLogic.cs b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..83354d5 --- /dev/null +++ b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,109 @@ +using Microsoft.Extensions.Logging; +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.BusinessLogicsContracts; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; + +namespace SecuritySystemBusinessLogic.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/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..44b56f0 --- /dev/null +++ b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,119 @@ +using Microsoft.Extensions.Logging; +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.BusinessLogicsContracts; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Enums; + +namespace SecuritySystemBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) return false; + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool ChangeStatus(OrderBindingModel model, OrderStatus status) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (element == null) + { + _logger.LogWarning("Read operation failed"); + return false; + } + if (element.Status != status - 1) + { + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); + } + OrderStatus oldStatus = model.Status; + model.Status = status; + if (model.Status == OrderStatus.Выдан) + model.DateImplement = DateTime.Now; + if (_orderStorage.Update(model) == null) + { + model.Status = oldStatus; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выполняется); + } + + public bool FinishOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Готов); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выдан); + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.SecureId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор secure", nameof(model.SecureId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество secure в заказе должно быть больше 0", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); + } + _logger.LogInformation("Order. Sum:{Cost}. Id: {Id}", model.Sum, model.Id); + } + } +} diff --git a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/SecureLogic.cs b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/SecureLogic.cs new file mode 100644 index 0000000..3cc8f0a --- /dev/null +++ b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/SecureLogic.cs @@ -0,0 +1,107 @@ +using Microsoft.Extensions.Logging; +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.BusinessLogicsContracts; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; + +namespace SecuritySystemBusinessLogic.BusinessLogics +{ + public class SecureLogic : ISecureLogic + { + private readonly ILogger _logger; + private readonly ISecureStorage _secureStorage; + public SecureLogic(ILogger logger, ISecureStorage secureStorage) + { + _logger = logger; + _secureStorage = secureStorage; + } + public bool Create(SecureBindingModel model) + { + CheckModel(model); + if (_secureStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Delete(SecureBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_secureStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + public SecureViewModel? ReadElement(SecureSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. SecureName:{SecureName}. Id:{Id}", model.SecureName, model.Id); + var element = _secureStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public List? ReadList(SecureSearchModel? model) + { + _logger.LogInformation("ReadList. SecureName:{SecureName}. Id:{Id}", model?.SecureName, model?.Id); + var list = model == null ? _secureStorage.GetFullList() : _secureStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public bool Update(SecureBindingModel model) + { + CheckModel(model); + if (_secureStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + private void CheckModel(SecureBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.SecureName)) + { + throw new ArgumentNullException("Нет названия secure", nameof(model.SecureName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена secure должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Secure. SecureName:{SecureName}. Price:{Price}. Id:{Id}", model.SecureName, model.Price, model.Id); + var element = _secureStorage.GetElement(new SecureSearchModel + { + SecureName = model.SecureName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Secure с таким названием уже есть"); + } + } + } +} diff --git a/SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj b/SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj new file mode 100644 index 0000000..d936e08 --- /dev/null +++ b/SecuritySystem/SecuritySystemBusinessLogic/SecuritySystemBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/SecuritySystem/SecuritySystemContracts/StoragesContracts/IComponentStorage.cs b/SecuritySystem/SecuritySystemContracts/StoragesContracts/IComponentStorage.cs index c28e617..67b3928 100644 --- a/SecuritySystem/SecuritySystemContracts/StoragesContracts/IComponentStorage.cs +++ b/SecuritySystem/SecuritySystemContracts/StoragesContracts/IComponentStorage.cs @@ -13,4 +13,5 @@ namespace SecuritySystemContracts.StoragesContracts ComponentViewModel? Update(ComponentBindingModel model); ComponentViewModel? Delete(ComponentBindingModel model); } + }