редактирование

This commit is contained in:
Nastya_Kozlova 2024-05-27 20:27:11 +04:00
parent 5337232d0c
commit 698d467bf1
11 changed files with 162 additions and 148 deletions

@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.IdentityModel.Tokens;
using System.Diagnostics;
using System.Reflection;
namespace CarCenterAdministratorApp.Controllers
{
@ -237,51 +238,51 @@ namespace CarCenterAdministratorApp.Controllers
return View(APIClient.GetRequest<List<EquipmentViewModel>>($"api/main/getequipmentlist?administratorId={APIClient.Administrator.Id}"));
}
[HttpGet]
public IActionResult CreateEquipment()
{
if (APIClient.Administrator == null)
{
return Redirect("~/Home/Enter");
}
var list = _car.ReadList(new CarSearchModel { AdministratorId = APIClient.Administrator.Id });
var simpCar = list.Select(x => new { CarId = x.Id, BrandCar = x.BrandCar });
ViewBag.Cars = new MultiSelectList(simpCar, "CarId", "BrandCar");
return View();
}
[HttpGet]
public IActionResult CreateEquipment()
{
if (APIClient.Administrator == null)
{
return Redirect("~/Home/Enter");
}
var list = _car.ReadList(new CarSearchModel { AdministratorId = APIClient.Administrator.Id });
var simpCar = list.Select(x => new { CarId = x.Id, BrandCar = x.BrandCar });
ViewBag.Cars = new MultiSelectList(simpCar, "CarId", "BrandCar");
return View();
}
[HttpPost]
public void CreateEquipment(string equipmentName, double equipmentPrice, int[] cars)
{
if (APIClient.Administrator == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(equipmentName) || equipmentPrice <= 0 || cars.Length == 0)
{
throw new Exception("Введите название и стоимость");
}
Dictionary<int, ICarModel> equipmentCars = new Dictionary<int, ICarModel>();
foreach (int id in cars)
{
equipmentCars.Add(id, _car.ReadElement(new CarSearchModel { Id = id }));
}
[HttpPost]
public void CreateEquipment(string equipmentName, double equipmentPrice, int[] cars)
{
if (APIClient.Administrator == null)
{
throw new Exception("Необходима авторизация");
}
if (string.IsNullOrEmpty(equipmentName) || equipmentPrice <= 0 || cars.Length == 0)
{
throw new Exception("Введите название и стоимость");
}
Dictionary<int, ICarModel> equipmentCars = new Dictionary<int, ICarModel>();
foreach (int id in cars)
{
equipmentCars.Add(id, _car.ReadElement(new CarSearchModel { Id = id }));
}
APIClient.PostRequest("api/main/CreateEquipment", new EquipmentBindingModel
{
AdministratorId = APIClient.Administrator.Id,
EquipmentName = equipmentName,
EquipmentPrice = equipmentPrice
});
for (int i = 0; i < cars.Length; i++)
{
APIClient.PostRequest("api/main/AddCarToEquipment", Tuple.Create(
new EquipmentSearchModel() { EquipmentName = equipmentName },
new CarViewModel() { Id = cars[i] }
));
}
Response.Redirect("ListEquipments");
}
APIClient.PostRequest("api/main/CreateEquipment", new EquipmentBindingModel
{
AdministratorId = APIClient.Administrator.Id,
EquipmentName = equipmentName,
EquipmentPrice = equipmentPrice
});
for (int i = 0; i < cars.Length; i++)
{
APIClient.PostRequest("api/main/AddCarToEquipment", Tuple.Create(
new EquipmentSearchModel() { EquipmentName = equipmentName },
new CarViewModel() { Id = cars[i] }
));
}
Response.Redirect("ListEquipments");
}
public IActionResult UpdateEquipment()
@ -291,11 +292,12 @@ namespace CarCenterAdministratorApp.Controllers
return Redirect("~/Home/Enter");
}
ViewBag.Equipments = APIClient.GetRequest<List<EquipmentViewModel>>($"api/main/getequipmentlist?administratorId={APIClient.Administrator.Id}");
ViewBag.Cars = APIClient.GetRequest<List<CarViewModel>>($"api/main/getcarlist?administratorid={APIClient.Administrator.Id}");
return View();
}
[HttpPost]
public void UpdateEquipment(int equipment, string equipmentName, double equipmentPrice)
public void UpdateEquipment(int equipment, string equipmentName, double equipmentPrice, List<int> cars)
{
if (APIClient.Administrator == null)
{
@ -309,12 +311,18 @@ namespace CarCenterAdministratorApp.Controllers
{
throw new Exception("Введите стоимость");
}
Dictionary<int, ICarModel> a = new Dictionary<int, ICarModel>();
foreach (int car in cars)
{
a.Add(car, new CarSearchModel { Id = car } as ICarModel);
}
APIClient.PostRequest("api/main/updateequipment", new EquipmentBindingModel
{
Id = equipment,
AdministratorId = APIClient.Administrator.Id,
EquipmentName = equipmentName,
EquipmentPrice = equipmentPrice
EquipmentPrice = equipmentPrice,
EquipmentCars = a,
});
Response.Redirect("ListEquipments");
}
@ -402,12 +410,13 @@ namespace CarCenterAdministratorApp.Controllers
{
return Redirect("~/Home/Enter");
}
ViewBag.Inspections = APIClient.GetRequest<List<InspectionViewModel>>($"api/main/getinspectionlist?administratorId={APIClient.Administrator.Id}");
ViewBag.Cars = APIClient.GetRequest<List<CarViewModel>>($"api/main/getcarlist?administratorid={APIClient.Administrator.Id}");
ViewBag.Inspections = APIClient.GetRequest<List<InspectionViewModel>>($"api/main/getinspectionlist?administratorId={APIClient.Administrator.Id}");
return View();
}
[HttpPost]
public void UpdateInspection(int inspection, string inspectionName, DateTime inspectionDate, int[] cars)
public void UpdateInspection(int inspection, string inspectionName, DateTime inspectionDate, List<int> cars)
{
if (APIClient.Administrator == null)
{
@ -417,14 +426,20 @@ namespace CarCenterAdministratorApp.Controllers
{
throw new Exception("Введите название");
}
APIClient.PostRequest("api/main/updateinspection", new InspectionBindingModel
Dictionary<int, ICarModel> a = new Dictionary<int, ICarModel>();
foreach (int car in cars)
{
a.Add(car, new CarSearchModel { Id = car } as ICarModel);
}
APIClient.PostRequest("api/main/updateinspection", new InspectionBindingModel
{
Id = inspection,
AdministratorId = APIClient.Administrator.Id,
InspectionName = inspectionName,
InspectionDate = inspectionDate
});
Response.Redirect("ListInspections");
InspectionDate = inspectionDate,
InspectionCars = a,
});
Response.Redirect("ListInspections");
}
public IActionResult DeleteInspection()

