From 6ca67882ecde9d51682dbd2ef38d4e5089a48fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 22:47:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=86=D0=B5=D0=BB=D1=8C=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D1=82=D1=80=D0=B0=D0=BD=D0=B7=D0=B0=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B8=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PastryStorage.cs | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs index aed74f0..3ea0953 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(); } @@ -78,16 +82,26 @@ namespace ConfectioneryDatabaseImplement.Implements 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; } } }