From 045fd7a8e31a299b24005e41e30c959bacebcbf2 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Wed, 29 May 2024 02:11:03 +0400 Subject: [PATCH] Assemblies & Products CRUD --- .../Controllers/HomeController.cs | 131 ++++++++++++------ .../Views/Home/Assemblies.cshtml | 6 +- .../Views/Home/Assembly.cshtml | 20 +-- .../Views/Home/Component.cshtml | 3 +- .../Views/Home/Components.cshtml | 2 +- .../Views/Home/Product.cshtml | 78 +++++++++++ .../Views/Home/Products.cshtml | 66 +++++++++ 7 files changed, 252 insertions(+), 54 deletions(-) create mode 100644 ComputerShopGuarantorApp/Views/Home/Product.cshtml create mode 100644 ComputerShopGuarantorApp/Views/Home/Products.cshtml diff --git a/ComputerShopGuarantorApp/Controllers/HomeController.cs b/ComputerShopGuarantorApp/Controllers/HomeController.cs index bc64c76..49d4114 100644 --- a/ComputerShopGuarantorApp/Controllers/HomeController.cs +++ b/ComputerShopGuarantorApp/Controllers/HomeController.cs @@ -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 _logger; @@ -38,22 +36,6 @@ namespace ComputerShopGuarantorApp.Controllers return View(ApiUser.GetRequest>($"api/component/getcomponents?userId={ApiUser.User.Id}")); } - /* - [HttpGet] - public ComponentViewModel? GetComponent(int ComponentId) - { - if (ApiUser.User == null) - throw new Exception("Необходима авторизация"); - - var Result = ApiUser.GetRequest($"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($"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 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 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>($"api/product/getproducts?userId={ApiUser.User.Id}"); + return View(Products); + } + + public IActionResult CreateProduct() + { + if (ApiUser.User == null) + throw new Exception("Вход только авторизованным"); + + ViewBag.Components = ApiUser.GetRequest>($"api/component/getcomponents?userId={ApiUser.User.Id}"); + ViewBag.Shipments = ApiUser.GetRequest>($"api/shipment/getshipments?userId={ApiUser.User.Id}"); + return View("Product"); + } + + [HttpPost] + public void CreateProduct(string ProductName, int ShipmentId, double Price, int Warranty, List 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>($"api/component/getcomponents?userId={ApiUser.User.Id}"); + ViewBag.Shipments = ApiUser.GetRequest>($"api/shipment/getshipments?userId={ApiUser.User.Id}"); + return View("Product", ApiUser.GetRequest($"api/product/getproduct?id={Id}")); + } + + [HttpPost] + public void UpdateProduct(int Id, int ShipmentId, string ProductName, double Price, int Warranty, List 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"); + } + // ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ ОСТАЛЬНОЕ diff --git a/ComputerShopGuarantorApp/Views/Home/Assemblies.cshtml b/ComputerShopGuarantorApp/Views/Home/Assemblies.cshtml index f6eead8..f9fb2e0 100644 --- a/ComputerShopGuarantorApp/Views/Home/Assemblies.cshtml +++ b/ComputerShopGuarantorApp/Views/Home/Assemblies.cshtml @@ -1,4 +1,4 @@ -@using ComputerShopContracts.ViewModels +@using ComputerShopContracts.ViewModels; @model List @{ @@ -24,8 +24,8 @@ Номер - Название - Цена + Название + Цена Категория diff --git a/ComputerShopGuarantorApp/Views/Home/Assembly.cshtml b/ComputerShopGuarantorApp/Views/Home/Assembly.cshtml index 6980489..95ef8f7 100644 --- a/ComputerShopGuarantorApp/Views/Home/Assembly.cshtml +++ b/ComputerShopGuarantorApp/Views/Home/Assembly.cshtml @@ -1,5 +1,4 @@ -@using ComputerShopDataModels.Enums; -@using ComputerShopContracts.ViewModels; +@using ComputerShopContracts.ViewModels; @model AssemblyViewModel @@ -22,24 +21,29 @@
- +
- +
- +
- + @foreach (var Component in ViewBag.Components) + { + @if (Model != null && Model.AssemblyComponents.Values.Any(x => Component.Id == x.Id)) + { + + } + else { } diff --git a/ComputerShopGuarantorApp/Views/Home/Component.cshtml b/ComputerShopGuarantorApp/Views/Home/Component.cshtml index 9348afe..48ede93 100644 --- a/ComputerShopGuarantorApp/Views/Home/Component.cshtml +++ b/ComputerShopGuarantorApp/Views/Home/Component.cshtml @@ -1,5 +1,4 @@ -@using ComputerShopDataModels.Enums; -@using ComputerShopContracts.ViewModels; +@using ComputerShopContracts.ViewModels; @model ComponentViewModel diff --git a/ComputerShopGuarantorApp/Views/Home/Components.cshtml b/ComputerShopGuarantorApp/Views/Home/Components.cshtml index 3820789..068a636 100644 --- a/ComputerShopGuarantorApp/Views/Home/Components.cshtml +++ b/ComputerShopGuarantorApp/Views/Home/Components.cshtml @@ -1,4 +1,4 @@ -@using ComputerShopContracts.ViewModels +@using ComputerShopContracts.ViewModels; @model List @{ diff --git a/ComputerShopGuarantorApp/Views/Home/Product.cshtml b/ComputerShopGuarantorApp/Views/Home/Product.cshtml new file mode 100644 index 0000000..4515873 --- /dev/null +++ b/ComputerShopGuarantorApp/Views/Home/Product.cshtml @@ -0,0 +1,78 @@ +@using ComputerShopContracts.ViewModels; + +@model ProductViewModel + +@{ + ViewData["Title"] = "Товар"; +} + +
+
+ @if (Model == null) + { +

Создание товара

+ } + else + { +

Редактирование товара

+ } +
+ + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+
+
+ +
+
+ +
diff --git a/ComputerShopGuarantorApp/Views/Home/Products.cshtml b/ComputerShopGuarantorApp/Views/Home/Products.cshtml new file mode 100644 index 0000000..dd43f68 --- /dev/null +++ b/ComputerShopGuarantorApp/Views/Home/Products.cshtml @@ -0,0 +1,66 @@ +@using ComputerShopContracts.ViewModels; + +@model List + +@{ + ViewData["Title"] = "Товары"; +} + +
+
+

Список товаров

+

Просматривайте список товаров и добавляйте новые, а также выбирайте товар для изменения или удаления

+
+ + @{ + if (Model == null) + { +

Авторизируйтесь

+ return; + } +

+ Добавить товар +

+ + + + + + + + + + + + + + @foreach (var Item in Model) + { + + + + + + + + + + } + +
НомерНазваниеПоставщикЦенаГарантия
+ @Html.DisplayFor(ModelItem => Item.Id) + + @Html.DisplayFor(ModelItem => Item.ProductName) + + @Html.DisplayFor(ModelItem => Item.ProviderName) + + @Html.DisplayFor(ModelItem => Item.Price) + + @Html.DisplayFor(ModelItem => Item.Warranty) + + + + +
+ } +