diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs index b694e88..aa72f38 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs @@ -72,25 +72,15 @@ namespace PrecastConcretePlantDatabaseImplement.Implements public ShopViewModel? Update(ShopBindingModel model) { using var context = new PrecastConcretePlantDataBase(); - using var transaction = context.Database.BeginTransaction(); - try + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) { - var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); - if (shop == null) - { - return null; - } - shop.Update(model); - context.SaveChanges(); - shop.UpdateReinforcedies(context, model); - transaction.Commit(); - return shop.GetViewModel; - } - catch - { - transaction.Rollback(); - throw; + return null; } + shop.Update(model); + shop.UpdateReinforcedies(context, model); + context.SaveChanges(); + return shop.GetViewModel; } public ShopViewModel? Delete(ShopBindingModel model) diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs index 4b737e8..fd0ff89 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs @@ -58,10 +58,13 @@ namespace PrecastConcretePlantDatabaseImplement.Models public void Update(ShopBindingModel model) { + if (model == null) + { + return; + } Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - ReinforcedMaxCount = model.ReinforcedMaxCount; } public ShopViewModel GetViewModel => new() @@ -76,30 +79,28 @@ namespace PrecastConcretePlantDatabaseImplement.Models public void UpdateReinforcedies(PrecastConcretePlantDataBase context, ShopBindingModel model) { - var shopReinforcedies = context.ShopReinforcedies.Where(rec => rec.ShopId == model.Id).ToList(); + var shopReinforcedies = context.ShopReinforcedies + .Where(rec => rec.ShopId == model.Id) + .ToList(); if (shopReinforcedies != null && shopReinforcedies.Count > 0) - { - context.ShopReinforcedies.RemoveRange(shopReinforcedies.Where(rec => !model.ShopReinforcedies.ContainsKey(rec.ReinforcedId))); - context.SaveChanges(); - - foreach (var updateReinforced in shopReinforcedies) + { + context.ShopReinforcedies + .RemoveRange(shopReinforcedies + .Where(rec => !model.ShopReinforcedies + .ContainsKey(rec.ReinforcedId))); + foreach (var updateReinforced in shopReinforcedies.Where(x => model.ShopReinforcedies.ContainsKey(x.ReinforcedId))) { updateReinforced.Count = model.ShopReinforcedies[updateReinforced.ReinforcedId].Item2; model.ShopReinforcedies.Remove(updateReinforced.ReinforcedId); } - context.SaveChanges(); } - var shop = context.Shops.First(x => x.Id == Id); - foreach (var st in model.ShopReinforcedies) + var shop = context.Shops.First(x => x.Id == model.Id); + shop.Reinforcedies.AddRange(model.ShopReinforcedies.Select(x => new ShopReinforced { - context.ShopReinforcedies.Add(new ShopReinforced - { - Shop = shop, - Reinforced = context.Reinforcedies.First(x => x.Id == st.Key), - Count = st.Value.Item2 - }); - context.SaveChanges(); - } + Reinforced = context.Reinforcedies.First(y => y.Id == x.Key), + Count = x.Value.Item2, + }).Except(shopReinforcedies ?? new())); + context.SaveChanges(); _shopReinforcedies = null; } }