diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs index aed74f0..45eafbb 100644 --- a/ConfectioneryDatabaseImplement/PastryStorage.cs +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -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; } } }