добавили десятый график. сделали 12 этап курсовой работы

This commit is contained in:
dasha 2023-05-19 20:01:29 +04:00
parent 6f449c505b
commit 10fb62b17e
7 changed files with 105 additions and 49 deletions

View File

@ -322,7 +322,7 @@ namespace HardwareShopStorekeeperApp.Controllers
{ {
throw new Exception($"Цена комплектующего не может быть меньше или равна 0"); throw new Exception($"Цена комплектующего не может быть меньше или равна 0");
} }
APIClient.PostRequest("api/component/updatedata", new ComponentBindingModel APIClient.PostRequest("api/component/updatecomponent", new ComponentBindingModel
{ {
Id = component, Id = component,
ComponentName = name, ComponentName = name,

View File

@ -25,6 +25,7 @@
<thead> <thead>
<tr> <tr>
<th scope="col">Комплектующее</th> <th scope="col">Комплектующее</th>
<th scope="col">Стоимость</th>
<th scope="col">Количество</th> <th scope="col">Количество</th>
<th scope="col">Сумма</th> <th scope="col">Сумма</th>
<th scope="col">Действие</th> <th scope="col">Действие</th>
@ -74,28 +75,29 @@
console.log('try to add component') console.log('try to add component')
var count = $('#count').val(); var count = $('#count').val();
var component = $('#component').val(); var component = $('#component').val();
if (component && count) if (component && count && count > 0) {
$.ajax({ $.ajax({
method: "GET", method: "GET",
url: `/Storekeeper/GetComponent`, url: `/Storekeeper/GetComponent`,
data: { Id: component }, data: { Id: component },
success: function (result) { success: function (result) {
let flag = false let flag = false
if (list.length > 0) { if (list.length > 0) {
list.forEach((elem) => { list.forEach((elem) => {
if (elem.component.id === parseInt(result.id)) { if (elem.component.id === parseInt(result.id)) {
console.log('component already added') console.log('component already added')
flag = true flag = true
} }
}) })
}
if (!flag) list.push({ component: result, count: count })
reloadTable()
countElem.value = '1'
} }
if (!flag) list.push({ component: result, count: count }) }).fail(function(xhr, textStatus, errorThrown) {
reloadTable() alert(xhr.responseText);
countElem.value = '1' })
} }
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
})
}) })
saveBtn.addEventListener("click", () => { saveBtn.addEventListener("click", () => {
@ -129,7 +131,7 @@
let price = 0; let price = 0;
let count = 0; let count = 0;
list.forEach((elem) => { list.forEach((elem) => {
resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \ resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.component.cost}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \
<div> \ <div> \
<button onclick="deleteComponent(${count})" type="button" class="btn btn-danger"> \ <button onclick="deleteComponent(${count})" type="button" class="btn btn-danger"> \
<i class="fa fa-trash" aria-hidden="true"></i> \ <i class="fa fa-trash" aria-hidden="true"></i> \

View File

@ -28,6 +28,7 @@
<thead> <thead>
<tr> <tr>
<th scope="col">Комплектующее</th> <th scope="col">Комплектующее</th>
<th scope="col">Стоимость</th>
<th scope="col">Количество</th> <th scope="col">Количество</th>
<th scope="col">Сумма</th> <th scope="col">Сумма</th>
<th scope="col">Действие</th> <th scope="col">Действие</th>
@ -79,27 +80,28 @@
console.log('try to add component') console.log('try to add component')
var count = $('#count').val(); var count = $('#count').val();
var component = $('#component').val(); var component = $('#component').val();
$.ajax({ if (component && count && count > 0)
method: "GET", $.ajax({
url: `/Storekeeper/GetComponent`, method: "GET",
data: { Id: component }, url: `/Storekeeper/GetComponent`,
success: function (result) { data: { Id: component },
let flag = false success: function (result) {
if (list.length > 0) { let flag = false
list.forEach((elem) => { if (list.length > 0) {
if (elem.component.id === parseInt(result.id)) { list.forEach((elem) => {
console.log('component already added') if (elem.component.id === parseInt(result.id)) {
flag = true console.log('component already added')
} flag = true
}) }
})
}
if (!flag) list.push({ component: result, count: count })
reloadTable()
countElem.value = '1'
} }
if (!flag) list.push({ component: result, count: count }) }).fail(function(xhr, textStatus, errorThrown) {
reloadTable() alert(xhr.responseText);
countElem.value = '1' });
}
}).fail(function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
});
}) })
saveBtn.addEventListener("click", () => { saveBtn.addEventListener("click", () => {
@ -133,7 +135,7 @@
let price = 0; let price = 0;
let count = 0; let count = 0;
list.forEach((elem) => { list.forEach((elem) => {
resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \ resultTable.innerHTML += `<tr><td>${elem.component.componentName}</td><td>${elem.component.cost}</td><td>${elem.count}</td><td>${Math.round(elem.component.cost * elem.count * 100) / 100}</td><td> \
<div> \ <div> \
<button onclick="deleteComponent(${count})" type="button" class="btn btn-danger"> \ <button onclick="deleteComponent(${count})" type="button" class="btn btn-danger"> \
<i class="fa fa-trash" aria-hidden="true"></i> \ <i class="fa fa-trash" aria-hidden="true"></i> \

View File

@ -19,6 +19,7 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper
if (element != null) if (element != null)
{ {
var componentGoods = context.GoodsComponents.Where(x => x.ComponentId == element.Id).ToList(); var componentGoods = context.GoodsComponents.Where(x => x.ComponentId == element.Id).ToList();
var componentBuilds = context.ComponentsBuilds.Where(x => x.ComponentId == element.Id).ToList();
componentGoods.ForEach(x => componentGoods.ForEach(x =>
{ {
var good = context.Goods.Include(x => x.Components).FirstOrDefault(rec => rec.Id == x.GoodId); var good = context.Goods.Include(x => x.Components).FirstOrDefault(rec => rec.Id == x.GoodId);
@ -31,6 +32,15 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper
good.Price = Math.Round(good.Price, 2); good.Price = Math.Round(good.Price, 2);
} }
}); });
componentBuilds.ForEach(x =>
{
var build = context.Builds.Include(x => x.Components).FirstOrDefault(rec => rec.Id == x.BuildId);
if (build != null)
{
build.Price -= element.Cost * x.Count;
build.Price = Math.Round(build.Price, 2);
}
});
context.Components.Remove(element); context.Components.Remove(element);
context.SaveChanges(); context.SaveChanges();
return element.GetViewModel; return element.GetViewModel;
@ -121,19 +131,36 @@ namespace HardwareShopDatabaseImplement.Implements.Storekeeper
using var transaction = context.Database.BeginTransaction(); using var transaction = context.Database.BeginTransaction();
try try
{ {
var good = context.Components var component = context.Components
.Include(x => x.Builds) .Include(x => x.Builds)
.ThenInclude(x => x.Build) .ThenInclude(x => x.Build)
.FirstOrDefault(rec => rec.Id == model.Id); .FirstOrDefault(rec => rec.Id == model.Id);
if (good == null || good.UserId != model.UserId) if (component == null || component.UserId != model.UserId)
{ {
return null; return null;
} }
good.Update(model); component.Update(model);
context.SaveChanges();
if (model.ComponentBuilds == null)
{
context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Where(x => x.ContainsComponent(model.Id))
.ToList().ForEach(x => { x.Price = x.Components.Sum(x => x.Component.Cost * x.Count); });
}
else
component.UpdateBuilds(context, model);
context.Goods
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Where(x => x.ContainsComponent(model.Id))
.ToList().ForEach(x => { x.Price = x.Components.Sum(x => x.Component.Cost * x.Count); });
context.SaveChanges(); context.SaveChanges();
good.UpdateBuilds(context, model);
transaction.Commit(); transaction.Commit();
return good.GetViewModel; return component.GetViewModel;
} }
catch catch
{ {

View File

@ -71,6 +71,11 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
Price = model.Price; Price = model.Price;
} }
public bool ContainsComponent(int componentId)
{
return GoodComponents.ContainsKey(componentId);
}
public GoodViewModel GetViewModel => new() public GoodViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,

View File

@ -91,6 +91,11 @@ namespace HardwareShopDatabaseImplement.Models.Worker
Price = model.Price; Price = model.Price;
} }
public bool ContainsComponent(int componentId)
{
return BuildComponents.ContainsKey(componentId);
}
public BuildViewModel GetViewModel => new() public BuildViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,

View File

@ -105,6 +105,21 @@ namespace HardwareShopRestApi.Controllers
} }
} }
[HttpPost]
public void UpdateComponent(ComponentBindingModel model)
{
try
{
model.ComponentBuilds = null!;
_component.Update(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления данных комплектующего");
throw;
}
}
[HttpPost] [HttpPost]
public void DeleteComponent(ComponentBindingModel model) public void DeleteComponent(ComponentBindingModel model)
{ {