This commit is contained in:
Игорь Гордеев 2024-05-30 16:08:09 +04:00
commit c66b4bc56b
5 changed files with 155 additions and 35 deletions

View File

@ -6,6 +6,7 @@ using ElectronicsShopDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Runtime.Intrinsics.X86; using System.Runtime.Intrinsics.X86;
using System.Text; using System.Text;
@ -36,7 +37,9 @@ namespace ElectronicsShopDataBaseImplement.Implements
} }
component.Update(model); component.Update(model);
context.SaveChanges(); context.SaveChanges();
return component.GetViewModel; return context.CostItems
.Include(x => x.Employee)
.FirstOrDefault(x => x.ID == component.ID)?.GetViewModel;
} }
public CostItemViewModel? Delete(CostItemBindingModel model) public CostItemViewModel? Delete(CostItemBindingModel model)
@ -47,7 +50,9 @@ namespace ElectronicsShopDataBaseImplement.Implements
{ {
context.CostItems.Remove(element); context.CostItems.Remove(element);
context.SaveChanges(); context.SaveChanges();
return element.GetViewModel; return context.CostItems
.Include(x => x.Employee)
.FirstOrDefault(x => x.ID == element.ID)?.GetViewModel;
} }
return null; return null;
} }

View File

@ -119,6 +119,42 @@ namespace ElectronicsShopEmployeeApp.Controllers {
Response.Redirect("CostItem"); Response.Redirect("CostItem");
} }
[HttpGet]
public IActionResult EditCostItem(int id) {
var _costitem = APIEmployee.GetRequset<CostItemViewModel>($"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] [HttpGet]
public IActionResult CreateProduct() { public IActionResult CreateProduct() {
ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"); ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}");
@ -126,11 +162,11 @@ namespace ElectronicsShopEmployeeApp.Controllers {
} }
[HttpPost] [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) { if (APIEmployee.Employee == null) {
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ"); throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ");
} }
if (productprice <= 0) { if (price <= 0) {
throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0"); throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0");
} }
APIEmployee.PostRequest("api/employee/createproduct", new ProductBindingModel { APIEmployee.PostRequest("api/employee/createproduct", new ProductBindingModel {
@ -141,6 +177,18 @@ namespace ElectronicsShopEmployeeApp.Controllers {
Response.Redirect("Index"); Response.Redirect("Index");
} }
[HttpGet]
public IActionResult DeleteCostItem(int id) {
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"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] [HttpGet]
public IActionResult EditProduct(int id) { public IActionResult EditProduct(int id) {
var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={id}"); var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={id}");
@ -149,7 +197,7 @@ namespace ElectronicsShopEmployeeApp.Controllers {
return Redirect("/Home/Index"); return Redirect("/Home/Index");
} }
ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}"); ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee?.ID}");
var obj = new ProductViewModel { var obj = new ProductViewModel {
ProductName = _product.ProductName, ProductName = _product.ProductName,
@ -158,8 +206,14 @@ namespace ElectronicsShopEmployeeApp.Controllers {
ID = _product.ID ID = _product.ID
}; };
if (ViewBag.CostItems.Count != 0) {
var _costitemLoad = (APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={obj.CostItemID}")); var _costitemLoad = (APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={obj.CostItemID}"));
if (_costitemLoad == null) {
return Redirect("/Home/Index");
}
if (_costitemLoad?.Name != ViewBag.CostItems[0].Name) { if (_costitemLoad?.Name != ViewBag.CostItems[0].Name) {
int index = 0; int index = 0;
@ -174,15 +228,17 @@ namespace ElectronicsShopEmployeeApp.Controllers {
ViewBag.CostItems[0] = _costitemLoad; ViewBag.CostItems[0] = _costitemLoad;
ViewBag.CostItems[index] = tmp; ViewBag.CostItems[index] = tmp;
} }
}
return View(obj); return View(obj);
} }
[HttpPost] [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) { if (APIEmployee.Employee == null) {
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ"); throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ");
} }
if (productprice <= 0) { if (price <= 0) {
throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0"); throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0");
} }
APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel { APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel {

View File

@ -21,12 +21,6 @@
<p> <p>
<a asp-action="CreateCostItem">Создать</a> <a asp-action="CreateCostItem">Создать</a>
</p> </p>
<p>
<a asp-action="UpdateCostItem">Изменить</a>
</p>
<p>
<a asp-action="DeleteCostItem">Удалить</a>
</p>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -50,20 +44,24 @@
<tbody> <tbody>
@foreach (var item in Model) { @foreach (var item in Model) {
<tr> <tr>
<td> <th>
@Html.DisplayFor(modelItem => item.ID) @Html.DisplayFor(modelItem => item.ID)
</td> </th>
<td> <th>
@Html.DisplayFor(modelItem => item.Name) @Html.DisplayFor(modelItem => item.Name)
</td> </th>
<td> <th>
@Html.DisplayFor(modelItem => item.EmployeeFIO) @Html.DisplayFor(modelItem => item.EmployeeFIO)
</td> </th>
<td> <th>
@Html.DisplayFor(modelItem => item.Price) @Html.DisplayFor(modelItem => item.Price)
</td> </th>
<td> <th>
@Html.DisplayFor(modelItem => item.CostNum) @Html.DisplayFor(modelItem => item.CostNum)
</th>
<td>
<a class="btn btn-primary btn-sm" asp-action="EditCostItem" asp-route-ID="@item.ID">Изменить</a>
<a class="btn btn-primary btn-sm" asp-action="DeleteCostItem" asp-route-ID="@item.ID">Удалить</a>
</td> </td>
</tr> </tr>
} }

View File

@ -0,0 +1,39 @@
@{
ViewData["Title"] = "EditCostItem";
}
<div class="text-center">
<h2 class="display-4">Создание статьи затрат</h2>
</div>
<form method="post">
<div class="row">
<label class="col-4">ID:</label>
<div class="col-8">
<input id="_ID" class="form-control" type="text" name="_ID" value="@Model.ID" readonly />
</div>
</div>
<div class="row">
<div class="col-4">Название статьи затрат:</div>
<div class="col-8">
<input id="name" type="text" name="name" value="@Model.Name" />
</div>
</div>
<div class="row">
<div class="col-4">Номер счета:</div>
<div class="col-8">
<input id="costnum" type="text" name="costnum" value="@Model.CostNum" />
</div>
</div>
<div class="row">
<div class="col-4">Затраты:</div>
<div class="col-8">
<input id="price" type="text" name="price" value="@Model.Price" />
</div>
</div>
<div class="row">
<div class="col-4"></div>
<div class="col-8">
<input type="submit" value="Сохранить" class="btn btn-primary" />
</div>
</div>
</form>

View File

@ -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] [HttpPost]
public void CreateProduct(ProductBindingModel model) { public void CreateProduct(ProductBindingModel model) {
try { try {