дореализованы CRUD операции с таблицами

This commit is contained in:
goldfest 2024-05-28 01:47:51 +04:00
parent 722ccfc36e
commit 484e41cdc7
9 changed files with 173 additions and 67 deletions

View File

@ -122,30 +122,6 @@ namespace AdministratorApp.Controllers
return View(model);
}
//[HttpPost]
//public IActionResult CreateCarSales(int id, string brand, string model, double cost)
//{
// CarSalesBindingModel models = new CarSalesBindingModel();
// models.Id = id;
// models.CarBrand = brand;
// models.CarModel = model;
// models.CarCost = cost;
// if (models.Id == 0)
// {
// models.DateCreate = DateTime.Now;
// models.AdministratorId = UserAdministrator.admin!.Id;
// if (_data.CreateCarSale(models))
// return RedirectToAction("IndexCarSales");
// }
// else
// {
// if (_data.UpdateCarSale(models))
// return RedirectToAction("IndexCarSales");
// }
// return View();
//}
[HttpGet]
public IActionResult IndexInspection()
{
@ -176,11 +152,12 @@ namespace AdministratorApp.Controllers
return View(new InspectionViewModel());
}
[HttpPost]
public IActionResult CreateInspection(int id, string name, int[] CarSaleIds)
public IActionResult CreateInspection(int id, string name, DateTime date, int[] CarSaleIds)
{
InspectionBindingModel model = new InspectionBindingModel();
model.Id = id;
model.InspectionName = name;
model.InspectionDate = date;
model.AdministratorId = UserAdministrator.admin!.Id;
var CarSales = _data.GetCarSales(UserAdministrator.admin!.Id);
double sum = 0;
@ -190,8 +167,7 @@ namespace AdministratorApp.Controllers
model.InspectionCars[CarSaleIds[i]] = (CarSale);
sum += CarSale.CarCost;
}
if (model.InspectionCars.Count == 0)
return RedirectToAction("IndexInspection");
model.InspectionCost = sum;
if (id != 0)
{
@ -201,6 +177,7 @@ namespace AdministratorApp.Controllers
{
_data.CreateInspection(model);
}
return RedirectToAction("IndexInspection");
}
[HttpGet]
@ -229,7 +206,6 @@ namespace AdministratorApp.Controllers
var value = _data.GetCompletions(id);
if (value != null)
{
ViewBag.Cars = value.СompletionCars;
return View(value);
}
}
@ -269,7 +245,7 @@ namespace AdministratorApp.Controllers
else
{
ViewBag.AllCarSales = CarSales;
return View(model);
return RedirectToAction("IndexCompletions");
}
}
[HttpGet]

View File

