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

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)
{
// 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();
}