From fb33d702e8135f1a6c7c3161e21574975901aa72 Mon Sep 17 00:00:00 2001 From: the Date: Wed, 17 May 2023 15:27:57 +0400 Subject: [PATCH] CRUD assembly --- .../Controllers/HomeController.cs | 61 ++++++++++++++ .../Views/Home/EditAssembly.cshtml | 81 +++++++++++++++++++ .../ComputerShopContracts.csproj | 1 + .../ViewModels/AssemblyViewModel.cs | 12 +++ .../Controllers/AssemblyController.cs | 32 ++++++++ 5 files changed, 187 insertions(+) create mode 100644 ComputerShopProvider/ComputerShopClientApp/Views/Home/EditAssembly.cshtml diff --git a/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs b/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs index 96c8fda..fa9b5d3 100644 --- a/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs +++ b/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs @@ -3,6 +3,7 @@ using ComputerShopContracts.BindingModels; using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; using Microsoft.AspNetCore.Mvc; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; using System.Diagnostics; namespace ComputerShopClientApp.Controllers @@ -276,6 +277,66 @@ namespace ComputerShopClientApp.Controllers Response.Redirect("Assembly"); } + public IActionResult EditAssembly() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Assemblies = APIClient.GetRequest>($"api/assembly/getassemblylist?clientId={APIClient.Client.Id}"); + return View(); + } + + [HttpPost] + public void EditAssembly(int assembly, string assemblyName, int price) + { + if (APIClient.Client == null) + { + throw new Exception("Необходима авторизация"); + } + if (string.IsNullOrEmpty(assemblyName)) + { + throw new Exception("Название не может быть пустым"); + } + if (price < 0) + { + throw new Exception("Цена не может быть меньше нуля"); + } + APIClient.PostRequest("api/assembly/editassembly", new AssemblyBindingModel + { + Id = assembly, + AssemblyName = assemblyName, + Price = price, + ClientId = APIClient.Client.Id + }); + Response.Redirect("Assembly"); + } + + [HttpGet] + public Tuple? GetAssembly(int assemblyId) + { + if (APIClient.Client == null) + { + throw new Exception("Необходима авторизация"); + } + var result = APIClient.GetRequest>>>($"api/assembly/getassembly?assemblyId={assemblyId}"); + if (result == null) + { + return default; + } + string table = ""; + for (int i = 0; i < result.Item2.Count; i++) + { + var componentName = result.Item2[i].Item1; + var componentAmount = result.Item2[i].Item2; + table += ""; + table += $"{componentName}"; + table += $"{componentAmount}"; + table += ""; + } + return Tuple.Create(result.Item1, table); + } + [HttpPost] diff --git a/ComputerShopProvider/ComputerShopClientApp/Views/Home/EditAssembly.cshtml b/ComputerShopProvider/ComputerShopClientApp/Views/Home/EditAssembly.cshtml new file mode 100644 index 0000000..14b04ca --- /dev/null +++ b/ComputerShopProvider/ComputerShopClientApp/Views/Home/EditAssembly.cshtml @@ -0,0 +1,81 @@ +@using ComputerShopContracts.ViewModels; +@using ComputerShopDataModels.Models; + +@{ + ViewData["Title"] = "EditAssembly"; +} + +
+
+ +
+ +
+
+
+ + +
+
+ + +
+
+ + + + + + + + + + + + + + +
+ Компонент + + Количество +
+
+
+
+
+
+
+ +@section Scripts + { + +} \ No newline at end of file diff --git a/ComputerShopProvider/ComputerShopContracts/ComputerShopContracts.csproj b/ComputerShopProvider/ComputerShopContracts/ComputerShopContracts.csproj index d8b29a6..47f9791 100644 --- a/ComputerShopProvider/ComputerShopContracts/ComputerShopContracts.csproj +++ b/ComputerShopProvider/ComputerShopContracts/ComputerShopContracts.csproj @@ -14,6 +14,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/ComputerShopProvider/ComputerShopContracts/ViewModels/AssemblyViewModel.cs b/ComputerShopProvider/ComputerShopContracts/ViewModels/AssemblyViewModel.cs index f170a52..239fc2a 100644 --- a/ComputerShopProvider/ComputerShopContracts/ViewModels/AssemblyViewModel.cs +++ b/ComputerShopProvider/ComputerShopContracts/ViewModels/AssemblyViewModel.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; +using Newtonsoft.Json; using System.Threading.Tasks; namespace ComputerShopContracts.ViewModels @@ -24,5 +25,16 @@ namespace ComputerShopContracts.ViewModels [DisplayName("ФИО клиента")] public string ClientFIO { get; set; } = string.Empty; + + public AssemblyViewModel() + { + + } + + [JsonConstructor] + public AssemblyViewModel(Dictionary AssemblyComponents) + { + this.AssemblyComponents = AssemblyComponents; + } } } diff --git a/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs b/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs index 6b67628..bf97d08 100644 --- a/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs +++ b/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs @@ -36,6 +36,24 @@ namespace ComputerShopRestApi.Controllers throw; } } + + [HttpGet] + public Tuple>>? GetAssembly(int assemblyId) + { + try + { + var elem = _assembly.ReadElement(new AssemblySearchModel { Id = assemblyId }); + if (elem == null) + return null; + return Tuple.Create(elem, elem.AssemblyComponents.Select(x => Tuple.Create(x.Value.Item1.ComponentName, x.Value.Item2)).ToList()); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения сборки по id={Id}", assemblyId); + throw; + } + } + [HttpGet] public List? GetComponentList() { @@ -89,5 +107,19 @@ namespace ComputerShopRestApi.Controllers throw; } } + + [HttpPost] + public void EditAssembly(AssemblyBindingModel model) + { + try + { + _assembly.Update(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления сборки"); + throw; + } + } } }