Добавлены цельная транзакция при обновлении изделия

This commit is contained in:
Данияр Аглиуллов 2023-02-28 22:47:06 +04:00
parent f2ca847c6b
commit b2eb054e22

View File

@ -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();
}
@ -71,23 +75,33 @@ namespace ConfectioneryDatabaseImplement.Implements
newPastry = context.Pastries.Add(newPastry).Entity;
context.SaveChanges();
model.Id = newPastry.Id;
newPastry.UpdateComponents(context, model);
// newPastry.UpdateComponents(context, model);
return newPastry.GetViewModel;
}
public PastryViewModel? Update(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id);
if (pastry == null)
using var transaction = context.Database.BeginTransaction();
try
{
return null;
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();
transaction.Commit();
return pastry.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
pastry.Update(model);
pastry.UpdateComponents(context, model);
context.Update(pastry);
context.SaveChanges();
return pastry.GetViewModel;
}
}
}