diff --git a/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs index f0e52b0..cd84e39 100644 --- a/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs +++ b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs @@ -4,6 +4,7 @@ using ConfectioneryContracts.StoragesContracts; using ConfectioneryContracts.ViewModels; using ConfectioneryDataBaseImplement.Models; using Microsoft.EntityFrameworkCore; +using System.Transactions; using System; using System.Collections.Generic; using System.Linq; @@ -126,8 +127,7 @@ namespace ConfectioneryDataBaseImplement.Implements shop.ShopPastries.Add(pastryId, (pastry, count)); } shop.RemapPastries(context); - transaction.Commit(); - return true; + count = 0; } else { @@ -136,18 +136,23 @@ namespace ConfectioneryDataBaseImplement.Implements if (shop.ShopPastries.TryGetValue(pastryId, out var pastryCount)) { var shopPastry = shop.ShopPastries[pastryId]; - shopPastry.Item2 = pastryCount.Item2 + count; + shopPastry.Item2 = pastryCount.Item2 + freeShopSpace; shop.ShopPastries[pastryId] = shopPastry; } else { Pastry pastry = context.Pastrys.First(x => x.Id == pastryId); - shop.ShopPastries.Add(pastryId, (pastry, count)); + shop.ShopPastries.Add(pastryId, (pastry, freeShopSpace)); } shop.RemapPastries(context); } } } + 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 e90d635..a5999c1 100644 --- a/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs @@ -67,8 +67,9 @@ namespace ConfectioneryDataBaseImplement.Models public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model) { + var list = context.ShopPastries.ToList(); var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList(); - if (shopPastries != null && shopPastries.Count > 0) + if (shopPastries != null) { // удалили те, которых нет в модели context.ShopPastries.RemoveRange(shopPastries.Where(rec => !model.ShopPastries.ContainsKey(rec.PastryId)));