add Furniture

This commit is contained in:
DozorovaA.A 2023-05-19 11:04:05 +04:00
parent 8caf2860a6
commit 897acf6c08
8 changed files with 426 additions and 11 deletions

View File

@ -20,7 +20,6 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
return context.Furnitures
.Include(x => x.User)
.Include(x => x.Materials)
.ThenInclude(x => x.Material).ToList()
.Select(x => x.GetViewModel).ToList();
}
public List<FurnitureViewModel> GetFilteredList(FurnitureSearchModel model)
@ -81,6 +80,7 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
{
var furniture = context.Furnitures
.Include(x => x.User)
.Include(x => x.Materials)
.FirstOrDefault(rec => rec.Id == model.Id);
if (furniture == null)
{

View File

@ -1,5 +1,6 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Models;
using FurnitureAssemblyStoreKeeperClientApp.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
@ -96,7 +97,8 @@ namespace FurnitureAssemblyStoreKeeperClientApp.Controllers
Response.Redirect("Enter");
return;
}
[HttpGet]
#region Scope
[HttpGet]
public IActionResult GetScopeList()
{
return View(APIClient.GetRequest<List<ScopeViewModel>>($"api/scope/GetScopeList"));
@ -177,7 +179,8 @@ namespace FurnitureAssemblyStoreKeeperClientApp.Controllers
});
Response.Redirect("GetScopeList");
}
#endregion
#region Material
[HttpGet]
public IActionResult GetMaterialsList()
{
@ -269,6 +272,127 @@ namespace FurnitureAssemblyStoreKeeperClientApp.Controllers
});
Response.Redirect("GetMaterialsList");
}
#endregion
#region Furniture
[HttpGet]
public IActionResult GetFurnitureList()
{
return View(APIClient.GetRequest<List<FurnitureViewModel>>($"api/furniture/getfurniturelist"));
}
[HttpGet]
public IActionResult CreateFurniture()
{
return View();
}
[HttpPost]
public double Calc(int count, int furniture)
{
var prod = APIClient.GetRequest<FurnitureViewModel>($"api/main/getfurniture?furnitureId={furniture}"
);
return count * (prod?.Cost ?? 1);
}
[HttpPost]
public void CreateFurniture(string name, int userId, int[] MaterialsIds, int[] counts)
{
if (APIClient.User == null)
{
throw new Exception("Вы как суда попали?");
}
if (string.IsNullOrEmpty(name))
{
throw new Exception("Название не указано");
}
if(counts == null || MaterialsIds.Length != counts.Length)
{
throw new Exception("Неверно заполнены компоненты");
}
var materials = APIClient.GetRequest<List<MaterialViewModel>>($"api/material/getmateriallist");
Dictionary<int, (IMaterialModel, int)> FurnitureMaterials = new Dictionary<int, (IMaterialModel, int)>();
double cost = 0;
for(int i = 0; i<counts.Length; i++)
{
var elem = (materials.Where(m => m.Id == MaterialsIds[i]).FirstOrDefault(), counts[i]);
FurnitureMaterials.Add(MaterialsIds[i], elem);
cost += counts[i] * materials.Where(m => m.Id == MaterialsIds[i]).FirstOrDefault().Cost;
}
APIClient.PostRequest("api/furniture/addfurniture", new FurnitureBindingModel
{
Name = name,
Cost = cost,
UserId = userId,
DateCreate = DateTime.UtcNow,
FurnitureMaterials = FurnitureMaterials
});
Response.Redirect("GetMaterialsList");
}
[HttpGet]
public IActionResult UpdateFurniture(int id)
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
var model = APIClient.GetRequest<FurnitureViewModel>($"api/furniture/GetFurniture?id={id}");
return View(model);
}
[HttpPost]
public void UpdateFurniture(int id, string name, double cost, int userId, int[] MaterialsIds, int[] counts)
{
if (APIClient.User == null)
{
throw new Exception("Вы как суда попали?");
}
if (string.IsNullOrEmpty(name))
{
throw new Exception("Название не указано");
}
var materials = APIClient.GetRequest<List<MaterialViewModel>>($"api/material/getmateriallist");
Dictionary<int, (IMaterialModel, int)> FurnitureMaterials = new Dictionary<int, (IMaterialModel, int)>();
double price = 0;
for (int i = 0; i < counts.Length; i++)
{
var elem = (materials.Where(m => m.Id == MaterialsIds[i]).FirstOrDefault(), counts[i]);
FurnitureMaterials.Add(MaterialsIds[i], elem);
price += elem.Item1.Cost * elem.Item2;
}
APIClient.PostRequest("api/furniture/updatefurniture", new FurnitureBindingModel
{
Id = id,
Name = name,
Cost = price,
UserId=userId,
FurnitureMaterials = FurnitureMaterials
});
Response.Redirect("GetFurnitureList");
}
[HttpGet]
public IActionResult DeleteFurniture()
{
if (APIClient.User == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<FurnitureViewModel>>($"api/furniture/GetFurnitureList"));
}
[HttpPost]
public void DeleteFurniture(int furniture)
{
if (APIClient.User == null)
{
throw new Exception("Вы как суда попали");
}
APIClient.PostRequest("api/furniture/deletefurniture", new MaterialBindingModel
{
Id = furniture,
});
Response.Redirect("GetFurnitureList");
}
#endregion
}
}

View File

