CRUD base

This commit is contained in:
Sergey Kozyrev 2024-05-25 21:36:21 +04:00
parent 4fd4b0351f
commit d748583b95
4 changed files with 304 additions and 71 deletions

View File

@ -18,22 +18,23 @@ namespace ImplementerApp.Controllers
_logger = logger; _logger = logger;
_data = data; _data = data;
} }
private bool IsLoggedIn { get {return UserImplementer.user != null; } }
public IActionResult IndexNonReg() public IActionResult IndexNonReg()
{ {
if (UserImplementer.user == null) if (!IsLoggedIn)
return View(); return View();
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
public IActionResult Index() public IActionResult Index()
{ {
if (UserImplementer.user == null) if (!IsLoggedIn)
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg");
return View(); return View();
} }
[HttpGet] [HttpGet]
public IActionResult Enter() public IActionResult Enter()
{ {
if (UserImplementer.user == null) if (!IsLoggedIn)
return View(); return View();
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
@ -112,30 +113,60 @@ namespace ImplementerApp.Controllers
[HttpGet] [HttpGet]
public IActionResult IndexProduct() public IActionResult IndexProduct()
{ {
if (UserImplementer.user != null) { if (IsLoggedIn) {
var products = _data.GetProducts(UserImplementer.user.Id); var products = _data.GetProducts(UserImplementer.user!.Id);
return View(products); return View(products);
} }
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg");
} }
public IActionResult CreateProduct() [HttpPost]
public IActionResult IndexProduct(int id)
{
_data.DeleteProduct(id);
return RedirectToAction("IndexProduct");
}
[HttpGet]
public IActionResult CreateProduct(int id)
{ {
var details = new List<DetailViewModel>(); var details = _data.GetDetails(UserImplementer.user!.Id);
details.Add(new DetailViewModel ViewBag.AllDetails = details;
{ if (id != 0)
Id = 1, {
Name = "Test", var value = _data.GetProduct(id);
Cost = 55.5, if (value != null)
UserId = 1 return View(value);
}); }
details.Add(new DetailViewModel return View(new ProductViewModel());
{ }
Id = 2, [HttpPost]
Name = "Test1", public IActionResult CreateProduct(int id, string title, int[] detailIds, int[] counts)
Cost = 32, {
UserId = 2 ProductBindingModel model = new ProductBindingModel();
}); model.Id = id;
return View(details); model.Name = title;
model.UserId = UserImplementer.user!.Id;
var details = _data.GetDetails(UserImplementer.user!.Id);
double sum = 0;
for (int i = 0; i < detailIds.Length; i++)
{
var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!;
if (counts[i] <= 0)
continue;
model.ProductDetails[detailIds[i]] = (detail, counts[i]);
sum += detail.Cost * counts[i];
}
if (model.ProductDetails.Count == 0)
return RedirectToAction("IndexProduct");
model.Cost = sum;
if (id != 0)
{
_data.UpdateProduct(model);
}
else
{
_data.CreateProduct(model);
}
return RedirectToAction("IndexProduct");
} }
[HttpGet] [HttpGet]
public IActionResult IndexProduction() public IActionResult IndexProduction()
@ -147,37 +178,66 @@ namespace ImplementerApp.Controllers
} }
return RedirectToAction("IndexNonReg"); return RedirectToAction("IndexNonReg");
} }
public IActionResult CreateProduction() [HttpPost]
public IActionResult IndexProduction(int id)
{
_data.DeleteProduction(id);
return RedirectToAction("IndexProduction");
}
[HttpGet]
public IActionResult CreateProduction(int id)
{ {
var details = new List<DetailViewModel>(); var details = _data.GetDetails(UserImplementer.user!.Id);
details.Add(new DetailViewModel ViewBag.AllDetails = details;
if (id != 0)
{
var value = _data.GetProduction(id);
if (value != null)
return View(value);
}
return View(new ProductionViewModel());
}
[HttpPost]
public IActionResult CreateProduction(int id, string title, int[] detailIds)
{
ProductionBindingModel model = new ProductionBindingModel();
model.Id = id;
model.Name = title;
model.UserId = UserImplementer.user!.Id;
var details = _data.GetDetails(UserImplementer.user!.Id);
double sum = 0;
for (int i = 0; i < detailIds.Length; i++)
{ {
Id = 1, var detail = details!.FirstOrDefault(x => x.Id == detailIds[i])!;
Name = "Test", model.ProductionDetails[detailIds[i]] = detail;
Cost = 55.5, sum += detail.Cost;
UserId = 1 }
}); model.Cost = sum;
details.Add(new DetailViewModel bool changed = false;
{ if (model.ProductionDetails.Count > 0)
Id = 2, {
Name = "Test1", if (id != 0)
Cost = 32, {
UserId = 2 changed = _data.UpdateProduction(model);
}); }
return View(details); else
{
changed = _data.CreateProduction(model);
}
}
if (changed)
return RedirectToAction("IndexProduction");
else
{
ViewBag.AllDetails = details;
return View(model);
}
} }
public IActionResult Privacy() public IActionResult Privacy()
{ {
ImplementerViewModel user = new() if (IsLoggedIn)
{ return View(UserImplementer.user);
Email = "mail@mail.ru", return RedirectToAction("IndexNonReg");
Login = "Login",
Password = "password",
Name = "User"
};
return View(user);
} }
public IActionResult DetailTimeReport() public IActionResult DetailTimeReport()
{ {

View File

@ -69,9 +69,43 @@ namespace ImplementerApp
return _productLogic.ReadList(new ProductSearchModel() { UserId = userId }); return _productLogic.ReadList(new ProductSearchModel() { UserId = userId });
} }
public ProductViewModel? GetProduct(int id)
{
return _productLogic.ReadElement(new() { Id = id });
}
public bool UpdateProduct(ProductBindingModel model)
{
return _productLogic.Update(model);
}
public bool DeleteProduct(int productId)
{
return _productLogic.Delete(new() { Id = productId });
}
public bool CreateProduct(ProductBindingModel model)
{
return _productLogic.Create(model);
}
public List<ProductionViewModel>? GetProductions(int userId) public List<ProductionViewModel>? GetProductions(int userId)
{ {
return _productionLogic.ReadList(new() { UserId = userId }); return _productionLogic.ReadList(new() { UserId = userId });
} }
public ProductionViewModel? GetProduction(int id)
{
return _productionLogic.ReadElement(new() { Id = id });
}
public bool CreateProduction(ProductionBindingModel model)
{
return _productionLogic.Create(model);
}
public bool UpdateProduction(ProductionBindingModel model)
{
return _productionLogic.Update(model);
}
public bool DeleteProduction(int productionId)
{
return _productionLogic.Delete(new() { Id = productionId});
}
} }
} }

