Update / Delete

This commit is contained in:
Илья Федотов 2024-05-29 21:21:17 +04:00
parent 9b14abf7e3
commit 48024dbc5e
9 changed files with 159 additions and 16 deletions

View File

@ -12,7 +12,7 @@ namespace ElectronicsShopDataBaseImplement
optionsBuilder)
{
if (optionsBuilder.IsConfigured == false) {
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-E2VPEN3\SQLEXPRESS;Initial Catalog=ElectronicsShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-O0N00SH\SQLEXPRESS;Initial Catalog=ElectronicsShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}

View File

@ -35,7 +35,9 @@ namespace ElectronicsShopDataBaseImplement.Implements
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
return context.Products
.Include(x => x.CostItem)
.FirstOrDefault(x => x.ID == model.ID)?.GetViewModel;
}
public ProductViewModel? Delete(ProductBindingModel model)
@ -58,8 +60,10 @@ namespace ElectronicsShopDataBaseImplement.Implements
return null;
}
using var context = new Database();
return context.Products.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) &&
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
return context.Products
.Include(x => x.CostItem)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) ||
(model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)

View File

@ -2,6 +2,7 @@ using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopEmployeeApp.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Diagnostics;
namespace ElectronicsShopEmployeeApp.Controllers {
@ -140,6 +141,61 @@ namespace ElectronicsShopEmployeeApp.Controllers {
Response.Redirect("Index");
}
[HttpGet]
public IActionResult EditProduct(int id) {
var _product = APIEmployee.GetRequset<ProductViewModel>($"api/main/getproduct?_productid={id}");
if (_product == null) {
return Redirect("/Home/Index");
}
ViewBag.CostItems = APIEmployee.GetRequset<List<CostItemViewModel>>($"api/employee/getcostitems?_employeeid={APIEmployee.Employee.ID}");
var obj = new ProductViewModel {
ProductName = _product.ProductName,
CostItemID = _product.CostItemID,
Price = _product.Price,
ID = _product.ID
};
var _costitemLoad = (APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={obj.CostItemID}"));
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) {
if (APIEmployee.Employee == null) {
throw new Exception("Ňîëüęî äë˙ ŕâňîđčçîâŕííűő");
}
if (productprice <= 0) {
throw new Exception("Ńňîčěîńňü ňîâŕđŕ äîëćíŕ áűňü áîëüřĺ 0");
}
APIEmployee.PostRequest("api/employee/editproduct", new ProductBindingModel {
ID = _ID,
CostItemID = costitem,
ProductName = name,
Price = Calc(costitem, productprice)
});
Response.Redirect("Index");
}
[HttpPost]
public double Calc(int costitem, double productprice) {
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={costitem}");

View File

@ -25,7 +25,7 @@
<a asp-action="UpdateCostItem">Изменить</a>
</p>
<p>
<a asp-action="CreateCostItem">Удалить</a>
<a asp-action="DeleteCostItem">Удалить</a>
</p>
<table class="table">
<thead>

View File

@ -27,7 +27,7 @@
<div class="row">
<div class="col-4"></div>
<div class="col-8">
<input type="submit" value="Создать" class="btn btn-outline-primary"/>
<input type="submit" value="Создать" class="btn btn-primary"/>
</div>
</div>
</form>

View File

@ -0,0 +1,74 @@
@using ElectronicsShopContracts.ViewModels
@model ProductViewModel
@{
ViewData["Title"] = "EditProduct";
}
<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.ProductName" />
</div>
</div>
<div class="row">
<div class="col-4">Статья затрат:</div>
<div class="col-8">
<select id="costitem" name="costitem" class="form-control" asp-items="@(new SelectList(ViewBag.CostItems, "ID", "Name"))"></select>
</div>
</div>
<div class="row">
<div class="col-4">Стоимость товара:</div>
<div class="col-8">
<input id="productprice" type="text" name="productprice" value="@Model.Price" />
</div>
</div>
<div class="row">
<div class="col-4">С учетом затрат:</div>
<div class="col-8">
<input id="price" type="text" name="price" readonly />
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4">
<input type="submit" value="Сохранить" class="btn btn-primary" />
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
</form>
<script>
$('#costitem').on('change', function () {
check();
});
$('#productprice').on('change', function () {
check();
});
function check() {
var costitem = $('#costitem').val();
var productprice = $('#productprice').val();
if (costitem && productprice) {
$.ajax({
method: "POST",
url: "/Home/Calc",
data: { costitem: costitem, productprice: productprice },
success: function (result) {
$("#price").val(result);
}
});
};
}
</script>

View File

@ -20,12 +20,6 @@
<p>
<a asp-action="CreateProduct">Создать товар</a>
</p>
<p>
<a asp-action="UpdateCostItem">Изменить</a>
</p>
<p>
<a asp-action="CreateCostItem">Удалить</a>
</p>
<table class="table">
<thead>
<tr>
@ -58,6 +52,10 @@
<th>
@Html.DisplayFor(modelItem => item.Price)
</th>
<td>
<a class="btn btn-primary btn-sm" asp-action="EditProduct" asp-route-ID="@item.ID">Изменить</a>
<a class="btn btn-primary btn-sm" asp-action="Delete" asp-route-id="@item.ID">Удалить</a>
</td>
</tr>
}
</tbody>

View File

@ -99,7 +99,18 @@ namespace ElectronicsShopRestAPI.Controllers {
_productLogic.Create(model);
}
catch(Exception ex) {
_logger.LogError(ex, "Ошибка создания заказа");
_logger.LogError(ex, "Ошибка создания товара");
throw;
}
}
[HttpPost]
public void EditProduct(ProductBindingModel model) {
try {
_productLogic.Update(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка обновления товара");
throw;
}
}

View File

@ -34,18 +34,18 @@ namespace ElectronicsShopRestAPI.Controllers {
}
}
[HttpGet]
public ProductViewModel? GetPoduct(int productID)
public ProductViewModel? GetProduct(int _productID)
{
try
{
return _product.ReadElement(new ProductSearchModel
{
ID = productID
ID = _productID
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения суши по id={Id}", productID);
_logger.LogError(ex, "Ошибка получения товаров id={Id}", _productID);
throw;
}
}