lab 2 hard full
This commit is contained in:
parent
1e9c5a2ae1
commit
55dcc5a9a7
@ -12,10 +12,14 @@ namespace PizzeriaBusinessLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
private readonly IShopStorage _shopStorage;
|
||||
private readonly IPizzaStorage _pizzaStorage;
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage, IPizzaStorage pizzaStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_orderStorage = orderStorage;
|
||||
_shopStorage = shopStorage;
|
||||
_pizzaStorage = pizzaStorage;
|
||||
}
|
||||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||
{
|
||||
@ -66,6 +70,17 @@ namespace PizzeriaBusinessLogic
|
||||
model.DateImplement = DateTime.Now;
|
||||
}
|
||||
|
||||
if (newStatus == OrderStatus.Готов)
|
||||
{
|
||||
var pizza = _pizzaStorage.GetElement(new PizzaSearchModel { Id = model.PizzaId });
|
||||
|
||||
if(!_shopStorage.TryRestoreShops(pizza, model.Count))
|
||||
{
|
||||
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Incorrect count.");
|
||||
throw new ArgumentException("Количество изделий слишком велико");
|
||||
}
|
||||
}
|
||||
|
||||
if (_orderStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
|
@ -11,6 +11,7 @@ namespace PizzeriaContracts.StoragesContracts
|
||||
List<ShopViewModel> GetFilteredList(ShopSearchModel model);
|
||||
ShopViewModel? GetElement(ShopSearchModel model);
|
||||
bool TrySellPizza(IPizzaModel pizza, int count);
|
||||
bool TryRestoreShops(IPizzaModel pizza, int count);
|
||||
ShopViewModel? Insert(ShopBindingModel model);
|
||||
ShopViewModel? Update(ShopBindingModel model);
|
||||
ShopViewModel? Delete(ShopBindingModel model);
|
||||
|
@ -43,6 +43,63 @@ namespace PizzeriaFileImplement.Implements
|
||||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
|
||||
public bool TryRestoreShops(IPizzaModel pizza, int count)
|
||||
{
|
||||
int freePlaces = 0;
|
||||
|
||||
source.Shops.ForEach(x =>
|
||||
{
|
||||
int currentFreePlaces = x.Capacity;
|
||||
foreach (var pair in x.ShopPizzas)
|
||||
{
|
||||
currentFreePlaces -= pair.Value.Item2;
|
||||
}
|
||||
freePlaces += currentFreePlaces;
|
||||
});
|
||||
|
||||
if (freePlaces < count) return false;
|
||||
|
||||
source.Shops.ForEach(x =>
|
||||
{
|
||||
int currentFreePlaces = x.Capacity;
|
||||
foreach (var elem in x.ShopPizzas)
|
||||
{
|
||||
currentFreePlaces -= elem.Value.Item2;
|
||||
}
|
||||
|
||||
if(!x.ShopPizzas.TryGetValue(pizza.Id, out var check))
|
||||
{
|
||||
x.ShopPizzas.Add(pizza.Id, (pizza, 0));
|
||||
}
|
||||
|
||||
x.ShopPizzas.TryGetValue(pizza.Id, out var pair);
|
||||
|
||||
if (count >= currentFreePlaces)
|
||||
{
|
||||
count -= currentFreePlaces;
|
||||
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + currentFreePlaces);
|
||||
}
|
||||
else
|
||||
{
|
||||
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + count);
|
||||
count = 0;
|
||||
}
|
||||
|
||||
x.Update(new ShopBindingModel
|
||||
{
|
||||
Id = x.Id,
|
||||
Addres = x.Addres,
|
||||
Capacity = x.Capacity,
|
||||
OpenTime = x.OpenTime,
|
||||
ShopName = x.ShopName,
|
||||
ShopPizzas = x.ShopPizzas
|
||||
});
|
||||
});
|
||||
|
||||
source.SaveShops();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TrySellPizza(IPizzaModel pizza, int count)
|
||||
{
|
||||
int hasCount = 0;
|
||||
@ -59,7 +116,6 @@ namespace PizzeriaFileImplement.Implements
|
||||
|
||||
source.Shops.ForEach(x =>
|
||||
{
|
||||
if (count < 0) return;
|
||||
if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair))
|
||||
{
|
||||
if(count >= pair.Item2)
|
||||
@ -71,6 +127,16 @@ namespace PizzeriaFileImplement.Implements
|
||||
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 - count);
|
||||
count = 0;
|
||||
}
|
||||
|
||||
x.Update(new ShopBindingModel
|
||||
{
|
||||
Id = x.Id,
|
||||
Addres = x.Addres,
|
||||
Capacity = x.Capacity,
|
||||
OpenTime = x.OpenTime,
|
||||
ShopName = x.ShopName,
|
||||
ShopPizzas = x.ShopPizzas
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -74,6 +74,7 @@ namespace PizzeriaFileImplement.models
|
||||
ShopName = model.ShopName;
|
||||
Addres = model.Addres;
|
||||
OpenTime = model.OpenTime;
|
||||
Capacity = model.Capacity;
|
||||
Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2);
|
||||
_shopPizzas = null;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user