Update BusinessLogic and some storages #1

Merged
Serxionaft merged 3 commits from LogicImplementer into main 2024-05-27 15:26:58 +04:00
4 changed files with 304 additions and 71 deletions
Showing only changes of commit d748583b95 - Show all commits

View File

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

View File

@ -69,9 +69,43 @@ namespace ImplementerApp
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)
{
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
@model List<DetailViewModel>
@model ProductViewModel;
@{
ViewData["Title"] = "CreateProduct";
ViewData["Title"] = "CreateProduct";
ViewBag.Details = Model.DetailProducts;
}
<div class="text-center">
<h2 class="display-4">Создание изделия</h2>
<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="title" id="title" /></div>
</div>
<div class="row">
<div class="col-4">Название:</div>
<div class="col-8"><input type="text" name="title" id="title" /></div>
</div>
<div class="container">
<div>Детали</div>
<div class="table-responsive-lg">
@ -21,24 +22,38 @@
<tr>
<th>Название</th>
<th>Количество</th>
<th>Стоимость</th>
<th>Удалить</th>
</tr>
</thead>
<tbody>
@foreach (var detail in Model)
@foreach (var detail in ViewBag.Details)
{
<tr>
<td>@detail.Name</td>
<tr data-detail-id="@detail.Value.Item1.Id">
<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>
<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>
</tr>
}
</tbody>
</table>
</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 class="row">
<div class="col-4">Сумма:</div>
@ -54,6 +69,61 @@
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script>
<script>
$(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>

View File

@ -1,9 +1,10 @@
@using Contracts.ViewModels
@model List<DetailViewModel>
@model ProductionViewModel
@{
ViewData["Title"] = "CreateProduction";
ViewBag.Details = Model.DetailProductions;
}
<div class="text-center">
<h2 class="display-4">Создание производства</h2>
@ -11,31 +12,42 @@
<form method="post">
<div class="row">
<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 class="container">
<div>Details</div>
<div>Детали</div>
<div class="table-responsive-lg">
<table id="detailsTable" class="display">
<thead>
<tr>
<th>Выбор</th>
<th>Название</th>
<th>Стоимость</th>
<th>Удалить</th>
</tr>
</thead>
<tbody>
@foreach (var detail in Model)
@foreach (var detail in ViewBag.Details)
{
<tr>
<tr data-detail-id="@detail.Value.Id">
<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>@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>
}
</tbody>
</table>
</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 class="row">
<div class="col-4">Сумма:</div>
@ -51,6 +63,63 @@
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script>
<script>
$(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>