View File

@ -1,18 +1,19 @@
@using Contracts.ViewModels @using Contracts.ViewModels
@model List<DetailViewModel> @model ProductViewModel;
@{ @{
ViewData["Title"] = "CreateProduct"; ViewData["Title"] = "CreateProduct";
ViewBag.Details = Model.DetailProducts;
} }
<div class="text-center"> <div class="text-center">
<h2 class="display-4">Создание изделия</h2> <h2 class="display-4">Создание изделия</h2>
</div> </div>
<form method="post"> <form method="post">
<div class="row"> <div class="row">
<div class="col-4">Название:</div> <div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="title" id="title" /></div> <div class="col-8"><input type="text" name="title" id="title" /></div>
</div> </div>
<div class="container"> <div class="container">
<div>Детали</div> <div>Детали</div>
<div class="table-responsive-lg"> <div class="table-responsive-lg">
@ -21,24 +22,38 @@
<tr> <tr>
<th>Название</th> <th>Название</th>
<th>Количество</th> <th>Количество</th>
<th>Стоимость</th>
<th>Удалить</th> <th>Удалить</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach (var detail in Model) @foreach (var detail in ViewBag.Details)
{ {
<tr> <tr data-detail-id="@detail.Value.Item1.Id">
<td>@detail.Name</td>
<td> <td>
<input type="number" name="count" value="0" min="0" class="form-control" /> <input type="hidden" name="detailIds" value="@detail.Key" />
@detail.Value.Item1.Name
</td> </td>
<td> <td>
<input type="number" name="counts" value="@detail.Value.Item2" min="0" class="form-control detail-count" data-cost="@detail.Value.Item1.Cost" />
</td>
<td>@detail.Value.Item1.Cost</td>
<td>
<button type="button" class="deleteDetail" data-detail-id="@detail.Value.Item1.Id">Удалить</button>
</td> </td>
</tr> </tr>
} }
</tbody> </tbody>
</table> </table>
</div> </div>
<select id="detailSelect" class="form-control">
<option value="">Выберите деталь</option>
@foreach (var detail in ViewBag.AllDetails)
{
<option value="@detail.Id" data-cost="@detail.Cost">@detail.Name</option>
}
</select>
<button type="button" id="addDetail" class="btn btn-secondary">Добавить деталь</button>
</div> </div>
<div class="row"> <div class="row">
<div class="col-4">Сумма:</div> <div class="col-4">Сумма:</div>
@ -54,6 +69,61 @@
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script> <script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
$('#detailsTable').DataTable(); function updateSum() {
var sum = 0;
$('#detailsTable tbody tr').each(function () {
var count = $(this).find('input[name="counts"]').val();
var cost = $(this).find('input[name="counts"]').data('cost');
sum += count * cost;
});
$('#sum').val(sum.toFixed(2));
}
$('.deleteDetail').click(function () {
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$(document).on('change', '.detail-count', function () {
updateSum();
});
$('#addDetail').click(function () {
var selectedDetail = $('#detailSelect option:selected');
if (selectedDetail.val()) {
var detailId = selectedDetail.val();
var detailName = selectedDetail.text();
var detailCost = selectedDetail.data('cost');
var newRow = `
<tr data-detail-id="${detailId}">
<td>
<input type="hidden" name="detailIds" value="${detailId}" />
${detailName}
</td>
<td><input type="number" name="counts" value="0" min="0" class="form-control detail-count" data-cost="${detailCost}" /></td>
<td>${detailCost}</td>
<td><button type="button" class="deleteDetail" data-detail-id="${detailId}">Удалить</button></td>
</tr>
`;
$('#detailsTable tbody').append(newRow);
$('.deleteDetail').off('click').on('click', function () {
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$('.detail-count').off('change').on('change', function () {
updateSum();
});
$('#detailSelect').val('');
} else {
alert('Выберите деталь для добавления');
}
});
updateSum();
}); });
</script> </script>

