From d4b269564b9ddbc31aa040b33280ba8412e4de49 Mon Sep 17 00:00:00 2001
From: parap <parap123456789@mail.ru>
Date: Sun, 23 Apr 2023 20:52:05 +0400
Subject: [PATCH] refactoring

---
 Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs  | 41 +++++++++++--
 Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs   |  1 +
 .../StoragesContracts/IShopStorage.cs         |  1 -
 .../Implements/ShopStorage.cs                 | 58 -------------------
 4 files changed, 38 insertions(+), 63 deletions(-)

diff --git a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs
index fb51d68..66ca639 100644
--- a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs
+++ b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs
@@ -12,13 +12,13 @@ namespace PizzeriaBusinessLogic
     {
         private readonly ILogger _logger;
         private readonly IOrderStorage _orderStorage;
-        private readonly IShopStorage _shopStorage;
+        private readonly IShopLogic _shopLogic;
         private readonly IPizzaStorage _pizzaStorage;
-        public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage, IPizzaStorage pizzaStorage)
+        public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IPizzaStorage pizzaStorage)
         {
             _logger = logger;
             _orderStorage = orderStorage;
-            _shopStorage = shopStorage;
+            _shopLogic = shopLogic;
             _pizzaStorage = pizzaStorage;
         }
         public List<OrderViewModel>? ReadList(OrderSearchModel? model)
@@ -74,7 +74,7 @@ namespace PizzeriaBusinessLogic
             {
                 var pizza = _pizzaStorage.GetElement(new PizzaSearchModel { Id = model.PizzaId });
 
-                if(!_shopStorage.TryRestoreShops(pizza, model.Count))
+                if(!TryRestoreShops(pizza, model.Count))
                 {
                     _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Incorrect count.");
                     throw new ArgumentException("Количество изделий слишком велико");
@@ -124,5 +124,38 @@ namespace PizzeriaBusinessLogic
             _logger.LogInformation("Sum:{ Sum}. Id: { Id}", model.Sum, model.Id);
         }
 
+        private bool TryRestoreShops(IPizzaModel pizza, int count)
+        {
+            int freePlaces = 0;
+
+            _shopLogic.ReadList(null).ForEach(x =>
+            {
+                int currentFreePlaces = x.Capacity;
+                foreach (var pair in x.ShopPizzas)
+                {
+                    currentFreePlaces -= pair.Value.Item2;
+                }
+                freePlaces += currentFreePlaces;
+            });
+
+            if (freePlaces < count) return false;
+
+            _shopLogic.ReadList(null).ForEach(x =>
+            {
+                if (count <= 0) return;
+
+                int currentFreePlaces = x.Capacity;
+                foreach (var elem in x.ShopPizzas)
+                {
+                    currentFreePlaces -= elem.Value.Item2;
+                }
+
+                _shopLogic.AddPizza(new ShopSearchModel { Id = x.Id }, pizza, Math.Min(currentFreePlaces, count));
+                count -= currentFreePlaces;
+            });
+
+            return true;
+        }
+
     }
 }
diff --git a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs
index 52c74e0..bf74f7a 100644
--- a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs
+++ b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs
@@ -141,6 +141,7 @@ namespace PizzeriaBusinessLogic
                 ShopName = model.ShopName,
                 OpenTime = model.OpenTime,
                 Addres = model.Addres,
+                Capacity = model.Capacity,
                 ShopPizzas = model.ShopPizzas,
             });
 
diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs
index 0c23f6b..ee44e2d 100644
--- a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs
+++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs
@@ -11,7 +11,6 @@ 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);
diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs
index 9949785..20080cf 100644
--- a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs
+++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs
@@ -42,64 +42,6 @@ namespace PizzeriaFileImplement.Implements
                 (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
                 (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;