From 18fc1b0e67de989541ff72a8b9254f2b54cd8425 Mon Sep 17 00:00:00 2001 From: dasha Date: Sat, 1 Apr 2023 21:43:08 +0400 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB?= =?UTF-8?q?=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 --- .../Storekeeper/ComponentLogic.cs | 122 +++++++++++++++++ .../BusinessLogics/Storekeeper/GoodLogic.cs | 122 +++++++++++++++++ .../BusinessLogics/Storekeeper/OrderLogic.cs | 124 +++++++++++++++++ .../BusinessLogics/UserLogic.cs | 128 ++++++++++++++++++ .../HardwareShopBusinessLogic.csproj | 4 - 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs create mode 100644 HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs new file mode 100644 index 0000000..e24794f --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/ComponentLogic.cs @@ -0,0 +1,122 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert 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; + } + + 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 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 bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update 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)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у пользователя", + nameof(model.UserId)); + } + _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/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs new file mode 100644 index 0000000..2120a54 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/GoodLogic.cs @@ -0,0 +1,122 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper +{ + public class GoodLogic : IGoodLogic + { + private readonly ILogger _logger; + private readonly IGoodStorage _goodStorage; + public GoodLogic(ILogger logger, IGoodStorage goodStorage) + { + _logger = logger; + _goodStorage = goodStorage; + } + public bool Create(GoodBindingModel model) + { + CheckModel(model); + if (_goodStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(GoodBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_goodStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public GoodViewModel? ReadElement(GoodSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. GoodName: {GoodName}. Id: {Id}", + model.GoodName, model.Id); + var element = _goodStorage.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(GoodSearchModel? model) + { + _logger.LogInformation("ReadList. GoodName: {GoodName}. Id: {Id}", + model?.GoodName, model?.Id); + var list = model == null ? _goodStorage.GetFullList() : + _goodStorage.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(GoodBindingModel model) + { + CheckModel(model); + if (_goodStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(GoodBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.GoodName)) + { + throw new ArgumentNullException("Нет названия товара", nameof(model.GoodName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена товара должна быть больше 0", + nameof(model.Price)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у пользователя", + nameof(model.Price)); + } + _logger.LogInformation("Good. GoodName: {GoodName}. Price: {Price}. Id: {Id}", + model.GoodName, model.Price, model.Id); + var element = _goodStorage.GetElement(new GoodSearchModel + { + GoodName = model.GoodName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Товар с таким названием уже есть"); + } + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs new file mode 100644 index 0000000..5410cc0 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Storekeeper/OrderLogic.cs @@ -0,0 +1,124 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BuisnessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Enums; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics.Storekeeper +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) + { + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("Order. OrderId: {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; + } + + private bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) + { + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (viewModel == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (viewModel.Status + 1 != newStatus) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + model.Status = newStatus; + if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now; + else + { + model.DateImplement = viewModel.DateImplement; + } + CheckModel(model, false); + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.GoodId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у товара", nameof(model.GoodId)); + } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у пользователя", nameof(model.UserId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество товара в заказе должно быть больше 0", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } + _logger.LogInformation("Order. OrderId: {Id}. Sum: {Sum}. GoodId: {GoodId}", model.Id, model.Sum, model.GoodId); + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выполняется); + } + + public bool FinishOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Готов); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выдан); + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs new file mode 100644 index 0000000..de4e507 --- /dev/null +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/UserLogic.cs @@ -0,0 +1,128 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.BusinessLogicsContracts; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HardwareShopBusinessLogic.BusinessLogics +{ + public class UserLogic : IUserLogic + { + private readonly ILogger _logger; + private readonly IUserStorage _userStorage; + public UserLogic(ILogger logger, IUserStorage userStorage) + { + _logger = logger; + _userStorage = userStorage; + } + public bool Create(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(UserBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_userStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public UserViewModel? ReadElement(UserSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login: {Login}. Email: {Email}. Id: {Id}.", + model.Login, model.Email, model.Id); + var element = _userStorage.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(UserSearchModel? model) + { + _logger.LogInformation("ReadList. Login: {Login}. Email: {Email}. Id: {Id}.", + model?.Login, model?.Email, model?.Id); + var list = model == null ? _userStorage.GetFullList() : + _userStorage.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(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(UserBindingModel 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)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); + } + _logger.LogInformation("User. Login: {Login}. Email: {Email}. Id: {Id}", + model.Login, model.Email, model.Id); + var elementLogin = _userStorage.GetElement(new UserSearchModel + { + Login = model.Login + }); + var elementEmail = _userStorage.GetElement(new UserSearchModel + { + Email = model.Email + }); + if (elementEmail != null && elementEmail.Id != model.Id) + { + throw new InvalidOperationException("Клиент с такой почтой уже есть"); + } + if (elementLogin != null && elementLogin.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином уже есть"); + } + } + } +} diff --git a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj index b47f27c..c758485 100644 --- a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj +++ b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj @@ -6,10 +6,6 @@ enable - - - -