From 7903dee3e82224ac29fa625733d5131e6fb42830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sat, 1 Apr 2023 19:41:22 +0400 Subject: [PATCH] =?UTF-8?q?BusinessLogics=20=D0=A0=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BD=D0=B8=D0=BA=D0=B0=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/Worker/BuildLogic.cs | 120 +++++++++++++++++ .../BusinessLogics/Worker/CommentLogic.cs | 107 +++++++++++++++ .../BusinessLogics/Worker/PurchaseLogic.cs | 127 ++++++++++++++++++ .../HardwareShopBusinessLogic.csproj | 11 +- .../BuisnessLogicsContracts/IPurchaseLogic.cs | 5 +- 5 files changed, 367 insertions(+), 3 deletions(-) create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs new file mode 100644 index 0000000..49a2435 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs @@ -0,0 +1,120 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopContracts.BusinessLogicsContracts +{ + public class BuildLogic : IBuildLogic + { + private readonly ILogger _logger; + private readonly IBuildStorage _buildStorage; + public BuildLogic(ILogger logger, IBuildStorage buildStorage) + { + _logger = logger; + _buildStorage = buildStorage; + } + + public List? ReadList(BuildSearchModel? model) + { + _logger.LogInformation("ReadList. BuildName:{BuildName}. Id:{Id}", model?.BuildName, model?.Id); + var list = model == null ? _buildStorage.GetFullList() : _buildStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public BuildViewModel? ReadElement(BuildSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. BuildName:{BuildName}. Id:{Id}", model.BuildName, model.Id); + var element = _buildStorage.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(BuildBindingModel model) + { + CheckModel(model); + if (_buildStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(BuildBindingModel model) + { + CheckModel(model); + if (_buildStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(BuildBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_buildStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(BuildBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.BuildName)) + { + throw new ArgumentNullException("Нет названия сборки", nameof(model.BuildName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Price)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.UserId)); + } + if (model.Id < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у сборки", nameof(model.Id)); + } + _logger.LogInformation("Build. BuildName:{BuildName}. Price:{Price}. Id:{Id}", model.BuildName, model.Price, model.Id); + var element = _buildStorage.GetElement(new BuildSearchModel + { + BuildName = model.BuildName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Продукт с таким названием уже есть"); + } + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs new file mode 100644 index 0000000..4d4a130 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs @@ -0,0 +1,107 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopContracts.BusinessLogicsContracts +{ + public class CommentLogic : ICommentLogic + { + private readonly ILogger _logger; + private readonly ICommentStorage _commentStorage; + public CommentLogic(ILogger logger, ICommentStorage commentStorage) + { + _logger = logger; + _commentStorage = commentStorage; + } + + public List? ReadList(CommentSearchModel? model) + { + _logger.LogInformation("ReadList. BuildId:{BuildId}. Id:{Id}", model?.BuildId, model?.Id); + var list = model == null ? _commentStorage.GetFullList() : _commentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public CommentViewModel? ReadElement(CommentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. BuildId:{BuildId}. Id:{Id}", model.BuildId, model.Id); + var element = _commentStorage.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(CommentBindingModel model) + { + CheckModel(model); + if (_commentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(CommentBindingModel model) + { + CheckModel(model); + if (_commentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(CommentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_commentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(CommentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Text)) + { + throw new ArgumentNullException("Нет текста у комментария", nameof(model.Id)); + } + if (model.BuildId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у сборки", nameof(model.BuildId)); + } + if (model.Id < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у комментария", nameof(model.Id)); + } + + _logger.LogInformation("Comment. BuildId:{BuildId}. Id:{Id}", model.BuildId, model.Id); + } + } +} \ No newline at end of file diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs new file mode 100644 index 0000000..7aa764f --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs @@ -0,0 +1,127 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Enums; +using Microsoft.Extensions.Logging; + +namespace HardwareShopContracts.BusinessLogicsContracts +{ + public class PurchaseLogic : IPurchaseLogic + { + private readonly ILogger _logger; + private readonly IPurchaseStorage _purchaseStorage; + public PurchaseLogic(ILogger logger, IPurchaseStorage purchaseStorage) + { + _logger = logger; + _purchaseStorage = purchaseStorage; + } + + public List? ReadList(PurchaseSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{Id}", model?.Id); + var list = model == null ? _purchaseStorage.GetFullList() : _purchaseStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public CommentViewModel? ReadElement(CommentSearchModel model) + { + throw new NotImplementedException(); + } + + public bool DeliveryPurchase(PurchaseBindingModel model) + { + var viewModel = _purchaseStorage.GetElement(new PurchaseSearchModel { Id = model.Id }); + if (viewModel == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (viewModel.PurchaseStatus + 1 != PurchaseStatus.Выдан) + { + _logger.LogWarning("Change status operation failed"); + return false; + } + model.PurchaseStatus = PurchaseStatus.Выдан; + model.DatePurchase = DateTime.Now; + CheckModel(model, false); + if (_purchaseStorage.Update(model) == null) + { + _logger.LogWarning("Change status operation failed"); + return false; + } + return true; + } + + public bool Create(PurchaseBindingModel model) + { + CheckModel(model); + if (model.PurchaseStatus != PurchaseStatus.Неизвестен) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + model.PurchaseStatus = PurchaseStatus.Выполняется; + if (_purchaseStorage.Insert(model) == null) + { + model.PurchaseStatus = PurchaseStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(PurchaseBindingModel model) + { + CheckModel(model); + if (_purchaseStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(PurchaseBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_purchaseStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(PurchaseBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.Id < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у продукта", nameof(model.Id)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.UserId)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } + _logger.LogInformation("Purchase. PurchaseID:{Id}. Sum:{ Sum}", model.Id, model.Sum); + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj index 957eaa4..b47f27c 100644 --- a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj +++ b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -8,7 +8,14 @@ - + + + + + + + + diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IPurchaseLogic.cs b/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IPurchaseLogic.cs index f1ca0da..0882fad 100644 --- a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IPurchaseLogic.cs +++ b/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IPurchaseLogic.cs @@ -7,7 +7,10 @@ namespace HardwareShopContracts.BusinessLogicsContracts public interface IPurchaseLogic { List? ReadList(PurchaseSearchModel? model); - bool CreatePurchase(PurchaseBindingModel model); + CommentViewModel? ReadElement(CommentSearchModel model); + bool Create(PurchaseBindingModel model); + bool Update(PurchaseBindingModel model); + bool Delete(PurchaseBindingModel model); bool DeliveryPurchase(PurchaseBindingModel model); } }