From b2eb054e2286c7847e6e7e6ca3593807bce5ca4a 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 | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) 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; } } }