Order logic fix

This commit is contained in:
abazov73 2023-04-09 18:08:04 +04:00
parent 6d1a904e1c
commit a607e89ea6

View File

@ -3,6 +3,7 @@ using ConstructionCompanyContracts.BusinessLogicContracts;
using ConstructionCompanyContracts.SearchModels; using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts; using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Enums;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -56,7 +57,8 @@ namespace ConstructionCompanyBusinessLogic.BusinessLogics
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
{ {
CheckModel(model); CheckModel(model);
model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Принят; if (model.Status != OrderStatus.Неизвестен) return false;
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null) if (_orderStorage.Insert(model) == null)
{ {
_logger.LogWarning("Insert operation failed"); _logger.LogWarning("Insert operation failed");
@ -64,27 +66,48 @@ namespace ConstructionCompanyBusinessLogic.BusinessLogics
} }
return true; 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) public bool FinishOrder(OrderBindingModel model)
{ {
CheckModel(model); CheckModel(model, false);
model.Status = ConstructionCompanyDataModels.Enums.OrderStatus.Завершён; var element = _orderStorage.GetElement(new OrderSearchModel
model.DateEnd = DateTime.Now;
if (_orderStorage.Update(model) == null)
{ {
_logger.LogWarning("Update operation failed"); Id = model.Id
});
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false; 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; return true;
} }