From 52914c9bd4273f9ce5f56a632d2ae5c291420ea5 Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Sun, 21 Apr 2024 19:06:13 +0400 Subject: [PATCH] Business Implementer --- .../BusinessLogic/DetailLogic.cs | 2 +- .../BusinessLogic/ImplementerLogic.cs | 112 ++++++++++++++++++ .../BusinessLogic/ProductLogic.cs | 107 +++++++++++++++++ .../BusinessLogic/ProductionLogic.cs | 110 +++++++++++++++++ .../IImplementerLogic.cs | 4 +- .../IProductionLogic.cs | 4 +- 6 files changed, 334 insertions(+), 5 deletions(-) create mode 100644 Course/BusinessLogic/BusinessLogic/ImplementerLogic.cs create mode 100644 Course/BusinessLogic/BusinessLogic/ProductLogic.cs create mode 100644 Course/BusinessLogic/BusinessLogic/ProductionLogic.cs diff --git a/Course/BusinessLogic/BusinessLogic/DetailLogic.cs b/Course/BusinessLogic/BusinessLogic/DetailLogic.cs index 85666a5..43f11f0 100644 --- a/Course/BusinessLogic/BusinessLogic/DetailLogic.cs +++ b/Course/BusinessLogic/BusinessLogic/DetailLogic.cs @@ -19,7 +19,7 @@ namespace BusinessLogic.BusinessLogic } public List? ReadList(DetailSearchModel? model) { - _logger.LogInformation("ReadList. DetailName:{name}. Id:{Id}", model.Name, model.Id); + _logger.LogInformation("ReadList. DetailName:{name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId); var list = model == null ? _detailStorage.GetFullList() : _detailStorage.GetFilteredList(model); if (list == null) { diff --git a/Course/BusinessLogic/BusinessLogic/ImplementerLogic.cs b/Course/BusinessLogic/BusinessLogic/ImplementerLogic.cs new file mode 100644 index 0000000..9bf9ddd --- /dev/null +++ b/Course/BusinessLogic/BusinessLogic/ImplementerLogic.cs @@ -0,0 +1,112 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicsContracts; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace BusinessLogic.BusinessLogic +{ + public class ImplementerLogic : IImplementerLogic + { + private readonly ILogger _logger; + private readonly IImplementerStorage _storage; + + public ImplementerLogic(ILogger logger, IImplementerStorage storage) + { + _logger = logger; + _storage = storage; + } + public List? ReadList(ImplementerSearchModel? model) + { + _logger.LogInformation("ReadList. Login:{Login}. Id:{Id}", model.Login, model.Id); + var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null"); + return null; + } + return list; + } + public ImplementerViewModel? ReadElement(ImplementerSearchModel? model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login:{Login}. Id:{Id}.", model.Login, model.Id); + var elem = _storage.GetElement(model); + if (elem == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id); + return elem; + } + private void CheckModel(ImplementerBindingModel? 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)); + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина пользователя", nameof(model)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля пользователя", nameof(model)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почты пользователя", nameof(model)); + } + var elem = _storage.GetElement(new ImplementerSearchModel + { + Login = model.Login + }); + if (elem != null && model.Id != elem.Id) + { + throw new InvalidOperationException("Такой логин уже используется в системе"); + } + } + public bool Create(ImplementerBindingModel? model) + { + CheckModel(model); + if (_storage.Insert(model!) == null) + { + _logger.LogWarning("Insert error"); + return false; + } + return true; + } + public bool Update(ImplementerBindingModel? model) + { + CheckModel(model); + if (_storage.Update(model!) == null) + { + _logger.LogWarning("Update error"); + return false; + } + return true; + } + public bool Delete(ImplementerBindingModel? model) + { + CheckModel(model, false); + _logger.LogInformation("Delete Implementer. Id:{Id}", model!.Id); + if (_storage.Delete(model!) == null) + { + _logger.LogWarning("Delete error"); + return false; + } + return true; + } + } +} diff --git a/Course/BusinessLogic/BusinessLogic/ProductLogic.cs b/Course/BusinessLogic/BusinessLogic/ProductLogic.cs new file mode 100644 index 0000000..04b07b0 --- /dev/null +++ b/Course/BusinessLogic/BusinessLogic/ProductLogic.cs @@ -0,0 +1,107 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using Contracts.StoragesContracts; +using Contracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace BusinessLogic.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. ProductName:{name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId); + var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogWarning("ReadList. Count:{Count}", list.Count); + return list; + } + public ProductViewModel? ReadElement(ProductSearchModel? model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ProductName:{Name}. Id:{Id}", model.Name, model.Id); + var elem = _productStorage.GetElement(model); + if (elem == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id); + return elem; + } + 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 ArgumentNullException("Нет названия изделия", nameof(model.Name)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Product. ProductName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id); + var elem = _productStorage.GetElement(new ProductSearchModel + { + Name = model.Name + }); + if (elem != null && elem.Id != model.Id) + { + throw new InvalidOperationException("Изделие с таким названием уже существует"); + } + } + public bool Create(ProductBindingModel? model) + { + CheckModel(model); + if (_productStorage.Insert(model!) == null) + { + _logger.LogWarning("Insert error"); + return false; + } + return true; + } + public bool Update(ProductBindingModel? model) + { + CheckModel(model); + if (_productStorage.Update(model!) == null) + { + _logger.LogWarning("Update error"); + 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 error"); + return false; + } + return true; + } + } +} diff --git a/Course/BusinessLogic/BusinessLogic/ProductionLogic.cs b/Course/BusinessLogic/BusinessLogic/ProductionLogic.cs new file mode 100644 index 0000000..2d72d66 --- /dev/null +++ b/Course/BusinessLogic/BusinessLogic/ProductionLogic.cs @@ -0,0 +1,110 @@ +using Contracts.BindingModels; +using Contracts.BusinessLogicsContracts; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace BusinessLogic.BusinessLogic +{ + public class ProductionLogic : IProductionLogic + { + private readonly ILogger _logger; + private readonly IProductionStorage _productionStorage; + + public ProductionLogic(ILogger logger, IProductionStorage productionStorage) + { + _logger = logger; + _productionStorage = productionStorage; + } + public List? ReadList(ProductionSearchModel? model) + { + _logger.LogInformation("ReadList. ProductionName:{name}. Id:{Id}. UserId:{UserId}", model?.Name, model?.Id, model?.UserId); + var list = model == null ? _productionStorage.GetFullList() : _productionStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogWarning("ReadList. Count:{Count}", list.Count); + return list; + } + public ProductionViewModel? ReadElement(ProductionSearchModel? model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ProductionName:{Name}. Id:{Id}", model.Name, model.Id); + var elem = _productionStorage.GetElement(model); + if (elem == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", elem.Id); + return elem; + } + + + private void CheckModel(ProductionBindingModel? 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)); + } + _logger.LogInformation("Production. ProductionName:{Name}. Cost:{Cost}. Id:{Id}", model.Name, model.Cost, model.Id); + var elem = _productionStorage.GetElement(new ProductionSearchModel + { + Name = model.Name + }); + if (elem != null && elem.Id != model.Id) + { + throw new InvalidOperationException("Деталь с таким названием уже существует"); + } + } + public bool Create(ProductionBindingModel? model) + { + CheckModel(model); + if (_productionStorage.Insert(model!) == null) + { + _logger.LogWarning("Insert error"); + return false; + } + return true; + } + public bool Update(ProductionBindingModel? model) + { + CheckModel(model); + if (_productionStorage.Update(model!) == null) + { + _logger.LogWarning("Update error"); + return false; + } + return true; + } + public bool Delete(ProductionBindingModel? model) + { + CheckModel(model, false); + _logger.LogInformation("Delete Id:{Id}", model!.Id); + if (_productionStorage.Delete(model!) == null) + { + _logger.LogWarning("Delete error"); + return false; + } + return true; + } + } +} diff --git a/Course/Contracts/BusinessLogicsContracts/IImplementerLogic.cs b/Course/Contracts/BusinessLogicsContracts/IImplementerLogic.cs index 2923b51..1cc0acc 100644 --- a/Course/Contracts/BusinessLogicsContracts/IImplementerLogic.cs +++ b/Course/Contracts/BusinessLogicsContracts/IImplementerLogic.cs @@ -9,7 +9,7 @@ namespace Contracts.BusinessLogicsContracts List? ReadList(ImplementerSearchModel? model); ImplementerViewModel? ReadElement(ImplementerSearchModel? model); bool Create(ImplementerBindingModel? model); - bool Update(ImplementerViewModel? model); - bool Delete(ImplementerViewModel? model); + bool Update(ImplementerBindingModel? model); + bool Delete(ImplementerBindingModel? model); } } diff --git a/Course/Contracts/BusinessLogicsContracts/IProductionLogic.cs b/Course/Contracts/BusinessLogicsContracts/IProductionLogic.cs index 16b3ae9..1efa92a 100644 --- a/Course/Contracts/BusinessLogicsContracts/IProductionLogic.cs +++ b/Course/Contracts/BusinessLogicsContracts/IProductionLogic.cs @@ -9,7 +9,7 @@ namespace Contracts.BusinessLogicsContracts List? ReadList(ProductionSearchModel? model); ProductionViewModel? ReadElement(ProductionSearchModel? model); bool Create(ProductionBindingModel? model); - bool Update(ProductionViewModel? model); - bool Delete(ProductionViewModel? model); + bool Update(ProductionBindingModel? model); + bool Delete(ProductionBindingModel? model); } }