This commit is contained in:
Николай 2023-05-16 23:01:02 +04:00
parent d498e507ff
commit b84b593b88
5 changed files with 212 additions and 14 deletions

View File

@ -53,13 +53,15 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return null; return null;
} }
using var context = new HardwareShopDatabase(); using var context = new HardwareShopDatabase();
return context.Builds var dadada = context.Builds
.Include(x => x.Purchases) .Include(x => x.Purchases)
.ThenInclude(x => x.Purchase) .ThenInclude(x => x.Purchase)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName) || .FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName) ||
(model.Id.HasValue && x.Id == model.Id)) (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel; ?.GetViewModel;
} return dadada;
}
public BuildViewModel? Insert(BuildBindingModel model) public BuildViewModel? Insert(BuildBindingModel model)
{ {

View File

@ -3,7 +3,9 @@ using HardwareShopContracts.BindingModels;
using HardwareShopContracts.BusinessLogicsContracts; using HardwareShopContracts.BusinessLogicsContracts;
using HardwareShopContracts.SearchModels; using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.Storekeeper;
using HardwareShopDatabaseImplement.Models.Worker; using HardwareShopDatabaseImplement.Models.Worker;
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models; using HardwareShopDataModels.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.ComponentModel; using System.ComponentModel;
@ -55,7 +57,33 @@ namespace HardwareShopRestApi.Controllers
} }
} }
[HttpPost] [HttpGet]
public List<Tuple<PurchaseViewModel, int>>? GetBuildPurchase(int buildId)
{
try
{
var result = _buildLogic.ReadElement(new() { Id = buildId });
List<Tuple<PurchaseViewModel, int>> listPurchase = new List<Tuple<PurchaseViewModel, int>>();
foreach (var item in result.BuildPurchases)
{
listPurchase.Add(Tuple.Create(new PurchaseViewModel
{
Id = item.Value.Item1.Id,
Sum = item.Value.Item1.Sum,
PurchaseStatus = item.Value.Item1.PurchaseStatus,
}, item.Value.Item2));
}
return listPurchase;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сборки");
throw;
}
}
[HttpPost]
public void Create(BuildBindingModel model) public void Create(BuildBindingModel model)
{ {
try try
@ -69,7 +97,8 @@ namespace HardwareShopRestApi.Controllers
} }
} }
[HttpPost]
[HttpPost]
public void Update(BuildBindingModel model) public void Update(BuildBindingModel model)
{ {
try try

View File

@ -2,10 +2,15 @@
using HardwareShopContracts.SearchModels; using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models; using HardwareShopDatabaseImplement.Models;
using HardwareShopDatabaseImplement.Models.ManyToMany;
using HardwareShopDatabaseImplement.Models.Storekeeper;
using HardwareShopDatabaseImplement.Models.Worker;
using HardwareShopDataModels.Enums; using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models;
using HardwareShopWorkerApp.Models; using HardwareShopWorkerApp.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Diagnostics; using System.Diagnostics;
using System.IO.Pipelines;
using static System.Net.Mime.MediaTypeNames; using static System.Net.Mime.MediaTypeNames;
namespace HardwareShopWorkerApp.Controllers namespace HardwareShopWorkerApp.Controllers
@ -143,6 +148,38 @@ namespace HardwareShopWorkerApp.Controllers
Response.Redirect("Builds"); Response.Redirect("Builds");
} }
[HttpPost]
public void LinkBuildPurchase(int buildId, int purchaseId, int count)
{
if (APIClient.User == null)
{
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
}
if (buildId <= 0)
{
throw new Exception($"Идентификтаор сборки не может быть ниже или равен 0");
}
if (purchaseId <= 0)
{
throw new Exception($"Идентификтаор покупки не может быть ниже или равен 0");
}
if (count <= 0)
{
throw new Exception($"Количество сборок в покупке не может быть ниже или равен 0");
}
var build = APIClient.GetRequest<BuildViewModel>($"api/build/getBuild?buildId={buildId}");
var purchase = APIClient.GetRequest<BuildViewModel>($"api/purchase/getpurchase?purchaseId={purchaseId}");
build.BuildPurchases.Add(purchaseId, (purchase as IPurchaseModel, count));
APIClient.PostRequest("api/build/update", new BuildBindingModel
{
Id = buildId,
Price = build.Price,
BuildName = build.BuildName,
BuildPurchases = build.BuildPurchases
});
Response.Redirect("Builds");
}
[HttpPost] [HttpPost]
public void DeleteBuild(int deleteBuildId) public void DeleteBuild(int deleteBuildId)
@ -369,9 +406,18 @@ namespace HardwareShopWorkerApp.Controllers
} }
[HttpGet] [HttpGet]
public IActionResult LinkPurchase() public IActionResult LinkPurchase(int buildId)
{ {
return View(); if (APIClient.User == null)
} {
} return Redirect("~/Home/Enter");
}
if (buildId <= 0)
{
throw new Exception($"Идентификтаор сборки не может быть ниже или равен 0");
}
ViewBag.Purchase = APIClient.GetRequest<List<PurchaseViewModel>>($"api/purchase/getpurchases?userId={APIClient.User.Id}");
return View(APIClient.GetRequest<List<Tuple<PurchaseViewModel, int>>>($"api/build/GetBuildPurchase?buildId={buildId}"));
}
}
} }

