diff --git a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs index 939b6a4..80230f2 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs @@ -10,8 +10,8 @@ namespace HardwareShopDatabaseImplement { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseNpgsql("Host=localhost;Port=5433;Database=Computer_Hardware_Store;Username=user;Password=12345"); - AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=Computer_Hardware_Store4;Username=postgres;Password=1234"); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/HardwareShop/HardwareShopRestApi/Controllers/PurchaseController.cs b/HardwareShop/HardwareShopRestApi/Controllers/PurchaseController.cs index 4e7bd46..06d0d52 100644 --- a/HardwareShop/HardwareShopRestApi/Controllers/PurchaseController.cs +++ b/HardwareShop/HardwareShopRestApi/Controllers/PurchaseController.cs @@ -25,6 +25,30 @@ namespace HardwareShopRestApi.Controllers _purchaseLogic = purchaseLogic; } + [HttpGet] + public Tuple>>? GetPurchaseUpdate(int purchaseId) + { + try + { + var purchase = _purchaseLogic.ReadElement(new() { Id = purchaseId }); + if (purchase == null) + return null; + var tuple = Tuple.Create(purchase, + purchase.PurchaseGoods.Select(x => Tuple.Create(new GoodViewModel + { + Id = x.Value.Item1.Id, + GoodName = x.Value.Item1.GoodName, + Price = x.Value.Item1.Price + }, x.Value.Item2)).ToList()); + return tuple; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения покупки"); + throw; + } + } + [HttpGet] public List? GetPurchases(int userId) { @@ -72,7 +96,7 @@ namespace HardwareShopRestApi.Controllers } [HttpPost] - public void UpdatePurchase(PurchaseBindingModel model) + public void UpdateStatusPurchase(PurchaseBindingModel model) { try { @@ -94,7 +118,13 @@ namespace HardwareShopRestApi.Controllers { try { - _purchaseLogic.Update(model); + for (int i = 0; i < model.PurchaseGoodsCounts.Count; i++) + { + model.PurchaseGoods.Add(model.ListPurchaseGoods[i].Id, (model.ListPurchaseGoods[i] as IGoodModel, model.PurchaseGoodsCounts[i])); + } + model.PurchaseBuilds = _purchaseLogic.ReadElement(new() { Id = model.Id }).PurchaseBuilds; + model.Sum = Calc(model); + _purchaseLogic.Update(model); } catch (Exception ex) { @@ -103,6 +133,20 @@ namespace HardwareShopRestApi.Controllers } } + private double Calc(PurchaseBindingModel purchase) + { + 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); + } + [HttpPost] public void DeletePurchase(PurchaseBindingModel model) { diff --git a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs index 2e27842..8a34cc2 100644 --- a/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopWorkerApp/Controllers/HomeController.cs @@ -352,7 +352,7 @@ namespace HardwareShopWorkerApp.Controllers } [HttpPost] - public void UpdatePurchase(int id, int status) + public void UpdateStatusPurchase(int id, int status) { if (APIClient.User == null) { @@ -366,13 +366,65 @@ namespace HardwareShopWorkerApp.Controllers { throw new Exception("Некорректный статус"); } - APIClient.PostRequest("api/purchase/updatepurchase", new PurchaseBindingModel + APIClient.PostRequest("api/purchase/UpdateStatusPurchase", new PurchaseBindingModel { Id = id, PurchaseStatus = (PurchaseStatus)status }); } + [HttpPost] + public void DeletePurchase(int purchaseId) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + if (purchaseId <= 0) + { + throw new Exception("Некорректный идентификатор"); + } + var purchase = APIClient.GetRequest($"api/purchase/getpurchase?purchaseId={purchaseId}"); + APIClient.PostRequest("api/purchase/DeletePurchase", new PurchaseBindingModel + { + Id = purchaseId, + PurchaseStatus = purchase.PurchaseStatus + }); + } + + [HttpGet] + public IActionResult UpdatePurchase(int purchaseId) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + ViewBag.Goods = APIClient.GetRequest>($"api/good/GetAllGoods"); + return View(purchaseId); + } + + [HttpPost] + public void UpdatePurchase([FromBody] PurchaseBindingModel purchaseModel) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + purchaseModel.UserId = APIClient.User.Id; + APIClient.PostRequest("api/purchase/update", purchaseModel); + } + + [HttpGet] + public Tuple>>? GetPurchaseUpdate(int purchaseId) + { + if (APIClient.User == null) + { + throw new Exception("Вы как сюда попали? Сюда вход только авторизованным"); + } + var result = APIClient.GetRequest>>?>($"api/purchase/getpurchaseupdate?purchaseId={purchaseId}"); + return result; + } + [HttpGet] public IActionResult WorkerReport() { diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/CreatePurchase.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/CreatePurchase.cshtml index bff305e..0cb7236 100644 --- a/HardwareShop/HardwareShopWorkerApp/Views/Home/CreatePurchase.cshtml +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/CreatePurchase.cshtml @@ -24,6 +24,7 @@ Товар Количество Цена + Действие @@ -93,12 +94,21 @@ saveBtn.addEventListener("click", () => { console.log('try to add purchase') + console.log(list) + if (list.length == 0) { + alert('failed add good. components are empty') + return + } let goods = [] let counts = [] list.forEach((x) => { goods.push(x.good); counts.push(parseInt(x.count)) }) + console.log(JSON.stringify({ + "Sum": parseFloat(totalSum.value), + "ListPurchaseGoods": goods, "PurchaseGoodsCounts": counts + })); $.ajax( { url: `/Home/CreatePurchase`, @@ -115,12 +125,25 @@ function reloadTable() { resultTable.innerHTML = '' let price = 0; + let count = 0; list.forEach((elem) => { + resultTable.innerHTML += `${elem.good.goodName}${elem.count}${Math.round(elem.good.price * elem.count * 100) / 100} \ +
\ + \ +
` + count++; console.log(elem); - resultTable.innerHTML += `${elem.good.goodName}${elem.count}${Math.round(elem.good.price * elem.count * 100) / 100}` price += elem.good.price * elem.count }) - totalSum.value = Math.round(price * 100) / 100 + totalSum.value = Math.round(price * 110) / 100 + console.log(totalSum.value); + } + + function deleteGood(id) { + list = list.filter(value => value.good.goodName != resultTable.rows[id].cells[0].innerText) + reloadTable() } diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/Purchases.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/Purchases.cshtml index c8acf18..e21e699 100644 --- a/HardwareShop/HardwareShopWorkerApp/Views/Home/Purchases.cshtml +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/Purchases.cshtml @@ -17,8 +17,9 @@

Покупки

Добавить - + +

@@ -117,20 +118,24 @@ { url: `/Home/DeletePurchase`, type: 'POST', - data: { id: purchase } + data: { purchaseId: purchase } } - ).done(() => window.location.href = '/Home/Purchase') + ).done(() => window.location.href = '/Home/Purchases') }) done.addEventListener("click", () => { console.log('try to delete purchase') $.ajax( { - url: `/Home/UpdatePurchase`, + url: `/Home/UpdateStatusPurchase`, type: 'POST', data: { id: purchase, status: 1 } } ).done(() => window.location.href = '/Home/Purchases') }) + update.addEventListener("click", () => { + console.log('try to update purchase') + window.location.href = '/Home/UpdatePurchase?purchaseId=' + purchase + }) }; } diff --git a/HardwareShop/HardwareShopWorkerApp/Views/Home/UpdatePurchase.cshtml b/HardwareShop/HardwareShopWorkerApp/Views/Home/UpdatePurchase.cshtml new file mode 100644 index 0000000..c150829 --- /dev/null +++ b/HardwareShop/HardwareShopWorkerApp/Views/Home/UpdatePurchase.cshtml @@ -0,0 +1,242 @@ +@using HardwareShopContracts.ViewModels +@model int +@{ + ViewData["Title"] = "UpdatePurchase"; + Layout = "~/Views/Shared/_LayoutWorker.cshtml"; +} + +
+

Редактирование покупки

+
+ +
+
+ + +
+

Товары

+
+ + +
+ + + + + + + + + + + +
ТоварКоличествоЦенаДействие
+
+ +
+
+ + + +@section Scripts + { + +} + + +@*
+
+
+

Товары

+
+
+ + + + + + + + + + + +
+ Номер + + Название товара + + Цена + + Количество +
+
+
+
+ + + + + +
+ +
+*@ \ No newline at end of file