diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs index 12a4925..d6aa6cb 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CostItemStorage.cs @@ -6,6 +6,7 @@ using ElectronicsShopDataBaseImplement.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Runtime.Intrinsics.X86; using System.Text; @@ -34,9 +35,11 @@ namespace ElectronicsShopDataBaseImplement.Implements if (component == null) { return null; } - component.Update(model); + component.Update(model); context.SaveChanges(); - return component.GetViewModel; + return context.CostItems + .Include(x => x.Employee) + .FirstOrDefault(x => x.ID == component.ID)?.GetViewModel; } public CostItemViewModel? Delete(CostItemBindingModel model) @@ -47,8 +50,10 @@ namespace ElectronicsShopDataBaseImplement.Implements { context.CostItems.Remove(element); context.SaveChanges(); - return element.GetViewModel; - } + return context.CostItems + .Include(x => x.Employee) + .FirstOrDefault(x => x.ID == element.ID)?.GetViewModel; + } return null; } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs index e619f5e..6d4b734 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs @@ -119,6 +119,42 @@ namespace ElectronicsShopEmployeeApp.Controllers { Response.Redirect("CostItem"); } + [HttpGet] + public IActionResult EditCostItem(int id) { + var _costitem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={id}"); + + if (_costitem == null) { + return Redirect("/Home/CostItem"); + } + + var obj = new CostItemViewModel { + ID = _costitem.ID, + Name = _costitem.Name, + Price = _costitem.Price, + CostNum = _costitem.CostNum, + }; + + return View(obj); + } + + [HttpPost] + public void EditCostItem(string name, double price, int costNum, int _ID) { + if (APIEmployee.Employee == null) { + throw new Exception(" "); + } + if (price <= 0) { + throw new Exception(" 0"); + } + APIEmployee.PostRequest("api/employee/editcostitem", new CostItemBindingModel { + ID = _ID, + Name = name, + Price = price, + CostNum = costNum, + EmployeeID = APIEmployee.Employee.ID + }); + Response.Redirect("CostItem"); + } + [HttpGet] public IActionResult CreateProduct() { ViewBag.CostItems = APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"); @@ -126,11 +162,11 @@ namespace ElectronicsShopEmployeeApp.Controllers { } [HttpPost] - public void CreateProduct(string name, int costitem, double productprice) { + public void CreateProduct(string name, int costitem, double productprice, double price) { if (APIEmployee.Employee == null) { throw new Exception(" "); } - if (productprice <= 0) { + if (price <= 0) { throw new Exception(" 0"); } APIEmployee.PostRequest("api/employee/createproduct", new ProductBindingModel { @@ -141,6 +177,18 @@ namespace ElectronicsShopEmployeeApp.Controllers { Response.Redirect("Index"); } + [HttpGet] + public IActionResult DeleteCostItem(int id) { + var _costItem = APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={id}"); + + if (_costItem == null) { + return Redirect("/Home/Index"); + } + + APIEmployee.PostRequest("api/employee/deletecostitem", new CostItemBindingModel { ID = id }); + return RedirectToAction("CostItem"); + } + [HttpGet] public IActionResult EditProduct(int id) { var _product = APIEmployee.GetRequset($"api/main/getproduct?_productid={id}"); @@ -149,7 +197,7 @@ namespace ElectronicsShopEmployeeApp.Controllers { return Redirect("/Home/Index"); } - ViewBag.CostItems = APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"); + ViewBag.CostItems = APIEmployee.GetRequset>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee?.ID}"); var obj = new ProductViewModel { ProductName = _product.ProductName, @@ -158,31 +206,39 @@ namespace ElectronicsShopEmployeeApp.Controllers { ID = _product.ID }; - var _costitemLoad = (APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={obj.CostItemID}")); - if (_costitemLoad?.Name != ViewBag.CostItems[0].Name) { - int index = 0; + if (ViewBag.CostItems.Count != 0) { + var _costitemLoad = (APIEmployee.GetRequset($"api/employee/getcostitem?_costitemid={obj.CostItemID}")); - for (int i = 0; i < ViewBag.CostItems.Count; i++) { - if (ViewBag.CostItems[i].Name == _costitemLoad?.Name) { - index = i; - break; - } + if (_costitemLoad == null) { + return Redirect("/Home/Index"); } - var tmp = ViewBag.CostItems[0]; - ViewBag.CostItems[0] = _costitemLoad; - ViewBag.CostItems[index] = tmp; + if (_costitemLoad?.Name != ViewBag.CostItems[0].Name) { + int index = 0; + + for (int i = 0; i < ViewBag.CostItems.Count; i++) { + if (ViewBag.CostItems[i].Name == _costitemLoad?.Name) { + index = i; + break; + } + } + + var tmp = ViewBag.CostItems[0]; + ViewBag.CostItems[0] = _costitemLoad; + ViewBag.CostItems[index] = tmp; + } } + return View(obj); } [HttpPost] - public void EditProduct(string name, int costitem, double productprice, int _ID) { + public void EditProduct(string name, int costitem, double productprice, int _ID, double price) { if (APIEmployee.Employee == null) { throw new Exception(" "); } - if (productprice <= 0) { + if (price <= 0) { throw new Exception(" 0"); } APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel { diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml index b8b928e..f32a31d 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/CostItem.cshtml @@ -21,12 +21,6 @@

Создать

-

- Изменить -

-

- Удалить -

@@ -50,20 +44,24 @@ @foreach (var item in Model) { - + } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/EditCostItem.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/EditCostItem.cshtml new file mode 100644 index 0000000..9855840 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/EditCostItem.cshtml @@ -0,0 +1,39 @@ +@{ + ViewData["Title"] = "EditCostItem"; +} + +
+

Создание статьи затрат

+
+ +
+ +
+ +
+
+
+
Название статьи затрат:
+
+ +
+
+
+
Номер счета:
+
+ +
+
+
+
Затраты:
+
+ +
+
+
+
+
+ +
+
+ diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs index 2983893..0d71f41 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs @@ -93,6 +93,28 @@ namespace ElectronicsShopRestAPI.Controllers { } } + [HttpPost] + public void EditCostItem(CostItemBindingModel model) { + try { + _costItem.Update(model); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка обновления данных"); + throw; + } + } + + [HttpPost] + public void DeleteCostItem(CostItemBindingModel model) { + try { + _costItem.Delete(model); + } + catch (Exception ex) { + _logger.LogError(ex, "Ошибка удаления статьи затрат"); + throw; + } + } + [HttpPost] public void CreateProduct(ProductBindingModel model) { try {
+ @Html.DisplayFor(modelItem => item.ID) - - + + @Html.DisplayFor(modelItem => item.Name) - - + + @Html.DisplayFor(modelItem => item.EmployeeFIO) - - + + @Html.DisplayFor(modelItem => item.Price) - - + + @Html.DisplayFor(modelItem => item.CostNum) + + Изменить + Удалить