From d063e4323626ad051a91965cb0223d18f923efb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=A4=D0=B5=D0=B4=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Sat, 1 Jun 2024 00:42:46 +0400 Subject: [PATCH] Order delete/edit --- .../BusinessLogic/OrderLogic.cs | 18 +++++++ .../BusinessLogicContracts/IOrderLogic.cs | 6 ++- .../StorageContracts/IOrderStorage.cs | 5 +- .../Implements/OrderStorage.cs | 38 ++++++++------ .../Models/Order.cs | 6 +++ .../Controllers/HomeController.cs | 6 --- .../Controllers/MainController.cs | 52 ++++++++++++++++++- .../ElectronicsShopShopClientApp/APIClient.cs | 18 +++++++ .../Controllers/HomeController.cs | 29 +++++++++-- .../Views/Home/Index.cshtml | 4 +- .../Views/Home/OrderView.cshtml | 30 ++++++++--- .../Views/Home/Orders.cshtml | 4 +- 12 files changed, 177 insertions(+), 39 deletions(-) diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs index f961bbc..0b40f86 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -39,6 +39,24 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return true; } + public bool Delete(OrderBindingModel model) { + CheckModel(model, false); + _logger.LogInformation($"Delete. ID:{model.ID}"); + if (_storage.Delete(model) == null) { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public bool DeleteProduct(OrderBindingModel model) { + if (_storage.DeleteProduct(model) == null) { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public List? ReadList(OrderSearchModel? model) { _logger.LogInformation($"ReadList:ID:{model?.ID}"); diff --git a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs index fad4955..9c731c8 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IOrderLogic.cs @@ -18,5 +18,9 @@ namespace ElectronicsShopContracts.BusinessLogicContracts bool CreateOrder(OrderBindingModel model); bool Update(OrderBindingModel model); - } + bool Delete(OrderBindingModel model); + + bool DeleteProduct(OrderBindingModel model); + + } } diff --git a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IOrderStorage.cs b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IOrderStorage.cs index 6a5bff3..06fab76 100644 --- a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IOrderStorage.cs +++ b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IOrderStorage.cs @@ -17,5 +17,8 @@ namespace ElectronicsShopContracts.StorageContracts OrderViewModel? GetElement(OrderSearchModel model); OrderViewModel? Insert(OrderBindingModel model); OrderViewModel? Update(OrderBindingModel model); - } + OrderViewModel? Delete(OrderBindingModel model); + OrderViewModel? DeleteProduct(OrderBindingModel model); + + } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs index 8d93598..db2be2e 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/OrderStorage.cs @@ -4,6 +4,7 @@ using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataBaseImplement.Models; using Microsoft.EntityFrameworkCore; +using System.Xml.Linq; namespace ElectronicsShopDataBaseImplement.Implements @@ -26,19 +27,27 @@ namespace ElectronicsShopDataBaseImplement.Implements public OrderViewModel? Update(OrderBindingModel model) { using var context = new Database(); using var transcation = context.Database.BeginTransaction(); - try { - var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID); - if (order == null) { - return null; - } - order.UpdateProducts(context, model); - transcation.Commit(); - return order.GetViewModel; - } - catch { - transcation.Rollback(); - throw; - } + + var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID); + if (order == null) { + return null; + } + order.UpdateProducts(context, model); + transcation.Commit(); + return order.GetViewModel; + } + + public OrderViewModel? DeleteProduct(OrderBindingModel model) { + using var context = new Database(); + using var transcation = context.Database.BeginTransaction(); + + var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID); + if (order == null) { + return null; + } + order.DeleteProducts(context, model); + transcation.Commit(); + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) @@ -105,6 +114,5 @@ namespace ElectronicsShopDataBaseImplement.Implements .ThenInclude(x => x._product).ToList() .Select(x => x.GetViewModel).ToList(); } - } -} +} \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs index bda47a0..f80844e 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs @@ -97,5 +97,11 @@ namespace ElectronicsShopDataBaseImplement.Models } _productList = null; } + + public void DeleteProducts(Database context, OrderBindingModel model) { + var orderProducts = context.OrderProducts.Where(rec => rec.OrderID == model.ID).ToList(); + context.OrderProducts.RemoveRange(orderProducts.Where(rec => !model.ProductList.ContainsKey(rec.ProductID))); + context.SaveChanges(); + } } } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs index 6d4b734..6e8c886 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs @@ -252,12 +252,6 @@ namespace ElectronicsShopEmployeeApp.Controllers { [HttpGet] public IActionResult DeleteProduct(int id) { - var _product = APIEmployee.GetRequset($"api/main/getproduct?_productid={id}"); - - if (_product == null) { - return Redirect("/Home/Index"); - } - APIEmployee.PostRequest("api/employee/deleteproduct", new ProductBindingModel { ID = id }); diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs index 88a33cf..779cffc 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs @@ -83,6 +83,17 @@ namespace ElectronicsShopRestAPI.Controllers { } } + [HttpPost] + public void DeleteOrders(OrderBindingModel model) { + try { + _order.Delete(model); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка удаления заказа"); + throw; + } + } + [HttpPost] public void CreateOrder(OrderBindingModel model) { try { @@ -147,7 +158,8 @@ namespace ElectronicsShopRestAPI.Controllers { ID = orderid, ClientID = view.ClientID, DateCreate = view.DateCreate, - ProductList = _productlist + ProductList = _productlist, + //sum }; var operationResult = _order.Update(model); if (!operationResult) { @@ -159,5 +171,43 @@ namespace ElectronicsShopRestAPI.Controllers { throw; } } + + [HttpPost] + public void DeleteProductOrder(List jslist) { + + int _orderId = JsonConvert.DeserializeObject(jslist[0]); + int _productId = JsonConvert.DeserializeObject(jslist[1]); + + var view = _order.ReadElement(new OrderSearchModel { ID = _orderId }); + if (view == null) { + _logger.LogError("Ошибка получения данных"); + return; + } + + _productlist = view.ProductList; + foreach (var item in _productlist) { + if (item.Key == _productId) { + _productlist.Remove(_productId); + } + } + + try { + var model = new OrderBindingModel { + ID = _orderId, + ClientID = view.ClientID, + DateCreate = view.DateCreate, + ProductList = _productlist + //sum + }; + var operationResult = _order.DeleteProduct(model); + if (!operationResult) { + throw new Exception("Ошибка при сохранении, дополнительная информация в логах"); + } + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка"); + throw; + } + } } } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs b/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs index a1e3c8e..e7d75d4 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/APIClient.cs @@ -40,6 +40,24 @@ namespace ElectronicsShopUserApp { } } + public static void PostRequestStr(string requstUrl, int _orderId, int _productId) { + var list = new List() { + JsonConvert.SerializeObject(_orderId), + JsonConvert.SerializeObject(_productId), + }; + + var json = JsonConvert.SerializeObject(list); + + var data = new StringContent(json, Encoding.UTF8, "application/json"); + + var response = _client.PostAsync(requstUrl, data); + + var result = response.Result.Content.ReadAsStringAsync().Result; + if (!response.Result.IsSuccessStatusCode) { + throw new Exception(result); + } + } + public static void ListPostRequest(string requstUrl, T model, int count, int orderID) { var list = new List() { JsonConvert.SerializeObject(model), diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs index 3ef23d6..6726a78 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Controllers/HomeController.cs @@ -15,6 +15,7 @@ namespace ElectronicsShopUserApp.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private Dictionary _productList; + public int Id; //private readonly IOrderLogic _order; public HomeController(ILogger logger/*, IOrderLogic orderLogic*/) { @@ -122,11 +123,21 @@ namespace ElectronicsShopUserApp.Controllers { } [HttpGet] + public IActionResult DeleteOrder(int id) { + APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id }); + return RedirectToAction("Orders"); + } + + [HttpGet] public IActionResult OrderView() { if (APIClient.Client == null) { return Redirect("~/Home/Enter"); } var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); + if (view != null) { + Id = view.ID; + } + var products = APIClient.GetRequset>>($"api/main/getorderproducts?_orderid={view?.ID}"); foreach (var pr in products) { @@ -135,15 +146,27 @@ namespace ElectronicsShopUserApp.Controllers { _productList.Add(product.ID, (product, count)); } - return View(_productList); + (int, Dictionary) tuple = (Id, _productList); + return View(tuple); } [HttpPost] - public void OrderView(int sum) { + public void OrderView(int sum, int id) { + if (sum <= 0) { + APIClient.PostRequest($"api/main/deleteorders", new OrderBindingModel { ID = id}); + } Response.Redirect("Orders"); } - [HttpGet] + [HttpGet] + public IActionResult DeleteProductOrder(int id) { + var view = APIClient.GetRequset($"api/main/getorder?_clientid={APIClient.Client?.ID}"); + APIClient.PostRequestStr($"api/main/deleteproductorder", view.ID, id); + + return RedirectToAction("OrderView"); + } + + [HttpGet] public IActionResult AddProduct() { ViewBag.Products = APIClient.GetRequset>($"api/main/getproducts"); return View(); diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Index.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Index.cshtml index 5dd8627..3464ddf 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Index.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Index.cshtml @@ -33,7 +33,7 @@ @foreach (var item in Model) { - + @Html.DisplayFor(modelItem => item.ID) @@ -43,7 +43,7 @@ @Html.DisplayFor(modelItem => item.Sum) - + } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml index 33dae27..01445ea 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/OrderView.cshtml @@ -1,7 +1,7 @@ @using ElectronicsShopContracts.ViewModels @using ElectronicsShopDataModels.Models -@model Dictionary +@model (int, Dictionary) @{ ViewData["Title"] = "OrderView"; @@ -11,7 +11,15 @@

Создание корзины

+ +
+
+
+
+ +
+
Сумма:
@@ -25,6 +33,9 @@ + @@ -37,11 +48,14 @@ - @foreach (var item in Model) { + @foreach (var item in Model.Item2) { - + + - + } diff --git a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Orders.cshtml b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Orders.cshtml index b633899..0654f8a 100644 --- a/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Orders.cshtml +++ b/ElectronicsShop/ElectronicsShopShopClientApp/Views/Home/Orders.cshtml @@ -48,8 +48,8 @@ @Html.DisplayFor(modelItem => item.Sum) }
+ Номер + Продукт
- @Html.DisplayFor(modelItem => item.Value.Item1.ProductName) - + @Html.DisplayFor(modelItem => item.Key) + + @Html.DisplayFor(modelItem => item.Value.Item1.ProductName) + @Html.DisplayFor(modelItem => item.Value.Item2) @@ -51,9 +65,9 @@ @Html.DisplayFor(modelItem => item.Value.Item1.Price) - Удалить - + Удалить +
- Изменить - Удалить + Изменить + Удалить