реализовал редактирование сборки

This commit is contained in:
Николай 2023-05-16 10:40:26 +04:00
parent 4121d5ec1c
commit b9119331fe
3 changed files with 53 additions and 7 deletions

View File

@ -43,11 +43,11 @@ namespace HardwareShopRestApi.Controllers
} }
[HttpGet] [HttpGet]
public BuildViewModel? GetBuild(int id) public BuildViewModel? GetBuild(int buildId)
{ {
try try
{ {
return _buildLogic.ReadElement(new() { Id = id }); return _buildLogic.ReadElement(new() { Id = buildId });
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -1,4 +1,5 @@
using HardwareShopContracts.BindingModels; using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models; using HardwareShopDatabaseImplement.Models;
using HardwareShopDataModels.Enums; using HardwareShopDataModels.Enums;
@ -99,8 +100,27 @@ namespace HardwareShopWorkerApp.Controllers
Response.Redirect("Builds"); Response.Redirect("Builds");
} }
[HttpGet]
public BuildViewModel GetBuild(int buildId)
{
if (APIClient.User == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (buildId <= 0)
{
throw new Exception($"Идентификтаор сборки не может быть ниже или равен 0");
}
var result = APIClient.GetRequest<BuildViewModel>($"api/build/getBuild?buildId={buildId}");
if (result == null)
{
return null;
}
return result;
}
[HttpPost] [HttpPost]
public void UpdateBuild(string name) public void UpdateBuild(string name, int buildId)
{ {
if (APIClient.User == null) if (APIClient.User == null)
{ {
@ -108,10 +128,15 @@ namespace HardwareShopWorkerApp.Controllers
} }
if (string.IsNullOrEmpty(name)) if (string.IsNullOrEmpty(name))
{ {
throw new Exception($"Имя магазина не должно быть пустым"); throw new Exception($"Имя сборки не должно быть пустым");
}
if (buildId <= 0)
{
throw new Exception($"Идентификтаор сборки не может быть ниже или равен 0");
} }
APIClient.PostRequest("api/build/update", new BuildBindingModel APIClient.PostRequest("api/build/update", new BuildBindingModel
{ {
Id = buildId,
BuildName = name, BuildName = name,
UserId = APIClient.User.Id UserId = APIClient.User.Id
}); });

View File

@ -44,6 +44,9 @@
<td> <td>
@Html.DisplayFor(modelItem => item.BuildName) @Html.DisplayFor(modelItem => item.BuildName)
</td> </td>
<td>
<button onclick="update(@item.Id)" type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#updateModal">Изменить</button>
</td>
</tr> </tr>
} }
</tbody> </tbody>
@ -83,7 +86,7 @@
<div class="modal fade" id="updateModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal fade" id="updateModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog"> <div class="modal-dialog">
<div class="modal-content"> <div class="modal-content">
<form method="post" asp-controller="home" asp-action="update"> <form method="post" asp-controller="home" asp-action="UpdateBuild">
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Обновление сборки</h5> <h5 class="modal-title" id="exampleModalLabel">Обновление сборки</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
@ -91,9 +94,10 @@
<div class="modal-body"> <div class="modal-body">
<div class="form-group"> <div class="form-group">
<label>Name</label> <label>Name</label>
<input type="text" class="form-control" required="required" name="name" /> <input type="text" class="form-control" required="required" id="name" name="name" />
</div> </div>
</div> </div>
<input type="hidden" id="buildId" name="buildId" />
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
<input type="submit" class="btn btn-primary" value="Сохранить"> <input type="submit" class="btn btn-primary" value="Сохранить">
@ -104,3 +108,20 @@
</div> </div>
} }
</div> </div>
<script>
function update(buildId) {
$.ajax({
method: "GET",
url: "/Home/GetBuild",
data: { buildId: buildId },
success: function (result) {
if (result != null)
{
$('#name').val(result.buildName);
$('#buildId').val(result.id);
}
console.log(result);
}
});
}
</script>