Assemblies & Products CRUD
This commit is contained in:
parent
6a5bb257d9
commit
045fd7a8e3
@ -1,14 +1,12 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopGuarantorApp.Models;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ComputerShopGuarantorApp.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
|
||||
@ -38,22 +36,6 @@ namespace ComputerShopGuarantorApp.Controllers
|
||||
return View(ApiUser.GetRequest<List<ComponentViewModel>>($"api/component/getcomponents?userId={ApiUser.User.Id}"));
|
||||
}
|
||||
|
||||
/*
|
||||
[HttpGet]
|
||||
public ComponentViewModel? GetComponent(int ComponentId)
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
throw new Exception("Необходима авторизация");
|
||||
|
||||
var Result = ApiUser.GetRequest<ComponentViewModel>($"api/component/getcomponent?id={ComponentId}");
|
||||
if (Result == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
*/
|
||||
|
||||
public IActionResult CreateComponent()
|
||||
{
|
||||
return View("Component");
|
||||
@ -126,24 +108,6 @@ namespace ComputerShopGuarantorApp.Controllers
|
||||
return View(Assemblies);
|
||||
}
|
||||
|
||||
/*
|
||||
[HttpGet]
|
||||
public AssemblyViewModel? GetAssembly(int AssemblyId)
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
{
|
||||
throw new Exception("Необходима авторизация");
|
||||
}
|
||||
|
||||
var Result = ApiUser.GetRequest<AssemblyViewModel>($"api/assembly/getassembly?id={AssemblyId}");
|
||||
if (Result == null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
*/
|
||||
|
||||
public IActionResult CreateAssembly()
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
@ -154,13 +118,13 @@ namespace ComputerShopGuarantorApp.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void CreateAssembly(string AssemblyName, double Price, string Category, int[] ComponentIds)
|
||||
public void CreateAssembly(string AssemblyName, double Price, string Category, List<int> ComponentIds)
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
throw new Exception("Вход только авторизованным");
|
||||
|
||||
var SelectedComponents = ComponentIds.ToDictionary(Id => Id, Id => new ComponentBindingModel { Id = Id });
|
||||
ApiUser.PostRequest("api/assembly/createAssembly", new AssemblyBindingModel
|
||||
ApiUser.PostRequest("api/assembly/createassembly", new AssemblyBindingModel
|
||||
{
|
||||
UserId = ApiUser.User.Id,
|
||||
AssemblyName = AssemblyName,
|
||||
@ -181,7 +145,7 @@ namespace ComputerShopGuarantorApp.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void UpdateAssembly(int Id, string AssemblyName, double Price, string Category, int[] ComponentIds)
|
||||
public void UpdateAssembly(int Id, string AssemblyName, double Price, string Category, List<int> ComponentIds)
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
throw new Exception("Вход только авторизованным");
|
||||
@ -214,6 +178,93 @@ namespace ComputerShopGuarantorApp.Controllers
|
||||
Response.Redirect("../Assemblies");
|
||||
}
|
||||
|
||||
/*------------------------------------------------------
|
||||
* Товары
|
||||
*-----------------------------------------------------*/
|
||||
|
||||
public IActionResult Products()
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
return Redirect("~/Home/Enter");
|
||||
|
||||
var Products = ApiUser.GetRequest<List<ProductViewModel>>($"api/product/getproducts?userId={ApiUser.User.Id}");
|
||||
return View(Products);
|
||||
}
|
||||
|
||||
public IActionResult CreateProduct()
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
throw new Exception("Вход только авторизованным");
|
||||
|
||||
ViewBag.Components = ApiUser.GetRequest<List<ComponentViewModel>>($"api/component/getcomponents?userId={ApiUser.User.Id}");
|
||||
ViewBag.Shipments = ApiUser.GetRequest<List<ShipmentViewModel>>($"api/shipment/getshipments?userId={ApiUser.User.Id}");
|
||||
return View("Product");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void CreateProduct(string ProductName, int ShipmentId, double Price, int Warranty, List<int> ComponentIds)
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
throw new Exception("Вход только авторизованным");
|
||||
|
||||
var SelectedComponents = ComponentIds.ToDictionary(Id => Id, Id => new ComponentBindingModel { Id = Id });
|
||||
ApiUser.PostRequest("api/product/createproduct", new ProductBindingModel
|
||||
{
|
||||
UserId = ApiUser.User.Id,
|
||||
ShipmentId = ShipmentId != 0 ? ShipmentId : null,
|
||||
ProductName = ProductName,
|
||||
Price = Price,
|
||||
Warranty = Warranty,
|
||||
ProductComponents = SelectedComponents,
|
||||
});
|
||||
Response.Redirect("Products");
|
||||
}
|
||||
|
||||
public IActionResult UpdateProduct(int Id)
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
return Redirect("~/Home/Enter");
|
||||
|
||||
ViewBag.Components = ApiUser.GetRequest<List<ComponentViewModel>>($"api/component/getcomponents?userId={ApiUser.User.Id}");
|
||||
ViewBag.Shipments = ApiUser.GetRequest<List<ShipmentViewModel>>($"api/shipment/getshipments?userId={ApiUser.User.Id}");
|
||||
return View("Product", ApiUser.GetRequest<ProductViewModel>($"api/product/getproduct?id={Id}"));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void UpdateProduct(int Id, int ShipmentId, string ProductName, double Price, int Warranty, List<int> ComponentIds)
|
||||
{
|
||||
if (ApiUser.User == null)
|
||||
throw new Exception("Вход только авторизованным");
|
||||
|
||||
if (Id == 0)
|
||||
throw new Exception("Товар не указан");
|
||||
|
||||
if (string.IsNullOrEmpty(ProductName))
|
||||
throw new Exception("Название товара не указано");
|
||||
|
||||
if (Warranty < 0)
|
||||
throw new Exception("Некорректное значение гарантии");
|
||||
|
||||
var SelectedComponents = ComponentIds.ToDictionary(Id => Id, Id => new ComponentBindingModel { Id = Id });
|
||||
ApiUser.PostRequest("api/product/updateproduct", new ProductBindingModel
|
||||
{
|
||||
Id = Id,
|
||||
UserId = ApiUser.User.Id,
|
||||
ShipmentId = ShipmentId != 0 ? ShipmentId : null,
|
||||
ProductName = ProductName,
|
||||
Price = Price,
|
||||
Warranty = Warranty,
|
||||
ProductComponents = SelectedComponents,
|
||||
});
|
||||
Response.Redirect("../Products");
|
||||
}
|
||||
|
||||
public void DeleteProduct(int Id)
|
||||
{
|
||||
ApiUser.PostRequest($"api/product/deleteproduct", new ProductBindingModel { Id = Id });
|
||||
Response.Redirect("../Products");
|
||||
}
|
||||
|
||||
|
||||
// ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
@using ComputerShopContracts.ViewModels
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
|
||||
@model List<AssemblyViewModel>
|
||||
@{
|
||||
@ -24,8 +24,8 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Номер</th>
|
||||
<th>Название</th>
|
||||
<th>Цена</th>
|
||||
<th class="w-25">Название</th>
|
||||
<th class="w-25">Цена</th>
|
||||
<th>Категория</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
|
@ -1,5 +1,4 @@
|
||||
@using ComputerShopDataModels.Enums;
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
|
||||
@model AssemblyViewModel
|
||||
|
||||
@ -22,24 +21,29 @@
|
||||
<form method="post">
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Название:</label>
|
||||
<input type="text" name="assemblyName" class="mb-3 form-control" value="@(Model == null ? "" : Model.AssemblyName)">
|
||||
<input type="text" name="assemblyName" class="mb-3 form-control" value="@(Model?.AssemblyName ?? "")">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Цена:</label>
|
||||
<input type="number" name="cost" class="mb-3 form-control" value="@(Model == null ? 0 : Model.Price)" readonly>
|
||||
<input type="number" name="price" class="mb-3 form-control" value="@(Model?.Price ?? 0.00)" readonly>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Категория:</label>
|
||||
<input type="text" name="category" class="mb-3 form-control" value="@(Model == null ? "" : Model.Category)">
|
||||
<input type="text" name="category" class="mb-3 form-control" value="@(Model?.Category ?? "")">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Комплектующие:</label>
|
||||
<select multiple size="6" name="componentIds" class="form-control">
|
||||
@{
|
||||
foreach (var Component in ViewBag.Components)
|
||||
<select name="componentIds" class="form-control border border-dark rounded" multiple size="6">
|
||||
@foreach (var Component in ViewBag.Components)
|
||||
{
|
||||
@if (Model != null && Model.AssemblyComponents.Values.Any(x => Component.Id == x.Id))
|
||||
{
|
||||
<option value="@Component.Id" selected="selected">@Component.ComponentName</option>
|
||||
}
|
||||
else
|
||||
{
|
||||
<option value="@Component.Id">@Component.ComponentName</option>
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
@using ComputerShopDataModels.Enums;
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
|
||||
@model ComponentViewModel
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
@using ComputerShopContracts.ViewModels
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
|
||||
@model List<ComponentViewModel>
|
||||
@{
|
||||
|
78
ComputerShopGuarantorApp/Views/Home/Product.cshtml
Normal file
78
ComputerShopGuarantorApp/Views/Home/Product.cshtml
Normal file
@ -0,0 +1,78 @@
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
|
||||
@model ProductViewModel
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Товар";
|
||||
}
|
||||
|
||||
<div class="container-md py-4 mb-4">
|
||||
<div class="text-center">
|
||||
@if (Model == null)
|
||||
{
|
||||
<h2>Создание товара</h2>
|
||||
}
|
||||
else
|
||||
{
|
||||
<h2>Редактирование товара</h2>
|
||||
}
|
||||
</div>
|
||||
|
||||
<form method="post">
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Название:</label>
|
||||
<input type="text" name="productName" class="mb-3 form-control" value="@(Model?.ProductName ?? "")">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Цена:</label>
|
||||
<input type="number" name="price" class="mb-3 form-control" value="@(Model?.Price ?? 0.00)" readonly>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Гарантия:</label>
|
||||
<input type="number" name="warranty" class="mb-3 form-control" value="@(Model?.Warranty ?? 0)">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Комплектующие:</label>
|
||||
<select name="componentIds" class="form-control border border-dark rounded" multiple size="6">
|
||||
@foreach (var Component in ViewBag.Components)
|
||||
{
|
||||
@if (Model != null && Model.ProductComponents.Values.Any(x => Component.Id == x.Id))
|
||||
{
|
||||
<option value="@Component.Id" selected="selected">@Component.ComponentName</option>
|
||||
}
|
||||
else
|
||||
{
|
||||
<option value="@Component.Id">@Component.ComponentName</option>
|
||||
}
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label class="mb-3">Поставщик:</label>
|
||||
<select name="shipmentId" class="form-control border border-dark rounded" size="4">
|
||||
@foreach (var Shipment in ViewBag.Shipments)
|
||||
{
|
||||
@if (Model != null && Model.ShipmentId == Shipment.Id)
|
||||
{
|
||||
<option value="@Shipment.Id" selected="selected">@Shipment.ProviderName</option>
|
||||
}
|
||||
else
|
||||
{
|
||||
<option value="@Shipment.Id">@Shipment.ProviderName</option>
|
||||
}
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-8"></div>
|
||||
<div class="col-4">
|
||||
<input type="submit" value="Сохранить" class="btn btn-primary" />
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
66
ComputerShopGuarantorApp/Views/Home/Products.cshtml
Normal file
66
ComputerShopGuarantorApp/Views/Home/Products.cshtml
Normal file
@ -0,0 +1,66 @@
|
||||
@using ComputerShopContracts.ViewModels;
|
||||
|
||||
@model List<ProductViewModel>
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Товары";
|
||||
}
|
||||
|
||||
<div class="container-md py-4 mb-4">
|
||||
<div class="text-center">
|
||||
<h2>Список товаров</h2>
|
||||
<p class="lead text-muted">Просматривайте список товаров и добавляйте новые, а также выбирайте товар для изменения или удаления</p>
|
||||
</div>
|
||||
|
||||
@{
|
||||
if (Model == null)
|
||||
{
|
||||
<h3 class="display-4">Авторизируйтесь</h3>
|
||||
return;
|
||||
}
|
||||
<p>
|
||||
<a class="btn btn-primary" asp-action="CreateProduct">Добавить товар</a>
|
||||
</p>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Номер</th>
|
||||
<th class="w-25">Название</th>
|
||||
<th class="w-25">Поставщик</th>
|
||||
<th class="w-25">Цена</th>
|
||||
<th>Гарантия</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var Item in Model)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@Html.DisplayFor(ModelItem => Item.Id)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(ModelItem => Item.ProductName)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(ModelItem => Item.ProviderName)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(ModelItem => Item.Price)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(ModelItem => Item.Warranty)
|
||||
</td>
|
||||
<td>
|
||||
<a asp-action="UpdateProduct" asp-route-Id="@(Item.Id)" role="button"><i class="bi bi-pen-fill"></i></a>
|
||||
</td>
|
||||
<td>
|
||||
<a asp-action="DeleteProduct" asp-route-Id="@(Item.Id)" role="button"><i class="bi bi-trash-fill"></i></a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
</div>
|
Loading…
Reference in New Issue
Block a user