Merge branch 'Lab3_Base' into Lab3_Hard
This commit is contained in:
commit
2ae833bcd5
@ -79,7 +79,7 @@ namespace ConfectioneryDatabaseImplement.Models
|
||||
.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in pastryComponents)
|
||||
foreach (var updateComponent in pastryComponents.Where(x => model.PastryComponents.ContainsKey(x.ComponentId)))
|
||||
{
|
||||
updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2;
|
||||
model.PastryComponents.Remove(updateComponent.ComponentId);
|
||||
|
@ -47,6 +47,8 @@ namespace ConfectioneryDatabaseImplement.Implements
|
||||
}
|
||||
using var context = new ConfectioneryDatabase();
|
||||
return context.Pastries
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.Select(x => x.GetViewModel)
|
||||
.Where(x => x.PastryName.Contains(model.PastryName))
|
||||
.ToList();
|
||||
@ -56,6 +58,8 @@ namespace ConfectioneryDatabaseImplement.Implements
|
||||
{
|
||||
using var context = new ConfectioneryDatabase();
|
||||
return context.Pastries
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
@ -68,26 +72,33 @@ namespace ConfectioneryDatabaseImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
newPastry = context.Pastries.Add(newPastry).Entity;
|
||||
context.Pastries.Add(newPastry);
|
||||
context.SaveChanges();
|
||||
model.Id = newPastry.Id;
|
||||
newPastry.UpdateComponents(context, model);
|
||||
return newPastry.GetViewModel;
|
||||
}
|
||||
|
||||
public PastryViewModel? Update(PastryBindingModel model)
|
||||
{
|
||||
using var context = new ConfectioneryDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (pastry == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
pastry.Update(model);
|
||||
pastry.UpdateComponents(context, model);
|
||||
context.Update(pastry);
|
||||
context.SaveChanges();
|
||||
pastry.UpdateComponents(context, model);
|
||||
transaction.Commit();
|
||||
return pastry.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user