From 4bcf953c46beccfb6da4c80c5a2e54aaffd8c88a Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Mon, 20 May 2024 11:06:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=87=D0=B8=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B3=20=D1=81=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/Shop.cs | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs index 728ac2b..f5a941c 100644 --- a/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs @@ -29,21 +29,12 @@ namespace SecuritySystemDatabaseImplement.Models { if (_shopSecures == null) { - // TODO - try - { - _shopSecures = Secures - .ToDictionary( - shopSecure => shopSecure.SecureId, - shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count) - ); - } - catch (Exception) - { - _shopSecures = new(); - throw; - } - + _shopSecures = Secures + .ToDictionary( + shopSecure => shopSecure.SecureId, + shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count) + ); + UsedSpace = _shopSecures.Select(x => x.Value.Item2).Sum(); } return _shopSecures; @@ -85,14 +76,11 @@ namespace SecuritySystemDatabaseImplement.Models }; public void UpdateSecures(SecuritySystemDatabase context, ShopBindingModel model) { - // TODO - //context.ShopSecures.RemoveRange(context.ShopSecures.ToList()); - //context.SaveChanges(); - var shopSecures = context.ShopSecures.Where(shopSecure => shopSecure.SecureId == model.Id).ToList(); + var shopSecures = context.ShopSecures.Where(rec => rec.ShopId == model.Id).ToList(); if (shopSecures != null && shopSecures.Count > 0) { // удалили те, которых нет в модели - context.ShopSecures.RemoveRange(shopSecures.Where(shopSecure => !model.ShopSecures.ContainsKey(shopSecure.SecureId) || shopSecure.Count == 0)); + context.ShopSecures.RemoveRange(shopSecures.Where(rec => !model.ShopSecures.ContainsKey(rec.SecureId))); context.SaveChanges(); shopSecures = context.ShopSecures.Where(rec => rec.ShopId == model.Id).ToList(); // обновили количество у существующих записей @@ -104,13 +92,13 @@ namespace SecuritySystemDatabaseImplement.Models context.SaveChanges(); } var shop = context.Shops.First(x => x.Id == Id); - foreach (var obj in model.ShopSecures) + foreach (var elem in model.ShopSecures) { context.ShopSecures.Add(new ShopSecure { Shop = shop, - Secure = context.Secures.First(x => x.Id == obj.Key), - Count = obj.Value.Item2 + Secure = context.Secures.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 }); context.SaveChanges(); }