@ -0,0 +1,95 @@
@using FurnitureAssemblyContracts.ViewModels
@{
ViewData["Title"] = "Создание мебели";
var materialList = APIClient.GetRequest<List<MaterialViewModel>>($"api/material/GetMaterialList");
var userList = APIClient.GetRequest<List<UserViewModel>>($"api/user/GetUserList");
}
<div class="text-center">
<h2 class="display-4">Создание мебели</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="name" id="name" /></div>
</div>
<div class="row">
<div class="col-4">Стоимость:</div>
<div class="col-8"><input type="text" name="cost" id="cost" /></div>
<div class="row">
<div class="col-4">Изготовитель:</div>
<div class="col-8">
<select name="userId">
<option value="0" disabled></option>
@foreach (var user in userList)
{
<option value="@user.Id">
@Html.DisplayFor(modelItem => user.Name)
</option>
}
</select>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>
</th>
<th>
Название
</th>
<th>
Стоимость
</th>
<th>
Количество
</th>
</tr>
</thead>
<tbody>
@foreach (var item in materialList)
{
<tr>
<td>
<input type="checkbox" class="form-check-input" name="MaterialsIds[]" value="@item.Id" id="@item.Id">
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cost)
</td>
<td>
<input type="text" id="@item.Id:count" name="counts[]" />
</td>
</tr>
}
</tbody>
</table>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Создать" class="btn btn-primary" /></div>
</div>
</form>
<script>
$('#furniture').on('change', function () {
check();
});
$('#count').on('change', function () {
check();
});
function check() {
var count = $('#count').val();
var furniture = $('#furniture').val();
if (count && furniture) {
$.ajax({
method: "POST",
url: "/Home/Calc",
data: { count: count, furniture: furniture },
success: function (result) {
$("#cost").val(result);}});
};
}
</script>

View File

@ -0,0 +1,27 @@
@using FurnitureAssemblyContracts.ViewModels;
@{
ViewData["Title"] = "Удаление мебели";
}
@model List<FurnitureViewModel>
<div class="text-center">
<h2 class="display-4">Удаление мебели</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Выбранная мебель:</div>
<div class="col-8">
<select id="furniture" name="furniture" class="form-control">
@foreach (var item in Model)
{
<option value="@item.Id">
@Html.DisplayFor(modelItem => item.Name)
</option>
}
</select>
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Удалить" class="btn btn-primary" /></div>
</div>
</form>

View File

@ -1,5 +1,80 @@
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@using FurnitureAssemblyContracts.ViewModels
@model List<FurnitureViewModel>
@{
ViewData["Title"] = "Мебель";
}
<div class="text-center">
<h1 class="display-4">Мебель</h1>
</div>
<div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<div>
<a asp-action="CreateFurniture">Создать</a>
<a asp-action="DeleteFurniture">Удалить</a>
</div>
<table class="table">
<thead>
<tr>
<th>
Id
</th>
<th>
Название
</th>
<th>
Стоимость
</th>
<th>
Дата создания
</th>
<th>
Изготовитель
</th>
<th>
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cost)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateCreate)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
<div class="row">
<div class="col-8"></div>
<a asp-action="UpdateFurniture" asp-route-id="@item.Id">Обновить</a>
</div>
</td>
</tr>
}
</tbody>
</table>
}
</div>

View File

@ -0,0 +1,93 @@
@using FurnitureAssemblyContracts.ViewModels
@model FurnitureViewModel
@{
ViewData["Title"] = "Обновление мебели";
var materialList = APIClient.GetRequest<List<MaterialViewModel>>($"api/material/GetMaterialList");
var userList = APIClient.GetRequest<List<UserViewModel>>($"api/user/GetUserList");
}
<div class="text-center">
<h2 class="display-4">Обновление мебели</h2>
</div>
<form method="post">
<div class="row" id = @Model.Id>
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="name" id="name" value=@Model.Name /></div>
</div>
<div class="row">
<div class="col-4">Стоимость:</div>
<div class="col-8">@Html.DisplayFor(modelItem => Model.Cost)</div>
</div>
<div class="row">
<div class="col-4">Изготовитель:</div>
<div class="row">
<div class="col-8">
<select id="userId" name="userId" class="form-control" onselect = @Model.UserName>
@foreach (var item in userList)
{
<option value="@item.Id">
@Html.DisplayFor(modelItem => item.Name)
</option>
}
</select>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>
</th>
<th>
Название
</th>
<th>
Стоимость
</th>
<th>
Количество
</th>
<th>
Изготовитель
</th>
</tr>
</thead>
<tbody>
@foreach (var item in materialList)
{
<tr>
<td>
@if(Model.FurnitureMaterials.Keys.Contains(item.Id))
{
<input type="checkbox" class="form-check-input" name="MaterialsIds[]" value="@item.Id" id="@item.Id" checked = true>
}
else
{
<input type="checkbox" class="form-check-input" name="MaterialsIds[]" value="@item.Id" id="@item.Id">
}
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Cost)
</td>
<td>
<input type="text" id="@item.Id:count" name="counts[]" />
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
</tr>
}
</tbody>
</table>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Изменить" class="btn btn-primary" /></div>
</div>
</form>

View File

@ -32,10 +32,7 @@
<div class="col-4">Стоимость:</div>
<div class="col-8"><input type="text" name="cost" id="cost" /></div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Изменить" class="btn btn-primary" /></div>
</div>
<div class="row">
<div class="col-4">Область применения:</div>
<div class="col-8">
@ -62,5 +59,9 @@
</select>
</div>
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Изменить" class="btn btn-primary" /></div>
</div>
</form>

View File

@ -31,7 +31,7 @@
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Мебель</a>
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="GetFurnitureList">Мебель</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="GetMaterialsList">Материалы</a>