переименовал переменные
This commit is contained in:
parent
b592b03d2f
commit
8740bec214
@ -70,16 +70,16 @@ namespace CarRepairShopDatabaseImplement.Implements
|
|||||||
using var transaction = context.Database.BeginTransaction();
|
using var transaction = context.Database.BeginTransaction();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var product = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id);
|
var repair = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id);
|
||||||
if (product == null)
|
if (repair == null)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
product.Update(model);
|
repair.Update(model);
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
product.UpdateComponents(context, model);
|
repair.UpdateComponents(context, model);
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
return product.GetViewModel;
|
return repair.GetViewModel;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
@ -16,19 +16,19 @@ namespace CarRepairShopDatabaseImplement.Models
|
|||||||
[Required]
|
[Required]
|
||||||
public double Price { get; set; }
|
public double Price { get; set; }
|
||||||
|
|
||||||
private Dictionary<int, (IComponentModel, int)>? _productComponents = null;
|
private Dictionary<int, (IComponentModel, int)>? _repairComponents = null;
|
||||||
|
|
||||||
[NotMapped]
|
[NotMapped]
|
||||||
public Dictionary<int, (IComponentModel, int)> RepairComponents
|
public Dictionary<int, (IComponentModel, int)> RepairComponents
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_productComponents == null)
|
if (_repairComponents == null)
|
||||||
{
|
{
|
||||||
_productComponents = Components
|
_repairComponents = Components
|
||||||
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
||||||
}
|
}
|
||||||
return _productComponents;
|
return _repairComponents;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,31 +69,31 @@ namespace CarRepairShopDatabaseImplement.Models
|
|||||||
|
|
||||||
public void UpdateComponents(CarRepairShopDatabase context, RepairBindingModel model)
|
public void UpdateComponents(CarRepairShopDatabase context, RepairBindingModel model)
|
||||||
{
|
{
|
||||||
var productComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
var repairComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
||||||
if (productComponents != null && productComponents.Count > 0)
|
if (repairComponents != null && repairComponents.Count > 0)
|
||||||
{ // удалили те, которых нет в модели
|
{ // удалили те, которых нет в модели
|
||||||
context.RepairComponents.RemoveRange(productComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId)));
|
context.RepairComponents.RemoveRange(repairComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId)));
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
// обновили количество у существующих записей
|
// обновили количество у существующих записей
|
||||||
foreach (var updateComponent in productComponents)
|
foreach (var updateComponent in repairComponents)
|
||||||
{
|
{
|
||||||
updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2;
|
updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2;
|
||||||
model.RepairComponents.Remove(updateComponent.ComponentId);
|
model.RepairComponents.Remove(updateComponent.ComponentId);
|
||||||
}
|
}
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
var product = context.Repairs.First(x => x.Id == Id);
|
var repair = context.Repairs.First(x => x.Id == Id);
|
||||||
foreach (var pc in model.RepairComponents)
|
foreach (var pc in model.RepairComponents)
|
||||||
{
|
{
|
||||||
context.RepairComponents.Add(new RepairComponent
|
context.RepairComponents.Add(new RepairComponent
|
||||||
{
|
{
|
||||||
Repair = product,
|
Repair = repair,
|
||||||
Component = context.Components.First(x => x.Id == pc.Key),
|
Component = context.Components.First(x => x.Id == pc.Key),
|
||||||
Count = pc.Value.Item2
|
Count = pc.Value.Item2
|
||||||
});
|
});
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
}
|
}
|
||||||
_productComponents = null;
|
_repairComponents = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user