diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComponentLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..86dd084 --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,114 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopBusinessLogic.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/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComputerLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComputerLogic.cs new file mode 100644 index 0000000..8eac11e --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComputerLogic.cs @@ -0,0 +1,119 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopBusinessLogic.BusinessLogics +{ + public class ComputerLogic : IComputerLogic + { + private readonly ILogger _logger; + private readonly IComputerStorage _computerStorage; + + public ComputerLogic(ILogger logger, IComputerStorage computerStorage) + { + _logger = logger; + _computerStorage = computerStorage; + } + + public List? ReadList(ComputerSearchModel? model) + { + _logger.LogInformation("ReadList. ComputerName:{ComputerName}.Id:{ Id}", model?.ComputerName, model?.Id); + var list = model == null ? _computerStorage.GetFullList() : _computerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public ComputerViewModel? ReadElement(ComputerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComputerName:{ComputerName}.Id:{ Id}", model.ComputerName, model.Id); + var element = _computerStorage.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(ComputerBindingModel model) + { + CheckModel(model); + if (_computerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ComputerBindingModel model) + { + CheckModel(model); + if (_computerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ComputerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_computerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ComputerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComputerName)) + { + throw new ArgumentNullException("Нет названия компьютера", nameof(model.ComputerName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена компьютера должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Computer. ComputerName:{ComputerName}.Cost:{ Cost}. Id: { Id}", model.ComputerName, model.Price, model.Id); + var element = _computerStorage.GetElement(new ComputerSearchModel + { + ComputerName = model.ComputerName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компьютер с таким названием уже есть"); + } + } + } +} diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..17fc279 --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,124 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopBusinessLogic.BusinessLogics +{ + 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 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("Update 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); + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool DeliveryOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выдан); + } + + public bool FinishOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Готов); + } + public bool TakeOrderInWork(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выполняется); + } + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. 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 void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.ComputerId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор документа", + nameof(model.ComputerId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество компьютеров в заказе должно быть больше 0", + nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", + nameof(model.Sum)); + } + _logger.LogInformation("Computer. OrderID:{Id}. Sum:{Sum}. ComputerID:{ComputerID}.}", + model.Id, model.Sum, model.ComputerId); + } + } +} diff --git a/ComputersShop/ComputersShopBusinessLogic/ComponentLogic.cs b/ComputersShop/ComputersShopBusinessLogic/ComponentLogic.cs deleted file mode 100644 index ea37014..0000000 --- a/ComputersShop/ComputersShopBusinessLogic/ComponentLogic.cs +++ /dev/null @@ -1,114 +0,0 @@ -using ComputersShopContracts.BindingModels; -using ComputersShopContracts.BusinessLogicContracts; -using ComputersShopContracts.SearchModels; -using ComputersShopContracts.StoragesContracts; -using ComputersShopContracts.ViewModels; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ComputersShopBusinessLogic -{ - 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/ComputersShop/ComputersShopBusinessLogic/ComputerLogic.cs b/ComputersShop/ComputersShopBusinessLogic/ComputerLogic.cs deleted file mode 100644 index dfd2e92..0000000 --- a/ComputersShop/ComputersShopBusinessLogic/ComputerLogic.cs +++ /dev/null @@ -1,119 +0,0 @@ -using ComputersShopContracts.BindingModels; -using ComputersShopContracts.BusinessLogicContracts; -using ComputersShopContracts.SearchModels; -using ComputersShopContracts.StoragesContracts; -using ComputersShopContracts.ViewModels; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ComputersShopBusinessLogic -{ - public class ComputerLogic : IComputerLogic - { - private readonly ILogger _logger; - private readonly IComputerStorage _computerStorage; - - public ComputerLogic(ILogger logger, IComputerStorage computerStorage) - { - _logger = logger; - _computerStorage = computerStorage; - } - - public List? ReadList(ComputerSearchModel? model) - { - _logger.LogInformation("ReadList. ComputerName:{ComputerName}.Id:{ Id}", model?.ComputerName, model?.Id); - var list = model == null ? _computerStorage.GetFullList() : _computerStorage.GetFilteredList(model); - if (list == null) - { - _logger.LogWarning("ReadList return null list"); - return null; - } - _logger.LogInformation("ReadList. Count:{Count}", list.Count); - return list; - } - - public ComputerViewModel? ReadElement(ComputerSearchModel model) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - _logger.LogInformation("ReadElement. ComputerName:{ComputerName}.Id:{ Id}", model.ComputerName, model.Id); - var element = _computerStorage.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(ComputerBindingModel model) - { - CheckModel(model); - if (_computerStorage.Insert(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - return true; - } - - public bool Update(ComputerBindingModel model) - { - CheckModel(model); - if (_computerStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } - - public bool Delete(ComputerBindingModel model) - { - CheckModel(model, false); - _logger.LogInformation("Delete. Id:{Id}", model.Id); - if (_computerStorage.Delete(model) == null) - { - _logger.LogWarning("Delete operation failed"); - return false; - } - return true; - } - - private void CheckModel(ComputerBindingModel model, bool withParams = true) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - if (!withParams) - { - return; - } - if (string.IsNullOrEmpty(model.ComputerName)) - { - throw new ArgumentNullException("Нет названия компьютера", nameof(model.ComputerName)); - } - if (model.Price <= 0) - { - throw new ArgumentNullException("Цена компьютера должна быть больше 0", nameof(model.Price)); - } - _logger.LogInformation("Computer. ComputerName:{ComputerName}.Cost:{ Cost}. Id: { Id}", model.ComputerName, model.Price, model.Id); - var element = _computerStorage.GetElement(new ComputerSearchModel - { - ComputerName = model.ComputerName - }); - if (element != null && element.Id != model.Id) - { - throw new InvalidOperationException("Компьютер с таким названием уже есть"); - } - } - } -} diff --git a/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs deleted file mode 100644 index 52732f9..0000000 --- a/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs +++ /dev/null @@ -1,124 +0,0 @@ -using ComputersShopContracts.BindingModels; -using ComputersShopContracts.BusinessLogicContracts; -using ComputersShopContracts.SearchModels; -using ComputersShopContracts.StoragesContracts; -using ComputersShopContracts.ViewModels; -using ComputersShopDataModels.Enums; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ComputersShopBusinessLogic -{ - 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 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("Update 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); - if (_orderStorage.Update(model) == null) - { - model.Status--; - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } - public bool DeliveryOrder(OrderBindingModel model) - { - return StatusUpdate(model, OrderStatus.Выдан); - } - - public bool FinishOrder(OrderBindingModel model) - { - return StatusUpdate(model, OrderStatus.Готов); - } - public bool TakeOrderInWork(OrderBindingModel model) - { - return StatusUpdate(model, OrderStatus.Выполняется); - } - public List? ReadList(OrderSearchModel? model) - { - _logger.LogInformation("ReadList. 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 void CheckModel(OrderBindingModel model, bool withParams = true) - { - if (model == null) - { - throw new ArgumentNullException(nameof(model)); - } - if (!withParams) - { - return; - } - if (model.ComputerId < 0) - { - throw new ArgumentNullException("Некорректный идентификатор документа", - nameof(model.ComputerId)); - } - if (model.Count <= 0) - { - throw new ArgumentNullException("Количество компьютеров в заказе должно быть больше 0", - nameof(model.Count)); - } - if (model.Sum <= 0) - { - throw new ArgumentNullException("Сумма заказа должна быть больше 0", - nameof(model.Sum)); - } - _logger.LogInformation("Computer. OrderID:{Id}. Sum:{Sum}. ComputerID:{ComputerID}.}", - model.Id, model.Sum, model.ComputerId); - } - } -} diff --git a/ComputersShop/ComputersShopView/FormComputer.resx b/ComputersShop/ComputersShopView/FormComputer.resx index 63454f6..214dda5 100644 --- a/ComputersShop/ComputersShopView/FormComputer.resx +++ b/ComputersShop/ComputersShopView/FormComputer.resx @@ -66,13 +66,4 @@ True - - True - - - True - - - True - \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index 825c727..a93980f 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -1,4 +1,4 @@ -using ComputersShopBusinessLogic; +using ComputersShopBusinessLogic.BusinessLogics; using ComputersShopContracts.BusinessLogicContracts; using ComputersShopContracts.StoragesContracts; using ComputersShopListImplement.Implements; @@ -9,7 +9,7 @@ using OrdersShopListImplement.Implements; namespace ComputersShopView { - internal static class Program + internal static class Program { private static ServiceProvider? _serviceProvider; public static ServiceProvider? ServiceProvider => _serviceProvider;