This commit is contained in:
Игорь Гордеев 2024-05-29 21:23:38 +04:00
commit 2e476d5169
9 changed files with 159 additions and 16 deletions

View File

@ -12,7 +12,7 @@ namespace ElectronicsShopDataBaseImplement
optionsBuilder) optionsBuilder)
{ {
if (optionsBuilder.IsConfigured == false) { 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); base.OnConfiguring(optionsBuilder);
} }

View File

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

View File

@ -2,6 +2,7 @@ using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.ViewModels; using ElectronicsShopContracts.ViewModels;
using ElectronicsShopEmployeeApp.Models; using ElectronicsShopEmployeeApp.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Diagnostics; using System.Diagnostics;
namespace ElectronicsShopEmployeeApp.Controllers { namespace ElectronicsShopEmployeeApp.Controllers {
@ -140,6 +141,61 @@ namespace ElectronicsShopEmployeeApp.Controllers {
Response.Redirect("Index"); 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] [HttpPost]
public double Calc(int costitem, double productprice) { public double Calc(int costitem, double productprice) {
var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={costitem}"); var _costItem = APIEmployee.GetRequset<CostItemViewModel>($"api/employee/getcostitem?_costitemid={costitem}");

View File

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

View File

@ -27,7 +27,7 @@
<div class="row"> <div class="row">
<div class="col-4"></div> <div class="col-4"></div>
<div class="col-8"> <div class="col-8">
<input type="submit" value="Создать" class="btn btn-outline-primary"/> <input type="submit" value="Создать" class="btn btn-primary"/>
</div> </div>
</div> </div>
</form> </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> <p>
<a asp-action="CreateProduct">Создать товар</a> <a asp-action="CreateProduct">Создать товар</a>
</p> </p>
<p>
<a asp-action="UpdateCostItem">Изменить</a>
</p>
<p>
<a asp-action="CreateCostItem">Удалить</a>
</p>
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -58,6 +52,10 @@
<th> <th>
@Html.DisplayFor(modelItem => item.Price) @Html.DisplayFor(modelItem => item.Price)
</th> </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> </tr>
} }
</tbody> </tbody>

View File

@ -99,7 +99,18 @@ namespace ElectronicsShopRestAPI.Controllers {
_productLogic.Create(model); _productLogic.Create(model);
} }
catch(Exception ex) { 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; throw;
} }
} }

View File

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