Починил баг с магазинами

This commit is contained in:
Никита Потапов 2024-05-20 11:06:33 +04:00
parent d51c8ddcab
commit 4bcf953c46

View File

@ -29,21 +29,12 @@ namespace SecuritySystemDatabaseImplement.Models
{ {
if (_shopSecures == null) if (_shopSecures == null)
{ {
// TODO _shopSecures = Secures
try .ToDictionary(
{ shopSecure => shopSecure.SecureId,
_shopSecures = Secures shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count)
.ToDictionary( );
shopSecure => shopSecure.SecureId,
shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count)
);
}
catch (Exception)
{
_shopSecures = new();
throw;
}
UsedSpace = _shopSecures.Select(x => x.Value.Item2).Sum(); UsedSpace = _shopSecures.Select(x => x.Value.Item2).Sum();
} }
return _shopSecures; return _shopSecures;
@ -85,14 +76,11 @@ namespace SecuritySystemDatabaseImplement.Models
}; };
public void UpdateSecures(SecuritySystemDatabase context, ShopBindingModel model) public void UpdateSecures(SecuritySystemDatabase context, ShopBindingModel model)
{ {
// TODO var shopSecures = context.ShopSecures.Where(rec => rec.ShopId == model.Id).ToList();
//context.ShopSecures.RemoveRange(context.ShopSecures.ToList());
//context.SaveChanges();
var shopSecures = context.ShopSecures.Where(shopSecure => shopSecure.SecureId == model.Id).ToList();
if (shopSecures != null && shopSecures.Count > 0) 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(); context.SaveChanges();
shopSecures = context.ShopSecures.Where(rec => rec.ShopId == model.Id).ToList(); shopSecures = context.ShopSecures.Where(rec => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей // обновили количество у существующих записей
@ -104,13 +92,13 @@ namespace SecuritySystemDatabaseImplement.Models
context.SaveChanges(); context.SaveChanges();
} }
var shop = context.Shops.First(x => x.Id == Id); 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 context.ShopSecures.Add(new ShopSecure
{ {
Shop = shop, Shop = shop,
Secure = context.Secures.First(x => x.Id == obj.Key), Secure = context.Secures.First(x => x.Id == elem.Key),
Count = obj.Value.Item2 Count = elem.Value.Item2
}); });
context.SaveChanges(); context.SaveChanges();
} }