From 8834faa3aea6792b2c15f2563752b0006e4b66d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Tue, 28 Mar 2023 13:20:54 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BA=D0=BE=D0=B4=D0=B0=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/StoreLogic.cs | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs index b09a2a7..ea36093 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs @@ -78,7 +78,21 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics } return true; } - public bool AddManufacture(IManufactureModel manufacturemodel, StoreSearchModel model, int count) + private void SaveChanges(StoreViewModel? model) + { + if (model == null) + { + return; + } + _storeStorage.Update(new StoreBindingModel() + { + StoreName = model.StoreName, + Address = model.Address, + OpeningDate = model.OpeningDate, + Manufactures = model.Manufactures + }); + } + public bool AddManufacture(IManufactureModel manufacture, StoreSearchModel model, int count) { if (model == null) { @@ -86,39 +100,30 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics } if (count <= 0) { - throw new ArgumentNullException("Количество добавляемых изделий должно быть больше нуля"); + throw new ArgumentException("Количество добавляемых изделий должно быть больше нуля"); } + _logger.LogInformation("Adding manufacture {manufacture} in store {store}. Id = {id}", manufacture.ManufactureName, model.StoreName, model.Id); //ищем магазин StoreViewModel? selectedStore = _storeStorage.GetElement(model); if (selectedStore == null) { + _logger.LogWarning("Store with id {Id} not found", model.Id); return false; } //ищем изделие в магазине - foreach (var manufacture in selectedStore.Manufactures) + foreach (var man in selectedStore.Manufactures) { - if (manufacture.Item1.Id == manufacturemodel.Id)//если изделие уже есть в магазине, увеличиваем его количество + if (man.Item1.Id == manufacture.Id)//если изделие уже есть в магазине, увеличиваем его количество { - selectedStore.Manufactures[manufacturemodel.Id - 1] = (manufacturemodel, selectedStore.Manufactures[manufacturemodel.Id - 1].Item2 + count); - _storeStorage.Update(new StoreBindingModel() - { - StoreName = selectedStore.StoreName, - Address = selectedStore.Address, - OpeningDate = selectedStore.OpeningDate, - Manufactures = selectedStore.Manufactures - }); + selectedStore.Manufactures[manufacture.Id - 1] = (manufacture, selectedStore.Manufactures[manufacture.Id - 1].Item2 + count); + SaveChanges(selectedStore); + _logger.LogInformation("Filled store Id = {storeId} with manufacture Id = {manufactureId}. Count = {count}", model.i) return true; } } //иначе добавляем изделие в магазин в указанном количестве - selectedStore.Manufactures.Add((manufacturemodel, count)); - _storeStorage.Update(new StoreBindingModel() - { - StoreName = selectedStore.StoreName, - Address = selectedStore.Address, - OpeningDate = selectedStore.OpeningDate, - Manufactures = selectedStore.Manufactures - }); + selectedStore.Manufactures.Add((manufacture, count)); + SaveChanges(selectedStore); return true; } private void CheckModel(StoreBindingModel model, bool withParams = true) @@ -150,4 +155,4 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics } } } -} +} \ No newline at end of file