From fe06bc28e174c85ff4185e2e3cea0e7279516205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Wed, 17 May 2023 17:14:45 +0400 Subject: [PATCH] fix linkpurchase --- .../Implements/Worker/PurchaseStorage.cs | 4 +- .../Controllers/BuildController.cs | 54 +++++++++++++------ .../Controllers/HomeController.cs | 21 +++++++- .../Views/Home/LinkPurchase.cshtml | 38 +++++++++++-- .../Views/Home/Purchases.cshtml | 2 +- 5 files changed, 94 insertions(+), 25 deletions(-) diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs index cb094d9..1d7aa8e 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs @@ -80,7 +80,9 @@ namespace HardwareShopDatabaseImplement.Implements.Worker return context.Purchases .Include(x => x.Goods) .ThenInclude(x => x.Good) - .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + .Include(x => x.Builds) + .ThenInclude(x => x.Build) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } diff --git a/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs b/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs index f64240b..dead656 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/BuildController.cs @@ -3,6 +3,7 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.BusinessLogicsContracts; using HardwareShopContracts.SearchModels; using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.ManyToMany; using HardwareShopDatabaseImplement.Models.Storekeeper; using HardwareShopDatabaseImplement.Models.Worker; using HardwareShopDataModels.Enums; @@ -122,17 +123,16 @@ namespace HardwareShopRestApi.Controllers { var build = GetBuild(buildId);//APIClient.GetRequest($"api/build/getBuild?buildId={buildId}"); var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId }); - if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false; - purchase.Sum = purchase.Sum + build.Price * count; + if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false; if (build.BuildPurchases.ContainsKey(purchaseId)) { - build.BuildPurchases[purchaseId] = (build.BuildPurchases[purchaseId].Item1, build.BuildPurchases[purchaseId].Item2 + count); + build.BuildPurchases[purchaseId] = (purchase, build.BuildPurchases[purchaseId].Item2 + count); } else { build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count)); - } + } Update(new BuildBindingModel { Id = buildId, @@ -140,7 +140,15 @@ namespace HardwareShopRestApi.Controllers BuildName = build.BuildName, BuildPurchases = build.BuildPurchases }); - return true; + purchase.Sum = Calc(purchase.Id); + _purchaseLogic.Update(new PurchaseBindingModel + { + Id = purchase.Id, + Sum = purchase.Sum, + PurchaseStatus = purchase.PurchaseStatus, + PurchaseGoods = purchase.PurchaseGoods, + }); + return true; } catch (Exception ex) { @@ -148,8 +156,21 @@ namespace HardwareShopRestApi.Controllers throw; } } + private double Calc(int purchaseId) + { + var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId }); + double price = 0; + foreach (var elem in purchase.PurchaseBuilds) + { + price += ((elem.Value.Item1?.Price ?? 0) * elem.Value.Item2); + } + foreach (var elem in purchase.PurchaseGoods) + { + price += ((elem.Value.Item1?.Price ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); + } - [HttpGet] public bool DeleteLinkPurchase(int deleteBuildId, int deletePurchaseId) { @@ -158,7 +179,6 @@ namespace HardwareShopRestApi.Controllers var build = GetBuild(deleteBuildId);//APIClient.GetRequest($"api/build/getBuild?buildId={buildId}"); var purchase = _purchaseLogic.ReadElement(new() { Id = deletePurchaseId }); if (purchase.PurchaseStatus == PurchaseStatus.Выдан) return false; - purchase.Sum = purchase.Sum - build.Price * build.BuildPurchases[deletePurchaseId].Item2; build.BuildPurchases.Remove(deletePurchaseId);//Add(purchaseId, (purchase as IPurchaseModel, count)); Update(new BuildBindingModel @@ -168,7 +188,7 @@ namespace HardwareShopRestApi.Controllers BuildName = build.BuildName, BuildPurchases = build.BuildPurchases }); - + purchase.Sum = Calc(purchase.Id); _purchaseLogic.Update(new PurchaseBindingModel { Id = purchase.Id, @@ -194,17 +214,8 @@ namespace HardwareShopRestApi.Controllers 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 @@ -218,6 +229,15 @@ namespace HardwareShopRestApi.Controllers BuildName = build.BuildName, BuildPurchases = build.BuildPurchases }); + purchase.Sum = Calc(purchase.Id); + _purchaseLogic.Update(new PurchaseBindingModel + { + Id = purchase.Id, + Sum = purchase.Sum, + PurchaseStatus = purchase.PurchaseStatus, + DatePurchase = purchase.DatePurchase, + PurchaseGoods = purchase.PurchaseGoods, + }); return true; } catch (Exception ex) diff --git a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs index 6c9b86f..2e27842 100644 --- a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs @@ -411,7 +411,7 @@ namespace HardwareShopWorkerApp.Controllers } - [HttpPost] + [HttpGet] public void DeleteLinkPurchase(int deleteBuildId, int deletePurchaseId) { if (APIClient.User == null) @@ -429,5 +429,24 @@ namespace HardwareShopWorkerApp.Controllers APIClient.GetRequest($"api/build/deleteLinkPurchase?deleteBuildId={deleteBuildId}&deletePurchaseId={deletePurchaseId}"); Response.Redirect($"LinkPurchase?buildId={deleteBuildId}"); } + + [HttpGet] + public void UpdateLinkPurchase(int updateBuildId, int updatePurchaseId, int count) + { + if (APIClient.User == null) + { + throw new Exception("Вы как суда попали? Суда вход только авторизованным"); + } + if (updateBuildId <= 0) + { + throw new Exception($"Идентификтаор сборки не может быть ниже или равен 0"); + } + if (updatePurchaseId <= 0) + { + throw new Exception($"Идентификтаор покупки не может быть ниже или равен 0"); + } + APIClient.GetRequest($"api/build/UpdateLinkPurchase?updateBuildId={updateBuildId}&updatePurchaseId={updatePurchaseId}&count={count}"); + Response.Redirect($"LinkPurchase?buildId={updateBuildId}"); + } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/LinkPurchase.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/LinkPurchase.cshtml index 710375a..2c23262 100644 --- a/HardwareShop/HardwareShopWorkerApp/Views/Home/LinkPurchase.cshtml +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/LinkPurchase.cshtml @@ -98,7 +98,7 @@