From a607e89ea6f21c59fbfb85db10f4dc5890975f6c Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Sun, 9 Apr 2023 18:08:04 +0400 Subject: [PATCH] Order logic fix --- .../BusinessLogics/OrderLogic.cs | 57 +++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/OrderLogic.cs b/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/OrderLogic.cs index fee1795..3039f34 100644 --- a/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/OrderLogic.cs +++ b/ConstructionCompany/ConstructionCompanyBusiness/BusinessLogics/OrderLogic.cs @@ -3,6 +3,7 @@ using ConstructionCompanyContracts.BusinessLogicContracts; using ConstructionCompanyContracts.SearchModels; using ConstructionCompanyContracts.StorageContracts; using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -56,7 +57,8 @@ namespace ConstructionCompanyBusinessLogic.BusinessLogics public bool CreateOrder(OrderBindingModel model) { CheckModel(model); - model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Принят; + if (model.Status != OrderStatus.Неизвестен) return false; + model.Status = OrderStatus.Принят; if (_orderStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); @@ -64,27 +66,48 @@ namespace ConstructionCompanyBusinessLogic.BusinessLogics } return true; } - public bool TakeOrderInWork(OrderBindingModel model) - { - CheckModel(model); - model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Выполняется; - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } + public bool FinishOrder(OrderBindingModel model) { - CheckModel(model); - model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Завершён; - model.DateEnd = DateTime.Now; - if (_orderStorage.Update(model) == null) + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel { - _logger.LogWarning("Update operation failed"); + Id = model.Id + }); + if (element == null) + { + _logger.LogWarning("Read operation failed"); return false; } + if (element.Status != OrderStatus.Выполняется) + { + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Заказ должен быть переведен в статус выполнения перед готовностью!"); + } + model.Status = OrderStatus.Завершён; + _orderStorage.Update(model); + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + if (element == null) + { + _logger.LogWarning("Read operation failed"); + return false; + } + if (element.Status != OrderStatus.Принят) + { + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Заказ должен быть переведен в статус принятого перед его выполнением!"); + } + model.Status = OrderStatus.Выполняется; + _orderStorage.Update(model); return true; }