@ -1,43 +0,0 @@
@using CarCenterContracts.ViewModels;
@using CarCenterDataModels.Models;
@{
ViewData["Title"] = "AddCarToEquipment";
}
@model Tuple<List<EquipmentViewModel>, List<CarViewModel>>
<div class="container">
<h2>Добавление машин в комплектацию</h2>
<form method="post">
<div class="form-group">
<label for="equipment">Выберите комплектацию</label>
</div>
<div class="form-group">
<label for="car">Выберите комплектацию</label>
<div class="card">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table mb-0">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Бренд машины</th>
<th scope="col">Модель</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<br>
<div class="u-container-layout u-container-layout-2">
<input type="submit" value="Добавить" class="btn btn-outline-dark text-center d-flex justify-content-md-center" />
</div>
</form>
</div>

@ -1,36 +0,0 @@
@using CarCenterContracts.ViewModels;
@using CarCenterDataModels.Models;
@{
ViewData["Title"] = "AddCarToInspection";
}
@model Tuple<List<InspectionViewModel>, List<CarViewModel>>
<div class="container">
<h2>Добавление машин в осмотры</h2>
<form method="post">
<div class="form-group">
<label for="inspection">осмотры:</label>
</div>
<div class="form-group">
<label>Машины для осмотров:</label>
<div class="table-responsive">
<table class="table">
<thead class="thead-dark">
<tr>
<th>Бренд машины</th>
<th>Модель</th>
</
<tbody>
</tbody>
</table>
</div>
</div>
<br>
<div class="u-container-layout u-container-layout-2">
<input type="submit" value="Добавить" class="btn btn-outline-dark text-center d-flex justify-content-md-center" />
</div>
</form>
</div>

@ -26,9 +26,6 @@
<th class="u-border-1 u-border-grey-50 u-table-cell">
Стоимость
</th>
<th class="u-border-1 u-border-grey-50 u-table-cell">
Предпродажная работа
</th>
</tr>
</thead>
<tbody class="u-table-body">

