From 06ec3952d30eb5d06dd0b97de8a80221ed9a1a92 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Mon, 29 Apr 2024 13:17:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=BD=D0=B8=D0=BA=D0=B0=20(+=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=D0=B0=20vendorSearchModel)=20?= =?UTF-8?q?=D0=B8=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D0=B4=D0=BE=D0=B2=D1=89=D0=B8=D0=BA=D0=B0=20(+=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=D0=B0=20storekeeperSearchModel)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/StoreKeeperLogic.cs | 107 ++++++++++++++++++ .../BusinessLogic/VendorLogic.cs | 107 ++++++++++++++++++ .../SearchModels/StoreKeeperSearchModel.cs | 2 +- .../SearchModels/VendorSearchModel.cs | 2 +- 4 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/StoreKeeperLogic.cs create mode 100644 ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/StoreKeeperLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/StoreKeeperLogic.cs new file mode 100644 index 0000000..aa056f6 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/StoreKeeperLogic.cs @@ -0,0 +1,107 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class StoreKeeperLogic : IStoreKeeperLogic + { + private readonly ILogger _logger; + private readonly IStoreKeeperStorage _storeKeeperStorage; + public StoreKeeperLogic(ILogger logger, IStoreKeeperStorage storeKeeperStorage) + { + _logger = logger; + _storeKeeperStorage = storeKeeperStorage; + } + public List? ReadList(StoreKeeperSearchModel? model) + { + _logger.LogInformation("ReadList. Login:{Login}.Id:{Id}", model?.Login, model?.Id); + var list = model == null ? _storeKeeperStorage.GetFullList() : _storeKeeperStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public StoreKeeperViewModel? ReadElement(StoreKeeperSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login:{Login}.Id:{Id}", model.Login, model.Id); + var element = _storeKeeperStorage.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(StoreKeeperBindingModel model) + { + CheckModel(model); + if (_storeKeeperStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(StoreKeeperBindingModel model) + { + CheckModel(model); + if (_storeKeeperStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(StoreKeeperBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_storeKeeperStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(StoreKeeperBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина кладовщика", nameof(model.Login)); + } + var element = _storeKeeperStorage.GetElement(new StoreKeeperSearchModel + { + Login = model.Login + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Кладовщик с таким логином уже есть"); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs new file mode 100644 index 0000000..449400c --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs @@ -0,0 +1,107 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.BusinessLogicsContracts; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace ComputerHardwareStoreBusinessLogic.BusinessLogic +{ + public class VendorLogic : IVendorLogic + { + private readonly ILogger _logger; + private readonly IVendorStorage _vendorStorage; + public VendorLogic(ILogger logger, IVendorStorage vendorStorage) + { + _logger = logger; + _vendorStorage = vendorStorage; + } + public List? ReadList(VendorSearchModel? model) + { + _logger.LogInformation("ReadList. Login:{Login}.Id:{Id}", model?.Login, model?.Id); + var list = model == null ? _vendorStorage.GetFullList() : _vendorStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public VendorViewModel? ReadElement(VendorSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login:{Login}.Id:{Id}", model.Login, model.Id); + var element = _vendorStorage.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(VendorBindingModel model) + { + CheckModel(model); + if (_vendorStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(VendorBindingModel model) + { + CheckModel(model); + if (_vendorStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(VendorBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_vendorStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(VendorBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина работника", nameof(model.Login)); + } + var element = _vendorStorage.GetElement(new VendorSearchModel + { + Login = model.Login + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Работник с таким логином уже есть"); + } + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs index e069ba1..ed748fe 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs @@ -3,7 +3,7 @@ public class StoreKeeperSearchModel { public int? Id { get; set; } - public string? Email { get; set; } + public string? Login { get; set; } public string? Password { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs index f15394a..fc54068 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/VendorSearchModel.cs @@ -3,7 +3,7 @@ public class VendorSearchModel { public int? Id { get; set; } - public string? Email { get; set; } + public string? Login { get; set; } public string? Password { get; set; } } }