View File

@ -1,9 +1,10 @@
@using Contracts.ViewModels @using Contracts.ViewModels
@model List<DetailViewModel> @model ProductionViewModel
@{ @{
ViewData["Title"] = "CreateProduction"; ViewData["Title"] = "CreateProduction";
ViewBag.Details = Model.DetailProductions;
} }
<div class="text-center"> <div class="text-center">
<h2 class="display-4">Создание производства</h2> <h2 class="display-4">Создание производства</h2>
@ -11,31 +12,42 @@
<form method="post"> <form method="post">
<div class="row"> <div class="row">
<div class="col-4">Название:</div> <div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="title" id="title" /></div> <div class="col-8"><input type="text" name="title" id="title" value="@Model.Name"/></div>
</div> </div>
<div class="container"> <div class="container">
<div>Details</div> <div>Детали</div>
<div class="table-responsive-lg"> <div class="table-responsive-lg">
<table id="detailsTable" class="display"> <table id="detailsTable" class="display">
<thead> <thead>
<tr> <tr>
<th>Выбор</th>
<th>Название</th> <th>Название</th>
<th>Стоимость</th>
<th>Удалить</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach (var detail in Model) @foreach (var detail in ViewBag.Details)
{ {
<tr> <tr data-detail-id="@detail.Value.Id">
<td> <td>
<input type="checkbox" name="details" value="@detail.Id" /> <input type="number" hidden="hidden" name="detailIds" value="@detail.Value.Id" />
@detail.Value.Name
</td> </td>
<td>@detail.Name</td> <td class="detail-cost" data-cost="@detail.Value.Cost">@detail.Value.Cost</td>
<td><button type="button" class="deleteDetail" data-detail-id="@detail.Value.Id">Удалить</button></td>
</tr> </tr>
} }
</tbody> </tbody>
</table> </table>
</div> </div>
<select id="detailSelect" class="form-control">
<option value="">Выберите деталь</option>
@foreach (var detail in ViewBag.AllDetails)
{
<option value="@detail.Id" data-cost="@detail.Cost">@detail.Name</option>
}
</select>
<button type="button" id="addDetail" class="btn btn-secondary">Добавить деталь</button>
</div> </div>
<div class="row"> <div class="row">
<div class="col-4">Сумма:</div> <div class="col-4">Сумма:</div>
@ -51,6 +63,63 @@
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script> <script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
$('#detailsTable').DataTable(); function updateSum() {
var sum = 0;
$('#detailsTable tbody tr').each(function () {
var cost = $(this).find('.detail-cost').data('cost');
sum += parseFloat(cost);
});
$('#sum').val(sum.toFixed(2));
}
$('.deleteDetail').off('click').on('click', function () {
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$('#addDetail').click(function () {
var selectedDetail = $('#detailSelect option:selected');
if (selectedDetail.val()) {
var exists = false;
$('#detailsTable tbody tr').each(function () {
if ($(this).data('detail-id') == detailId) {
exists = true;
return false;
}
});
if (exists) { return; }
var detailId = selectedDetail.val();
var detailName = selectedDetail.text();
var detailCost = selectedDetail.data('cost');
var newRow = `
<tr data-detail-id="${detailId}">
<td>
<input type="hidden" name="detailIds" value="${detailId}" />
${detailName}
</td>
<td class="detail-cost" data-cost="${detailCost}">${detailCost}</td>
<td><button type="button" class="deleteDetail" data-detail-id="${detailId}">Удалить</button></td>
</tr>
`;
$('#detailsTable tbody').append(newRow);
$('.deleteDetail').off('click').on('click', function () {
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$('#detailSelect').val('');
updateSum();
} else {
alert('Выберите деталь для добавления');
}
});
updateSum();
}); });
</script> </script>