ProductViews are finally done + fixes in product logic/storage/model.
This commit is contained in:
parent
eeda90cb78
commit
82be9eab7d
@ -35,6 +35,11 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
|
||||
|
||||
public bool Update(ProductBindingModel model)
|
||||
{
|
||||
if(string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
model.Name = _productStorage.GetElement(new ProductSearchModel { ID = model.ID }).Name;
|
||||
}
|
||||
|
||||
CheckModel(model);
|
||||
if (_productStorage.Update(model) == null)
|
||||
{
|
||||
|
@ -65,7 +65,7 @@ namespace ComputerStoreDatabaseImplement.Implements
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var specProduct = context.Products.FirstOrDefault(x => x.ID==model.ID);
|
||||
var specProduct = context.Products.Include(x => x.Employee).Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => x.ID==model.ID);
|
||||
if(specProduct == null) { return null; }
|
||||
specProduct.Update(model);
|
||||
context.SaveChanges();
|
||||
@ -83,7 +83,7 @@ namespace ComputerStoreDatabaseImplement.Implements
|
||||
public ProductViewModel? Delete(ProductBindingModel model)
|
||||
{
|
||||
using var context = new ComputerStoreDatabase();
|
||||
var specProduct = context.Products.Include(x => x.Components).FirstOrDefault(x => x.ID == model.ID);
|
||||
var specProduct = context.Products.Include(x => x.Employee).Include(x => x.Components).FirstOrDefault(x => x.ID == model.ID);
|
||||
if(specProduct == null) { return null; }
|
||||
|
||||
context.Products.Remove(specProduct);
|
||||
|
@ -87,12 +87,15 @@ namespace ComputerStoreDatabaseImplement.Models
|
||||
var productComponents = context.ProductComponents.Where(rec => rec.ProductID == model.ID).ToList();
|
||||
if(productComponents != null && productComponents.Count > 0)
|
||||
{
|
||||
context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID)));
|
||||
context.SaveChanges();
|
||||
foreach(var updateComponent in productComponents)
|
||||
if(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID)).Any())
|
||||
{
|
||||
updateComponent.Count = model.ProductComponents[updateComponent.ID].Item2;
|
||||
model.ProductComponents.Remove(updateComponent.ID);
|
||||
context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentID)));
|
||||
context.SaveChanges();
|
||||
}
|
||||
foreach(var updateComponent in productComponents.Where(x => model.ProductComponents.ContainsKey(x.ComponentID)))
|
||||
{
|
||||
updateComponent.Count = model.ProductComponents[updateComponent.ComponentID].Item2;
|
||||
model.ProductComponents.Remove(updateComponent.ComponentID);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
|
||||
|
||||
namespace ComputerStoreEmployeeApp.Controllers
|
||||
{
|
||||
@ -214,28 +215,91 @@ namespace ComputerStoreEmployeeApp.Controllers
|
||||
|
||||
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult ProductUpdate()
|
||||
{
|
||||
return View();
|
||||
[HttpGet("/Home/ProductUpdate/{id}")]
|
||||
public IActionResult ProductUpdate(int? id,bool selectChange = false)
|
||||
{
|
||||
|
||||
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
|
||||
var list = APIClient.GetRequest<List<ProductViewModel>>("api/main/getproductslist").Result;
|
||||
ViewBag.Products = list;
|
||||
|
||||
if (!id.HasValue)
|
||||
{
|
||||
var product = list.First();
|
||||
APIClient.productComponents = product.ProductComponents;
|
||||
ViewBag.Price = product.Price;
|
||||
return View(APIClient.productComponents);
|
||||
}
|
||||
|
||||
|
||||
ViewBag.ID = id;
|
||||
|
||||
if (selectChange)
|
||||
{
|
||||
var specproduct = list.First(x => x.ID == id);
|
||||
APIClient.productComponents = specproduct.ProductComponents;
|
||||
ViewBag.Price = specproduct.Price;
|
||||
return View(specproduct.ProductComponents);
|
||||
}
|
||||
|
||||
ViewBag.Price = APIClient.productComponents.Sum(x => x.Value.Component.Price * x.Value.Quantity);
|
||||
|
||||
return View(APIClient.productComponents);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void ProductUpdate(string val)
|
||||
public IActionResult ProductUpdate(int id, double productprice,string? productname)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Task.Run(() => APIClient.PatchRequest("api/main/updateproduct", new ProductBindingModel
|
||||
{
|
||||
ID = id,
|
||||
Name = productname,
|
||||
Price = productprice,
|
||||
EmployeeID = APIClient.Employee.ID,
|
||||
ProductComponents = APIClient.productComponents.Where(component => component.Value.Quantity != 0).ToDictionary(x => x.Key, x => (x.Value.Component as IComponentModel, x.Value.Quantity))
|
||||
|
||||
})).Result)
|
||||
{
|
||||
throw new InvalidOperationException("Something went wrong.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
APIClient.productComponents.Clear();
|
||||
ViewBag.Message = new string(ex.Message.ToString());
|
||||
ViewBag.Components = APIClient.GetRequest<List<ComponentViewModel>>("api/main/getcomponentslist").Result;
|
||||
return View(APIClient.productComponents);
|
||||
}
|
||||
|
||||
APIClient.productComponents.Clear();
|
||||
|
||||
return Redirect("ProductMenu");
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult ProductDelete()
|
||||
{
|
||||
ViewBag.Products = APIClient.GetRequest<List<ProductViewModel>>("api/main/getproductslist").Result;
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void ProductDelete(string val)
|
||||
public IActionResult ProductDelete(int product)
|
||||
{
|
||||
try
|
||||
{
|
||||
Task.Run(() => APIClient.DeleteRequest<string>($"api/main/deleteproduct/{product}"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ViewBag.Message = new string(ex.Message.ToString());
|
||||
return View();
|
||||
}
|
||||
|
||||
return Redirect("ProductMenu");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
|
18
ComputerStoreEmployeeApp/Views/Home/ProductDelete.cshtml
Normal file
18
ComputerStoreEmployeeApp/Views/Home/ProductDelete.cshtml
Normal file
@ -0,0 +1,18 @@
|
||||
@{
|
||||
ViewData["Title"] = "Delete a product";
|
||||
}
|
||||
<div class="text-center">
|
||||
<h2 class="display-4">Delete a product</h2>
|
||||
</div>
|
||||
<form method="post">
|
||||
<div class="flex-container" style="flex-direction: row; justify-content: space-between; display: flex; height: 100vh;">
|
||||
<div class="flex-containerB1" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
|
||||
<select id="product" name="product" class="form-control" asp-items="@(new SelectList(@ViewBag.Products,"ID","Name"))"></select>
|
||||
<input type="submit" id="productbtn" value="DELETE" class="btn btn-primary" />
|
||||
</div>
|
||||
</div>
|
||||
@if (!string.IsNullOrEmpty(ViewBag.Message))
|
||||
{
|
||||
<script>alert("@ViewBag.Message");</script>
|
||||
}
|
||||
</form>
|
@ -12,7 +12,7 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="flex-containerB2" style="display: flex; align-items: center;">
|
||||
<form method="get" asp-controller="Home" asp-action="ProductUpdate">
|
||||
<form method="get" asp-controller="Home" asp-action="ProductUpdate" asp-route-id="null">
|
||||
<input type="submit" id="productupdate" value="UPDATE" class="btn btn-primary" style="font-size: 20pt; width: 130px;" />
|
||||
</form>
|
||||
</div>
|
||||
|
96
ComputerStoreEmployeeApp/Views/Home/ProductUpdate.cshtml
Normal file
96
ComputerStoreEmployeeApp/Views/Home/ProductUpdate.cshtml
Normal file
@ -0,0 +1,96 @@
|
||||
@using ComputerStoreDataModels.Models;
|
||||
@model Dictionary<int,(IComponentModel,int)>
|
||||
@{
|
||||
ViewData["Title"] = "Update a product";
|
||||
}
|
||||
<div class="text-center">
|
||||
<h2 class="display-4">Update a product</h2>
|
||||
</div>
|
||||
<form method="post">
|
||||
<div class="flex-container" style="flex-direction: row; justify-content: space-between; display: flex; height: 100vh;">
|
||||
<div class="flex-containerB1" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
|
||||
<h1>Component:</h1>
|
||||
<select id="component" name="component" class="form-control" asp-items="@(new SelectList(@ViewBag.Components,"ID","Name"))"></select>
|
||||
<div class="componentquantity" style="text-align: left; font-size: 15px;">Quantity:</div>
|
||||
<input type="text" id="componentquantity" name="componentquantity" />
|
||||
<input type="button" id="componentbtn" value="ADD" class="btn btn-primary" />
|
||||
</div>
|
||||
<div class="flex-containerB2" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
<th>
|
||||
Name
|
||||
</th>
|
||||
<th>
|
||||
Count
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in Model)
|
||||
{
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Value.Item1.Name)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Value.Item2)
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="flex-containerB3" style="flex-direction: column; flex: 4; gap: 20px; display: flex; align-items: center; justify-content: center;">
|
||||
<h1>Product:</h1>
|
||||
<select id="product" name="product" class="form-control" asp-items="@(new SelectList(@ViewBag.Products,"ID","Name"))"></select>
|
||||
<div class="productname" style="text-align: left; font-size: 15px;">Name:</div>
|
||||
<input type="text" id="productname" name="productname" />
|
||||
<div class="productprice" style="text-align: left; font-size: 15px;">Price:</div>
|
||||
<input type="text" id="productprice" name="productprice" readonly />
|
||||
<input type="submit" id="productbtn" value="ADD" class="btn btn-primary" />
|
||||
</div>
|
||||
</div>
|
||||
@if (!string.IsNullOrEmpty(ViewBag.Message))
|
||||
{
|
||||
<script>alert("@ViewBag.Message");</script>
|
||||
}
|
||||
</form>
|
||||
<script>
|
||||
window.onload = function () {
|
||||
|
||||
$('#product').val(@ViewBag.ID);
|
||||
$('#productprice').val(@ViewBag.Price);
|
||||
}
|
||||
|
||||
$('#product').on('change',function() {
|
||||
var productid = $('#product').val();
|
||||
window.location.href = '/Home/ProductUpdate/' + productid + '?selectChange=' + true;
|
||||
});
|
||||
|
||||
$('#componentbtn').on("click", function () {
|
||||
var count = $('#componentquantity').val();
|
||||
var id = $('#component').val();
|
||||
|
||||
$.when(ajax1(id, count)).done(function (a1) {
|
||||
localStorage.setItem("price", $('#productprice').val());
|
||||
var productid = $('#product').val();
|
||||
window.location.href = '/Home/ProductUpdate/' + productid
|
||||
})
|
||||
});
|
||||
|
||||
function ajax1(id, count) {
|
||||
|
||||
return $.ajax({
|
||||
method: "POST",
|
||||
url: "/Home/ProductComponents",
|
||||
data: { id: id, componentquantity: count },
|
||||
success: function (result) {
|
||||
$('#productprice').val(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user