From 01defeaeaa0e05e821495ddd95b87c3532526d8a Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Fri, 2 Jun 2023 20:04:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20=D1=83?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D1=91=D0=BD=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0.=20=D0=A4?= =?UTF-8?q?=D0=B8=D0=BA=D1=81=20=D0=BF=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D1=81=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 14 ++++----- .../Models/Shop.cs | 31 ++++++++++--------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs index cd84e39..1e811f7 100644 --- a/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs +++ b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs @@ -108,7 +108,7 @@ namespace ConfectioneryDataBaseImplement.Implements { using ConfectioneryDatabase context = new ConfectioneryDatabase(); var transaction = context.Database.BeginTransaction(); - foreach (Shop shop in context.Shops) + foreach (Shop shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry)) { int freeShopSpace = shop.PastryCapacity - shop.ShopPastries.Select(y => y.Value.Item2).Sum(); if (freeShopSpace > 0) @@ -161,7 +161,7 @@ namespace ConfectioneryDataBaseImplement.Implements { using ConfectioneryDatabase context = new ConfectioneryDatabase(); var transaction = context.Database.BeginTransaction(); - foreach (Shop shop in context.Shops) + foreach (Shop shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry)) { int shopPastryCount = shop.ShopPastries.Select(x => x.Value).Where(x => x.Item1.Id == pastryId).Sum(x => x.Item2); if (count - shopPastryCount >= 0) @@ -177,11 +177,11 @@ namespace ConfectioneryDataBaseImplement.Implements shop.ShopPastries[pastryId] = shopPastry; } shop.RemapPastries(context); - if (count == 0) - { - transaction.Commit(); - return true; - } + } + if (count == 0) + { + transaction.Commit(); + return true; } transaction.Rollback(); return false; diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs index a5999c1..89252d8 100644 --- a/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs @@ -69,30 +69,33 @@ namespace ConfectioneryDataBaseImplement.Models { var list = context.ShopPastries.ToList(); var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList(); - if (shopPastries != null) + if (shopPastries != null && shopPastries.Count > 0) { // удалили те, которых нет в модели - context.ShopPastries.RemoveRange(shopPastries.Where(rec => !model.ShopPastries.ContainsKey(rec.PastryId))); + context.ShopPastries.RemoveRange(shopPastries.Where((ShopPastry rec) => !model.ShopPastries.ContainsKey(rec.PastryId))); context.SaveChanges(); + + shopPastries = context.ShopPastries.Where((ShopPastry rec) => rec.ShopId == model.Id).ToList(); // обновили количество у существующих записей foreach (var updateIngredient in shopPastries) { updateIngredient.Count = model.ShopPastries[updateIngredient.PastryId].Item2; model.ShopPastries.Remove(updateIngredient.PastryId); } - var shop = context.Shops.First(x => x.Id == Id); - foreach (var sp in model.ShopPastries) - { - context.ShopPastries.Add(new ShopPastry - { - Shop = shop, - Pastry = context.Pastrys.First(x => x.Id == sp.Key), - Count = sp.Value.Item2 - }); - context.SaveChanges(); - } - _shopPastries = null; + context.SaveChanges(); } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var sp in model.ShopPastries) + { + context.ShopPastries.Add(new ShopPastry + { + Shop = shop, + Pastry = context.Pastrys.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _shopPastries = null; } public ShopViewModel GetViewModel => new()