@ -109,7 +109,7 @@
var newRow = `
<tr data-car-id="${carId}">
<td>
<input type="hidden" name="carIds" value="${carId}" />
<input type="hidden" name="CarSaleIds" value="${carId}" />
${carName}
</td>

View File

@ -1,41 +1,66 @@
@using CarCenterContracts.ViewModels
@model List<CarSalesViewModel>
@model InspectionViewModel
@{
ViewData["Title"] = "CreateInspection";
ViewData["Title"] = "CreateInspections";
ViewBag.Cars = Model.InspectionCars;
}
<div class="text-center">
<h2 class="display-4">Создание осмотра</h2>
</div>
<form method="post">
<form id="InspectionsForm" 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-4">Название осмотра:</div>
<div class="col-8">
<input type="text" name="name" id="name" value="@Model.InspectionName" />
<span id="titleError" class="text-danger"></span>
</div>
<div class="col-4">Дата осмотра:</div>
<div class="col-8">
<input type="date" name="date" id="date" value="@Model.InspectionDate" />
<span id="titleError" class="text-danger"></span>
</div>
</div>
<div class="container">
<div>CarSales</div>
<div>Машины</div>
<div class="table-responsive-lg">
<table id="CarSalessTable" class="display">
<table id="carsTable" class="display">
<thead>
<tr>
<th>Выбор</th>
<th>Название</th>
<th>Бренд</th>
<th>Стоимость</th>
<th>Удалить</th>
</tr>
</thead>
<tbody>
@foreach (var cars in Model)
@foreach (var car in ViewBag.Cars)
{
<tr>
<tr data-car-id="@car.Value.Id">
<td>
<input type="checkbox" name="cars" value="@cars.Id" />
<input type="hidden" name="carIds" value="@car.Value.Id" />
@car.Value.CarBrand
</td>
<td>@cars.CarBrand</td>
<td class="car-cost" data-cost="@car.Value.CarCost">@car.Value.CarCost</td>
<td><button type="button" class="deletecar" data-car-id="@car.Value.Id">Удалить</button></td>
</tr>
}
</tbody>
</table>
</div>
<select id="carSelect" class="form-control">
<option value="">Выберите машину</option>
@foreach (var car in ViewBag.AllCarSales)
{
<option value="@car.Id" data-cost="@car.CarCost">@car.CarBrand</option>
}
</select>
<button type="button" id="addcar" class="btn btn-secondary">Добавить машину</button>
</div>
<div class="row">
<div class="col-4">Сумма:</div>
<div class="col-8"><input type="text" id="sum" name="sum" readonly /></div>
</div>
<div class="row">
<div class="col-8"></div>
@ -47,6 +72,96 @@
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function () {
$('#CarSalesTable').DataTable();
function updateSum() {
var sum = 0;
$('#carsTable tbody tr').each(function () {
var cost = $(this).find('.car-cost').data('cost');
sum += parseFloat(cost);
});
$('#sum').val(sum.toFixed(2));
}
$(document).on('click', '.deletecar', function () {
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$('#addcar').click(function () {
var selectedcar = $('#carSelect option:selected');
if (selectedcar.val()) {
var carId = selectedcar.val();
var exists = false;
$('#carsTable tbody tr').each(function () {
if ($(this).data('car-id') == carId) {
exists = true;
return false;
}
});
if (exists) {
alert('Эта машина уже добавлена.');
return;
}
var carName = selectedcar.text();
var carCost = selectedcar.data('cost');
var newRow = `
<tr data-car-id="${carId}">
<td>
<input type="hidden" name="CarSaleIds" value="${carId}" />
${carName}
</td>
<td class="car-cost" data-cost="${carCost}">${carCost}</td>
<td><button type="button" class="deletecar" data-car-id="${carId}">Удалить</button></td>
</tr>
`;
$('#carsTable tbody').append(newRow);
$('.deletecar').off('click').on('click', function () {
var row = $(this).closest('tr');
row.remove();
updateSum();
});
$('#carSelect').val('');
updateSum();
} else {
alert('Выберите машину для добавления');
}
});
var date = $('#date').val();
if (date == "") {
$('#dateError').text('Пожалуйста, выберите дату осмотра.');
isValid = false;
} else {
$('#dateError').text('');
}
$('#InspectionsForm').submit(function (event) {
var title = $('#title').val();
var isValid = true;
$('#titleError').text('');
if (title.length < 2 || title.length > 50) {
$('#titleError').text('Название должно быть от 2 до 50 символов.');
isValid = false;
}
var totalcars = $('#carsTable tbody tr').length;
if (totalcars == 0) {
alert('Пожалуйста, добавьте хотя бы одну деталь.');
isValid = false;
}
if (!isValid) {
event.preventDefault();
}
});
updateSum();
});
</script>
</script>

View File

@ -19,7 +19,7 @@
return;
}
<p>
<a asp-action="CreateCompletions">Создать комплектацию</a>
<a asp-action="CreateCompletions" asp-route-id="0">Создать комплектацию</a>
</p>
<table class="table">
<thead>

View File

@ -9,35 +9,35 @@
<div class="row">
<div class="col-4">Имя:</div>
<div class="col-8">
<input type="text" name="name" id="name" />
<span id="nameError" class="text-danger"></span>
<input type="text" name="fio" id="fio" value="@Model.AdministratorFIO" />
<span id="fioError" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-4">Логин:</div>
<div class="col-8">
<input type="text" name="login" id="login" />
<input type="text" name="login" id="login" value="@Model.AdministratorLogin" />
<span id="loginError" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-4">Почта:</div>
<div class="col-8">
<input type="email" name="email" id="email" />
<input type="email" name="email" id="email" value="@Model.AdministratorEmail" />
<span id="emailError" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-4">Номер телефона:</div>
<div class="col-8">
<input type="text" name="number" id="number" />
<input type="text" name="number" id="number" value="@Model.AdministratorNumber" />
<span id="numberError" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-4">Пароль:</div>
<div class="col-8">
<input type="password" name="password1" id="password1" />
<input type="password" name="password1" id="password1" value="@Model.AdministratorPassword" />
<span id="password1Error" class="text-danger"></span>
</div>
</div>
@ -50,7 +50,7 @@
</div>
<div class="row">
<div class="col-8"></div>
<div class="col-4"><input type="submit" value="Регистрация" class="btn btn-primary" /></div>
<div class="col-4"><input type="submit" value="Обновить" class="btn btn-primary" /></div>
</div>
</form>
@ -58,7 +58,7 @@
<script>
$(document).ready(function () {
$('#adminForm').submit(function (event) {
var name = $('#name').val();
var name = $('#fio').val();
var login = $('#login').val();
var email = $('#email').val();
var number = $('#number').val();
@ -66,7 +66,7 @@
var password2 = $('#password2').val();
var isValid = true;
$('#nameError').text('');
$('#fioError').text('');
$('#loginError').text('');
$('#emailError').text('');
$('#numberError').text('');
@ -74,8 +74,8 @@
$('#password2Error').text('');
// Валидация имени
if (name.length < 2 || name.length > 20) {
$('#nameError').text('Имя должно быть от 2 до 20 символов.');
if (fio.length < 2 || fio.length > 20) {
$('#fioError').text('Имя должно быть от 2 до 20 символов.');
isValid = false;
}

View File

@ -18,10 +18,9 @@ namespace CarCenterBusinessLogic.BusinessLogics
_logger = logger;
_CompletionsStorage = CompletionsStorage;
}
public bool Create(CompletionsBindingModel model)
public bool Create(CompletionsBindingModel? model)
{
CheckModel(model);
model.СompletionCars = new();
var result = _CompletionsStorage.Insert(model);
@ -34,7 +33,7 @@ namespace CarCenterBusinessLogic.BusinessLogics
return true;
}
public bool Delete(CompletionsBindingModel model)
public bool Delete(CompletionsBindingModel? model)
{
CheckModel(model, false);
@ -51,7 +50,7 @@ namespace CarCenterBusinessLogic.BusinessLogics
return true;
}
public CompletionsViewModel? ReadElement(CopletionsSearchModel model)
public CompletionsViewModel? ReadElement(CopletionsSearchModel? model)
{
if (model == null)
{
@ -90,7 +89,7 @@ namespace CarCenterBusinessLogic.BusinessLogics
return list;
}
public bool Update(CompletionsBindingModel model)
public bool Update(CompletionsBindingModel? model)
{
CheckModel(model);
@ -103,7 +102,7 @@ namespace CarCenterBusinessLogic.BusinessLogics
return true;
}
private void CheckModel(CompletionsBindingModel model, bool withParams = true)
private void CheckModel(CompletionsBindingModel? model, bool withParams = true)
{
if (model == null)
{

View File

@ -27,7 +27,6 @@ namespace CarCenterBusinessLogic.BusinessLogics
public bool Create(InspectionBindingModel model)
{
CheckModel(model);
model.InspectionCars = new();
var result = _inspectionStorage.Insert(model);
@ -57,7 +56,7 @@ namespace CarCenterBusinessLogic.BusinessLogics
return true;
}
public InspectionViewModel? ReadElement(InspectionSearchModel model)
public InspectionViewModel? ReadElement(InspectionSearchModel? model)
{
if (model == null)
{
@ -96,7 +95,7 @@ namespace CarCenterBusinessLogic.BusinessLogics
return list;
}
public bool Update(InspectionBindingModel model)
public bool Update(InspectionBindingModel? model)
{
CheckModel(model);
@ -109,7 +108,7 @@ namespace CarCenterBusinessLogic.BusinessLogics
return true;
}
private void CheckModel(InspectionBindingModel model, bool withParams = true)
private void CheckModel(InspectionBindingModel? model, bool withParams = true)
{
if (model == null)
{
@ -131,6 +130,20 @@ namespace CarCenterBusinessLogic.BusinessLogics
throw new ArgumentNullException("Стоимость не может быть меньше нуля", nameof(model.InspectionCost));
}
if (model.InspectionCars.Count == 0)
{
throw new ArgumentNullException("Количество деталей в изделии должно быть больше 0", nameof(model.InspectionCars));
}
_logger.LogInformation("Inspection. InspectionName:{InspectionName}. InspectionCost:{InspectionCost}. Id:{Id}", model.InspectionName, model.InspectionCost, model.Id);
var elem = _inspectionStorage.GetElement(new InspectionSearchModel
{
InspectionName = model.InspectionName
});
if (elem != null && elem.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже существует");
}
_logger.LogInformation("Inspection. Id: { Id}", model.Id);
}

View File

@ -73,6 +73,7 @@ namespace CarCenterDataBaseImplement.Models
СompletionName = СompletionName,
AdministratorId = AdministratorId,
СompletionPrice = СompletionPrice,
СompletionFeatures = СompletionFeatures,
СompletionCars = СompletionCars
};

View File

@ -56,6 +56,7 @@ namespace CarCenterDataBaseImplement.Models
EmployeeId = model.EmployeeId,
AdministratorId = model.AdministratorId,
InspectionName = model.InspectionName,
InspectionDate = model.InspectionDate,
InspectionCost = model.InspectionCost,
Employee = model.EmployeeId.HasValue ? context.Employees.FirstOrDefault(x => x.Id == model.EmployeeId) : null,
Cars = model.InspectionCars.Select(x => new InspectionCar
@ -72,6 +73,7 @@ namespace CarCenterDataBaseImplement.Models
Id = model.Id,
InspectionName = model.InspectionName,
InspectionCost = model.InspectionCost,
InspectionDate = model.InspectionDate,
AdministratorId = model.AdministratorId,
EmployeeId = model.EmployeeId
};