From 55dcc5a9a79b0689e557e16f2241acd5defe32c3 Mon Sep 17 00:00:00 2001 From: parap Date: Sat, 22 Apr 2023 20:55:10 +0400 Subject: [PATCH] lab 2 hard full --- Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs | 17 ++++- .../StoragesContracts/IShopStorage.cs | 1 + .../Implements/ShopStorage.cs | 68 ++++++++++++++++++- .../PizzeriaShopFileImplement/models/Shop.cs | 1 + 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs index f071a3f..fb51d68 100644 --- a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs @@ -12,10 +12,14 @@ namespace PizzeriaBusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopStorage _shopStorage; + private readonly IPizzaStorage _pizzaStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage, IPizzaStorage pizzaStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; + _pizzaStorage = pizzaStorage; } public List? 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"); diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs index ee44e2d..0c23f6b 100644 --- a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs @@ -11,6 +11,7 @@ namespace PizzeriaContracts.StoragesContracts List 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); diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs index d5ab7d3..9949785 100644 --- a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -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 + }); } }); diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs index fb23761..7ce5d9e 100644 --- a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs +++ b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs @@ -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; }