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; }