CRUD assembly

This commit is contained in:
the 2023-05-17 15:27:57 +04:00
parent 91d0fccdb3
commit fb33d702e8
5 changed files with 187 additions and 0 deletions

View File

@ -3,6 +3,7 @@ using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels; using ComputerShopContracts.SearchModels;
using ComputerShopContracts.ViewModels; using ComputerShopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System.Diagnostics; using System.Diagnostics;
namespace ComputerShopClientApp.Controllers namespace ComputerShopClientApp.Controllers
@ -276,6 +277,66 @@ namespace ComputerShopClientApp.Controllers
Response.Redirect("Assembly"); Response.Redirect("Assembly");
} }
public IActionResult EditAssembly()
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
ViewBag.Assemblies = APIClient.GetRequest<List<AssemblyViewModel>>($"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<AssemblyViewModel, string>? GetAssembly(int assemblyId)
{
if (APIClient.Client == null)
{
throw new Exception("Необходима авторизация");
}
var result = APIClient.GetRequest<Tuple<AssemblyViewModel, List<Tuple<string, int>>>>($"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 += "<tr style=\"height: 44px\">";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{componentName}</td>";
table += $"<td class=\"u-border-1 u-border-grey-30 u-table-cell\">{componentAmount}</td>";
table += "</tr>";
}
return Tuple.Create(result.Item1, table);
}
[HttpPost] [HttpPost]

View File

@ -0,0 +1,81 @@
@using ComputerShopContracts.ViewModels;
@using ComputerShopDataModels.Models;
@{
ViewData["Title"] = "EditAssembly";
}
<form method="post">
<div class="u-form-group u-form-name u-label-top">
<label class="u-label u-text-custom-color-1 u-label-1">Сборка: </label>
<div class="u-input u-input-rectangle">
<select id="assembly" name="assembly" class="form-control" asp-items="@(new SelectList(@ViewBag.Assemblies, "Id", "AssemblyName"))"></select>
</div>
</div>
<div class="u-form-group u-form-name u-label-top">
<label class="u-label u-text-custom-color-1 u-label-1">Название сборки</label>
<input type="text"
id="assemblyName"
placeholder="Введите название сборки"
name="assemblyName"
class="u-input u-input-rectangle" />
</div>
<div class="u-form-group u-form-name u-label-top">
<label class="u-label u-text-custom-color-1 u-label-1">Цена сборки</label>
<input type="text"
id="price"
placeholder="Введите цену сборки"
name="price"
class="u-input u-input-rectangle" />
</div>
<div class="u-table u-table-responsive u-table-1">
<label class="u-label u-text-custom-color-1 u-label-1">Компоненты сборки</label>
<table class="u-table-entity">
<colgroup>
<col width="63%" />
<col width="37%" />
</colgroup>
<thead class="u-custom-color-1 u-table-header u-table-header-1">
<tr style="height: 44px">
<th class="u-border-1 u-border-black u-table-cell">
Компонент
</th>
<th class="u-border-1 u-border-black u-table-cell">
Количество
</th>
</tr>
</thead>
<tbody class="u-table-body" id="table-elements">
</tbody>
</table>
</div>
<div class="u-align-right u-form-group u-form-submit u-label-top">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Сохранить" class="u-active-custom-color-6 u-border-none u-btn u-btn-submit u-button-style u-custom-color-1 u-hover-custom-color-2 u-btn-1" /></div>
</div>
</form>
@section Scripts
{
<script>
function check() {
var assembly = $('#assembly').val();
if (assembly) {
$.ajax({
method: "GET",
url: "/Home/GetAssembly",
data: { assemblyId: assembly },
success: function (result) {
$('#assemblyName').val(result.item1.assemblyName);
$('#price').val(result.item1.Price);
$('#table-elements').html(result.item2);
}
});
};
}
check();
$('#assembly').on('change', function () {
check();
});
</script>
}

View File

@ -14,6 +14,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" /> <PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" />
</ItemGroup> </ItemGroup>

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ComputerShopContracts.ViewModels namespace ComputerShopContracts.ViewModels
@ -24,5 +25,16 @@ namespace ComputerShopContracts.ViewModels
[DisplayName("ФИО клиента")] [DisplayName("ФИО клиента")]
public string ClientFIO { get; set; } = string.Empty; public string ClientFIO { get; set; } = string.Empty;
public AssemblyViewModel()
{
}
[JsonConstructor]
public AssemblyViewModel(Dictionary<int, (IComponentModel, int)> AssemblyComponents)
{
this.AssemblyComponents = AssemblyComponents;
}
} }
} }

View File

@ -36,6 +36,24 @@ namespace ComputerShopRestApi.Controllers
throw; throw;
} }
} }
[HttpGet]
public Tuple<AssemblyViewModel, List<Tuple<string, int>>>? 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] [HttpGet]
public List<ComponentViewModel>? GetComponentList() public List<ComponentViewModel>? GetComponentList()
{ {
@ -89,5 +107,19 @@ namespace ComputerShopRestApi.Controllers
throw; throw;
} }
} }
[HttpPost]
public void EditAssembly(AssemblyBindingModel model)
{
try
{
_assembly.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления сборки");
throw;
}
}
} }
} }