diff --git a/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs index 72f8fef..52732f9 100644 --- a/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs @@ -17,17 +17,73 @@ namespace ComputersShopBusinessLogic { 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); + _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"); @@ -36,40 +92,6 @@ namespace ComputersShopBusinessLogic _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } - - public bool CreateOrder(OrderBindingModel model) - { - CheckModel(model); - if(!CheckStatus(model, OrderStatus.Принят, false)) return false; - if (_orderStorage.Insert(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - return true; - } - - public bool TakeOrderInWork(OrderBindingModel model) - { - CheckModel(model); - if (!CheckStatus(model, OrderStatus.Выполняется)) return false; - return true; - } - - public bool DeliveryOrder(OrderBindingModel model) - { - CheckModel(model); - if (!CheckStatus(model, OrderStatus.Готов)) return false; - return true; - } - - public bool FinishOrder(OrderBindingModel model) - { - CheckModel(model); - if (!CheckStatus(model, OrderStatus.Выдан)) return false; - return true; - } - private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) @@ -82,35 +104,21 @@ namespace ComputersShopBusinessLogic } if (model.ComputerId < 0) { - throw new ArgumentNullException("Некорректный идентификатор компьютера", nameof(model.ComputerId)); + throw new ArgumentNullException("Некорректный идентификатор документа", + nameof(model.ComputerId)); } if (model.Count <= 0) { - throw new ArgumentNullException("Количество компьютеров в заказе должно быть больше 0", nameof(model.Count)); + throw new ArgumentNullException("Количество компьютеров в заказе должно быть больше 0", + nameof(model.Count)); } if (model.Sum <= 0) { - throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + throw new ArgumentNullException("Сумма заказа должна быть больше 0", + nameof(model.Sum)); } - _logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. ComputerId: { ComputerId}", model.Id, model.Sum, model.ComputerId); - } - - private bool CheckStatus(OrderBindingModel model, OrderStatus newstatus, bool update = true) - { - if (model.Status != newstatus - 1) - { - _logger.LogWarning("Failed to change status"); - return false; - } - model.Status = newstatus; - if(!update) return true; - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Insert operation failed"); - return false; - } - if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - return true; + _logger.LogInformation("Computer. OrderID:{Id}. Sum:{Sum}. ComputerID:{ComputerID}.}", + model.Id, model.Sum, model.ComputerId); } } } diff --git a/ComputersShop/ComputersShopView/FormComputer.Designer.cs b/ComputersShop/ComputersShopView/FormComputer.Designer.cs index fd0425a..86956da 100644 --- a/ComputersShop/ComputersShopView/FormComputer.Designer.cs +++ b/ComputersShop/ComputersShopView/FormComputer.Designer.cs @@ -67,6 +67,7 @@ // this.textBoxPrice.Location = new System.Drawing.Point(88, 38); this.textBoxPrice.Name = "textBoxPrice"; + this.textBoxPrice.ReadOnly = true; this.textBoxPrice.Size = new System.Drawing.Size(159, 23); this.textBoxPrice.TabIndex = 3; // diff --git a/ComputersShop/ComputersShopView/FormComputer.resx b/ComputersShop/ComputersShopView/FormComputer.resx index 214dda5..63454f6 100644 --- a/ComputersShop/ComputersShopView/FormComputer.resx +++ b/ComputersShop/ComputersShopView/FormComputer.resx @@ -66,4 +66,13 @@ True + + True + + + True + + + True + \ No newline at end of file