@ -28,7 +28,17 @@
name="equipmentPrice"
class="form-control" />
</div>
<div class="row">
<div class="col-4">Машины:</div>
<div class="col-8">
<select name="cars" class="form-control" multiple size="5" id="cars">
@foreach (var car in ViewBag.Cars)
{
<option value="@car.Id" data-name="@car.Id">@car.BrandCar</option>
}
</select>
</div>
</div>
</div>
<br>
<div class="u-container-layout u-container-layout-2">
@ -43,15 +53,19 @@
<script>
function check() {
var equipment = $('#equipment').val();
$("#cars option:selected").removeAttr("selected");
if (equipment) {
$.ajax({
method: "GET",
url: "/Home/GetEquipment",
data: { equipmentId: equipment },
success: function (result) {
$('#equipmentName').val(result.equipmentName);
$('#equipmentPrice').val(result.equipmentPrice);
$('#table-elements').html(result.item2);
$('#equipmentName').val(result.item1.equipmentName);
$('#equipmentPrice').val(result.item1.equipmentPrice);
$('#table-elements').html(result.item1.item2);
$.map(result.item2, function (n) {
$(`option[data-name=${n.item2}]`).attr("selected", "selected")
});
}
});
};

@ -29,6 +29,18 @@
class="form-control" />
</div>
<div class="row">
<div class="col-4">Машины:</div>
<div class="col-8">
<select name="cars" class="form-control" multiple size="5" id="cars">
@foreach (var car in ViewBag.Cars)
{
<option value="@car.Id" data-name="@car.Id">@car.BrandCar</option>
}
</select>
</div>
</div>
</div>
<br>
<div class="u-container-layout u-container-layout-2">
@ -43,15 +55,19 @@
<script>
function check() {
var inspection = $('#inspection').val();
$("#cars option:selected").removeAttr("selected");
if (inspection) {
$.ajax({
method: "GET",
url: "/Home/GetInspection",
data: { inspectionId: inspection },
success: function (result) {
$('#inspectionName').val(result.inspectionName);
$('#inspectionDate').val(result.inspectionDate);
$('#table-elements').html(result.item2);
$('#inspectionName').val(result.item1.inspectionName);
$('#inspectionDate').val(result.item1.inspectionDate);
$('#table-elements').html(result.item1.item2);
$.map(result.item2, function (n) {
$(`option[data-name=${n.item2}]`).attr("selected", "selected")
});
}
});
};

@ -128,10 +128,11 @@ namespace CarCenterDataBaseImplement.Implements
{
return null;
}
elem.UpdateCars(context, model);
context.SaveChanges();
elem.Update(model);
context.SaveChanges();
if (model.EquipmentCars != null)
elem.UpdateCars(context, model);
transaction.Commit();
return elem.GetViewModel;
}

@ -136,13 +136,20 @@ namespace CarCenterDataBaseImplement.Implements
{
return null;
}
elem.Update(model);
/*elem.Update(model);
context.SaveChanges();
if (model.InspectionCars != null)
elem.UpdateCars(context, model);
transaction.Commit();
return elem.GetViewModel;
}
return elem.GetViewModel;*/
elem.UpdateCars(context, model);
context.SaveChanges();
elem.Update(model);
context.SaveChanges();
transaction.Commit();
return elem.GetViewModel;
}
catch
{
transaction.Rollback();

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using CarCenterDataModels.Models;
using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
using System.Diagnostics;
namespace CarCenterDataBaseImplement.Models
{
@ -83,6 +84,12 @@ namespace CarCenterDataBaseImplement.Models
{
var equipmentCars = context.EquipmentCars.Where(rec => rec.EquipmentId == model.Id).ToList();
var list = new List<int>();
foreach (var rec in model.EquipmentCars)
{
list.Add(rec.Key);
}
if (equipmentCars != null && equipmentCars.Count > 0)
{
context.EquipmentCars.RemoveRange(equipmentCars.Where(rec => !model.EquipmentCars.ContainsKey(rec.CarId)));
@ -108,5 +115,35 @@ namespace CarCenterDataBaseImplement.Models
}
_equipmentCars = null;
}
/*public void UpdateCars(CarCenterDataBase context, EquipmentBindingModel model)
{
var equipmentCars = context.EquipmentCars.Where(rec => rec.EquipmentId == model.Id).ToList();
if (equipmentCars != null && equipmentCars.Count > 0)
{
context.EquipmentCars.RemoveRange(equipmentCars.Where(rec => !model.EquipmentCars.ContainsKey(rec.CarId)));
context.SaveChanges();
foreach (var updateCar in equipmentCars)
{
model.EquipmentCars.Remove(updateCar.CarId);
}
context.SaveChanges();
}
var equipment = context.Equipments.First(x => x.Id == Id);
foreach (var ec in model.EquipmentCars)
{
context.EquipmentCars.Add(new EquipmentCar
{
Equipment = equipment,
Car = context.Cars.First(x => x.Id == ec.Key),
});
context.SaveChanges();
}
_equipmentCars = null;
}*/
}
}

@ -78,7 +78,13 @@ namespace CarCenterDataBaseImplement.Models
{
var inspectionCars = context.InspectionCars.Where(rec => rec.InspectionId == model.Id).ToList();
if (inspectionCars != null && inspectionCars.Count > 0)
var list = new List<int>();
foreach (var rec in model.InspectionCars)
{
list.Add(rec.Key);
}
if (inspectionCars != null && inspectionCars.Count > 0)
{
context.InspectionCars.RemoveRange(inspectionCars.Where(rec => !model.InspectionCars.ContainsKey(rec.CarId)));
context.SaveChanges();

@ -448,7 +448,7 @@ namespace CarCenterRestApi.Controllers
{
try
{
model.EquipmentCars = null!;
/*model.EquipmentCars = null!;*/
_equipment.Update(model);
}
catch (Exception ex)
@ -525,7 +525,7 @@ namespace CarCenterRestApi.Controllers
try
{
model.InspectionCars = null!;
/*model.InspectionCars = null!;*/
_inspection.Update(model);
}
catch (Exception ex)