diff --git a/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs b/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs index b53bbee..f64240b 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs @@ -124,7 +124,15 @@ namespace HardwareShopRestApi.Controllers var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId }); if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false; purchase.Sum = purchase.Sum + build.Price * count; - build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count)); + if (build.BuildPurchases.ContainsKey(purchaseId)) + { + + build.BuildPurchases[purchaseId] = (build.BuildPurchases[purchaseId].Item1, build.BuildPurchases[purchaseId].Item2 + count); + } + else + { + build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count)); + } Update(new BuildBindingModel { Id = buildId, @@ -178,6 +186,47 @@ namespace HardwareShopRestApi.Controllers } } + [HttpGet] + public bool UpdateLinkPurchase(int updateBuildId, int updatePurchaseId, int count) + { + try + { + var build = GetBuild(updateBuildId);//APIClient.GetRequest($"api/build/getBuild?buildId={buildId}"); + var purchase = _purchaseLogic.ReadElement(new() { Id = updatePurchaseId }); + if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false; + purchase.Sum = purchase.Sum + build.Price * count; + if (build.BuildPurchases.ContainsKey(updatePurchaseId)) + { + if (count > build.BuildPurchases[updatePurchaseId].Item2) + { + purchase.Sum = purchase.Sum + (count - build.BuildPurchases[updatePurchaseId].Item2) * build.Price; + } + else + { + purchase.Sum = purchase.Sum - (build.BuildPurchases[updatePurchaseId].Item2 - count) * build.Price; + } + build.BuildPurchases[updatePurchaseId] = (build.BuildPurchases[updatePurchaseId].Item1, count); + } + else + { + return false; + } + Update(new BuildBindingModel + { + Id = updateBuildId, + Price = build.Price, + BuildName = build.BuildName, + BuildPurchases = build.BuildPurchases + }); + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + [HttpPost] public void DeleteBuild(BuildBindingModel model) { diff --git a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs index 7f14abb..6c9b86f 100644 --- a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs @@ -167,21 +167,7 @@ namespace HardwareShopWorkerApp.Controllers { throw new Exception($"Количество сборок в покупке не может быть ниже или равен 0"); } - //APIClient.PostRequest("api/build/linkPurchase", new { buildId, purchaseId, count }); APIClient.GetRequest($"api/build/linkPurchase?buildId={buildId}&purchaseId={purchaseId}&count={count}"); - - //var build = APIClient.GetRequest($"api/build/getBuild?buildId={buildId}"); - //var purchase = APIClient.GetRequest($"api/purchase/getpurchase?purchaseId={purchaseId}"); - //purchase.Sum = purchase.Sum + build.Price * count; - //build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count)); - //APIClient.PostRequest("api/build/update", new BuildBindingModel - //{ - // Id = buildId, - // Price = build.Price, - // BuildName = build.BuildName, - // BuildPurchases = build.BuildPurchases - //}); - //APIClient.PostRequest("api/purchase/update", purchase); Response.Redirect($"LinkPurchase?buildId={buildId}"); }