diff --git a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs index 5f8ad8b..178f78f 100644 --- a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/ShopLogic.cs @@ -153,6 +153,11 @@ namespace SecuritySystemBusinessLogic.BusinessLogics _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name); + if (GetFreeSpace(shopElement.Id) < count) + { + throw new InvalidOperationException("В магазине не хватает места"); + } + if (shopElement.ShopSecures.TryGetValue(secure.Id, out var sameSecure)) { shopElement.ShopSecures[secure.Id] = (secure, sameSecure.Item2 + count); @@ -176,34 +181,44 @@ namespace SecuritySystemBusinessLogic.BusinessLogics return true; } - public bool SupplySecures(ISecureModel secure, int count) => throw new NotImplementedException(); - public bool SellSecures(ISecureModel model, int count) => throw new NotImplementedException(); - public bool CheckSecuresCount(ISecureModel model, int count) + public bool SupplySecures(ISecureModel secure, int count) { - int securesInShops = _shopStorage.GetFullList() - .Select(x => x.ShopSecures.Select(y => y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0).Sum()).Sum(); - return securesInShops >= count; - } - public bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count) - { - if (shopSearchModel == null) - throw new ArgumentNullException(nameof(shopSearchModel)); - - var shop = _shopStorage.GetElement(shopSearchModel); - - if (shop == null) + if (!CheckSupplySecures(count)) { - _logger.LogWarning("Required shop element not found in storage"); - return false; + throw new InvalidOperationException("Невозможно пополнить: в магазинах не хватает места"); } - int securesInShop = _shopStorage.GetFullList().Select(x => x.ShopSecures.Select(y => y.Value.Item2).Sum()).Sum(); + var shops = _shopStorage.GetFullList(); + foreach (var shop in shops) + { + int shopFreeSpace = GetFreeSpace(shop.Id); + if (shopFreeSpace > 0 && count > 0) + { + int min = Math.Min(count, shopFreeSpace); + count -= min; + SupplySecures(new ShopSearchModel { Id = shop.Id }, secure, min); + } + } - return securesInShop + count <= shop.MaxSecuresCount; + return true; + } + public bool SellSecures(ISecureModel model, int count) + { + return _shopStorage.SellSecures(model, count); } public bool CheckSupplySecures(int count) { - throw new NotImplementedException(); + return GetFreeSpace() >= count; + } + private int GetFreeSpace() + { + var shops = _shopStorage.GetFullList(); + return shops.Select(shop => shop.MaxSecuresCount - shop.ShopSecures.Select(shopSecure => shopSecure.Value.Item2).Sum()).Sum(); + } + private int GetFreeSpace(int shopId) + { + var shop = _shopStorage.GetElement(new ShopSearchModel { Id = shopId }); + return shop.MaxSecuresCount - shop.ShopSecures.Select(shopSecure => shopSecure.Value.Item2).Sum(); } } } diff --git a/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs b/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs index e4a6503..fc8c42b 100644 --- a/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/SecuritySystem/SecuritySystemContracts/BusinessLogicsContracts/IShopLogic.cs @@ -17,14 +17,6 @@ namespace SecuritySystemContracts.BusinessLogicsContracts /// bool SellSecures(ISecureModel model, int count); /// - /// Проверяет наличие определенного количества продукта суммарно по всем магазинам - /// - bool CheckSecuresCount(ISecureModel model, int count); - /// - /// Проверяет можно ли пополнить конкретный магазин продукцией в указанном количестве - /// - bool CheckSupplySecures(ShopSearchModel shopSearchModel, int count); - /// /// Проверяет можно ли распределить во все магазины продукты в указанном количестве /// bool CheckSupplySecures(int count);