PIbd-21. Anisin R.S. Lab work 03 #4
@ -70,16 +70,16 @@ namespace CarRepairShopDatabaseImplement.Implements
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var product = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (product == null)
|
||||
var repair = context.Repairs.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (repair == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
product.Update(model);
|
||||
repair.Update(model);
|
||||
context.SaveChanges();
|
||||
product.UpdateComponents(context, model);
|
||||
repair.UpdateComponents(context, model);
|
||||
transaction.Commit();
|
||||
return product.GetViewModel;
|
||||
return repair.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
@ -16,19 +16,19 @@ namespace CarRepairShopDatabaseImplement.Models
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _productComponents = null;
|
||||
private Dictionary<int, (IComponentModel, int)>? _repairComponents = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> RepairComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_productComponents == null)
|
||||
if (_repairComponents == null)
|
||||
{
|
||||
_productComponents = Components
|
||||
_repairComponents = Components
|
||||
.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)
|
||||
{
|
||||
var productComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
||||
if (productComponents != null && productComponents.Count > 0)
|
||||
var repairComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
||||
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();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in productComponents)
|
||||
foreach (var updateComponent in repairComponents)
|
||||
{
|
||||
updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2;
|
||||
model.RepairComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
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)
|
||||
{
|
||||
context.RepairComponents.Add(new RepairComponent
|
||||
{
|
||||
Repair = product,
|
||||
Repair = repair,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_productComponents = null;
|
||||
_repairComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user