Третья усложнённая лабораторная работа. Фикс пополнения и списания

This commit is contained in:
abazov73 2023-06-02 20:04:06 +04:00
parent 904ba78dde
commit 01defeaeaa
2 changed files with 24 additions and 21 deletions

View File

@ -108,7 +108,7 @@ namespace ConfectioneryDataBaseImplement.Implements
{
using ConfectioneryDatabase context = new ConfectioneryDatabase();
var transaction = context.Database.BeginTransaction();
foreach (Shop shop in context.Shops)
foreach (Shop shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry))
{
int freeShopSpace = shop.PastryCapacity - shop.ShopPastries.Select(y => y.Value.Item2).Sum();
if (freeShopSpace > 0)
@ -161,7 +161,7 @@ namespace ConfectioneryDataBaseImplement.Implements
{
using ConfectioneryDatabase context = new ConfectioneryDatabase();
var transaction = context.Database.BeginTransaction();
foreach (Shop shop in context.Shops)
foreach (Shop shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry))
{
int shopPastryCount = shop.ShopPastries.Select(x => x.Value).Where(x => x.Item1.Id == pastryId).Sum(x => x.Item2);
if (count - shopPastryCount >= 0)
@ -177,12 +177,12 @@ namespace ConfectioneryDataBaseImplement.Implements
shop.ShopPastries[pastryId] = shopPastry;
}
shop.RemapPastries(context);
}
if (count == 0)
{
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}

View File

@ -69,17 +69,21 @@ namespace ConfectioneryDataBaseImplement.Models
{
var list = context.ShopPastries.ToList();
var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList();
if (shopPastries != null)
if (shopPastries != null && shopPastries.Count > 0)
{
// удалили те, которых нет в модели
context.ShopPastries.RemoveRange(shopPastries.Where(rec => !model.ShopPastries.ContainsKey(rec.PastryId)));
context.ShopPastries.RemoveRange(shopPastries.Where((ShopPastry rec) => !model.ShopPastries.ContainsKey(rec.PastryId)));
context.SaveChanges();
shopPastries = context.ShopPastries.Where((ShopPastry rec) => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей
foreach (var updateIngredient in shopPastries)
{
updateIngredient.Count = model.ShopPastries[updateIngredient.PastryId].Item2;
model.ShopPastries.Remove(updateIngredient.PastryId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var sp in model.ShopPastries)
{
@ -93,7 +97,6 @@ namespace ConfectioneryDataBaseImplement.Models
}
_shopPastries = null;
}
}
public ShopViewModel GetViewModel => new()
{