Принятая лаба №6

This commit is contained in:
Артем Харламов 2023-05-15 11:23:37 +04:00
parent ea2c698577
commit 643cfa5b07
2 changed files with 112 additions and 107 deletions

View File

@ -25,7 +25,8 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
_logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model?.ClientId, model?.OrderStatus, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
{
@ -35,13 +36,14 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public OrderViewModel? ReadElement(OrderSearchModel? model)
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ImplementerId:{ImplementerId}. OrderStatus:{OrderStatus}. Id:{Id}", model.ImplementerId, model.OrderStatus, model.Id);
_logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model.ClientId, model.OrderStatus, model.ImplementerId, model.DateFrom, model.DateTo, model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
@ -113,11 +115,14 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
});
if (element == null)
{
throw new ArgumentNullException(nameof(element));
throw new InvalidOperationException(nameof(element));
}
model.DateCreate = element.DateCreate;
model.DishId = element.DishId;
model.DateImplement = element.DateImplement;
model.ClientId = element.ClientId;
if (!model.ImplementerId.HasValue)
model.ImplementerId = element.ImplementerId;
model.Status = element.Status;
model.Count = element.Count;
model.Sum = element.Sum;
@ -134,7 +139,7 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
return true;
}
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus);
throw new ArgumentException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
}
}
}

View File

@ -39,9 +39,8 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
if (orders == null || orders.Count == 0)
{
_logger.LogWarning("DoWork. Orders is null or empty");
return;
}
_logger.LogDebug("DoWork for {Count} orders", orders.Count);
_logger.LogDebug("DoWork for {Count} orders", orders?.Count);
foreach (var implementer in implementers)
{
Task.Run(() => WorkerWorkAsync(implementer, orders));
@ -55,11 +54,12 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
/// <param name="orders"></param>
private async Task WorkerWorkAsync(ImplementerViewModel implementer, List<OrderViewModel>? orders)
{
if (_orderLogic == null || implementer == null || orders == null)
if (_orderLogic == null || implementer == null)
{
return;
}
await RunOrderInWork(implementer);
if (orders == null || orders.Count == 0)
return;
@ -83,6 +83,8 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
{
Id = order.Id
});
// отдыхаем
await Task.Delay(implementer.Qualification * _rnd.Next(10, 100));
}
// кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex)
@ -95,8 +97,6 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
_logger.LogError(ex, "Error while do work");
throw;
}
// отдыхаем
await Task.Delay(implementer.Qualification * _rnd.Next(10, 100));
}
});
}
@ -133,7 +133,7 @@ namespace AbstractFoodOrdersBusinessLogic.BusinessLogics
Id = runOrder.Id
});
// отдыхаем
await Task.Delay(implementer.Qualification * _rnd.Next(10, 100));
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}
// заказа может не быть, просто игнорируем ошибку
catch (InvalidOperationException ex)