View File

@ -55,9 +55,7 @@
<button onclick="getBuild(@item.Id)" type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#deleteModal"> <button onclick="getBuild(@item.Id)" type="button" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#deleteModal">
<i class="fa fa-trash" aria-hidden="true"></i> <i class="fa fa-trash" aria-hidden="true"></i>
</button> </button>
<button onclick="deleteComponent(@item.Id)" type="button" class="btn btn-danger"> <a class="btn btn-primary btn-lg mb-5" asp-controller="Home" asp-action="LinkPurchase" asp-route-buildId="@item.Id"></a>
<i class="fa fa-trash" aria-hidden="true"></i>
</button>
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -1,4 +1,127 @@
 @using HardwareShopContracts.ViewModels;
@model List<Tuple<PurchaseViewModel, int>>
@{
ViewData["Title"] = "Привязка сборок";
Layout = "~/Views/Shared/_LayoutWorker.cshtml";
}
<div class="text-center">
<h1 class="display-4">Привязка сборок</h1>
</div>
<div class="text-center">
@{
<p>
<button type="button" onclick="getBuild()" class="btn btn-primary btn-lg mb-5" data-bs-toggle="modal" data-bs-target="#createModal">Добавить</button>
</p>
<table class="table">
<thead>
<tr>
<th>
Идентификатор покупки
</th>
<th>
Цена
</th>
<th>
Статус
</th>
<th>
Количество сборок в заказе
</th>
<th>
Действия
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Item1.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Item1.Sum)
</td>
<td>
@Html.DisplayFor(modelItem => item.Item1.PurchaseStatus)
</td>
<td>
@Html.DisplayFor(modelItem => item.Item2)
</td>
<td>
<div>
<button onclick="getURLParameter" type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#updateModal">
<i class="fa fa-pencil" aria-hidden="true"></i>
</button>
<button onclick="deleteComponent(@item.Item1.Id)" type="button" class="btn btn-danger">
<i class="fa fa-trash" aria-hidden="true"></i>
</button>
</div>
</td>
</tr>
}
</tbody>
</table>
}
</div>
<div class="modal fade" id="createModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form method="post" asp-controller="home" asp-action="LinkBuildPurchase">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Создание сборку</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Количество</label>
<input type="number" class="form-control" required="required" name="count" />
</div>
<label class="form-label">Сборка</label>
<select id="purchaseId" name="purchaseId" class="form-control" asp-items="@(new SelectList(@ViewBag.Purchase, "Id","Id"))"></select>
</div>
<input type="hidden" id="buildId" name="buildId" />
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
<input type="submit" class="btn btn-primary" value="Сохранить">
</div>
</form>
</div>
</div>
</div>
@section Scripts
{
<script>
function getBuild() {
let currentUrl = window.location.href;
console.log(currentUrl);
getURLParameter(currentUrl, 'buildId');
console.log(getURLParameter(currentUrl, 'buildId'));
$('#buildId').val(getURLParameter(currentUrl, 'buildId'));
var id = document.getElementById("buildId");
console.log(id.value);
}
console.log(getURLParameter('http://www.example.com/?page=24&info=13', 'page'));
function getURLParameter(sUrl, sParam) {
let sPageURL = sUrl.substring(sUrl.indexOf('?') + 1);
let sURLVariables = sPageURL.split('&');
for (let i = 0; i < sURLVariables.length; i++) {
let sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
</script>
}
@*
@{ @{
ViewData["Title"] = "LinkPurchase"; ViewData["Title"] = "LinkPurchase";
@ -78,4 +201,4 @@
</div> </div>
</div> </div>
</div> </div>
</form> </form>*@