diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs index 13761bc..2b16c02 100644 --- a/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ShopLogic.cs @@ -171,27 +171,35 @@ namespace SushiBarBusinessLogic.BusinessLogics { throw new ArgumentException("Количество суши должно быть больше 0", nameof(count)); } - var freePlaces = _shopStorage.GetFullList() + var freeCount = _shopStorage.GetFullList() .Select(x => x.MaxCountSushi - x.ListSushi .Select(p => p.Value.Item2).Sum()).Sum() - count; - if (freePlaces < 0) + if (freeCount < 0) { _logger.LogInformation("AddSushi. Не удалось добавить изделия в магазины, они переполнены."); return false; } foreach (var shop in _shopStorage.GetFullList()) { - var temp = Math.Min(count, shop.MaxCountSushi - shop.ListSushi.Select(x => x.Value.Item2).Sum()); - if (temp <= 0) + int countFree = shop.MaxCountSushi - shop.ListSushi.Select(x => x.Value.Item2).Sum(); + if (countFree < count) { - continue; + if (!AddSushiInShop(new() { Id = shop.Id }, model, countFree)) + { + _logger.LogWarning("AddSushiInShop operation failed."); + return false; + } + count -= countFree; } - if (!AddSushiInShop(new() { Id = shop.Id }, model, temp)) + else { - _logger.LogWarning("При добавлении суши в магазины произошла ошибка"); - return false; + if (!AddSushiInShop(new() { Id = shop.Id }, model, count)) + { + _logger.LogWarning("AddSushiInShop operation failed."); + return false; + } + count = 0; } - count -= temp; if (count == 0) { return true;