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

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(); using ConfectioneryDatabase context = new ConfectioneryDatabase();
var transaction = context.Database.BeginTransaction(); 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(); int freeShopSpace = shop.PastryCapacity - shop.ShopPastries.Select(y => y.Value.Item2).Sum();
if (freeShopSpace > 0) if (freeShopSpace > 0)
@ -161,7 +161,7 @@ namespace ConfectioneryDataBaseImplement.Implements
{ {
using ConfectioneryDatabase context = new ConfectioneryDatabase(); using ConfectioneryDatabase context = new ConfectioneryDatabase();
var transaction = context.Database.BeginTransaction(); 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); int shopPastryCount = shop.ShopPastries.Select(x => x.Value).Where(x => x.Item1.Id == pastryId).Sum(x => x.Item2);
if (count - shopPastryCount >= 0) if (count - shopPastryCount >= 0)
@ -177,11 +177,11 @@ namespace ConfectioneryDataBaseImplement.Implements
shop.ShopPastries[pastryId] = shopPastry; shop.ShopPastries[pastryId] = shopPastry;
} }
shop.RemapPastries(context); shop.RemapPastries(context);
if (count == 0) }
{ if (count == 0)
transaction.Commit(); {
return true; transaction.Commit();
} return true;
} }
transaction.Rollback(); transaction.Rollback();
return false; return false;

View File

@ -69,30 +69,33 @@ namespace ConfectioneryDataBaseImplement.Models
{ {
var list = context.ShopPastries.ToList(); var list = context.ShopPastries.ToList();
var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).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(); context.SaveChanges();
shopPastries = context.ShopPastries.Where((ShopPastry rec) => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей // обновили количество у существующих записей
foreach (var updateIngredient in shopPastries) foreach (var updateIngredient in shopPastries)
{ {
updateIngredient.Count = model.ShopPastries[updateIngredient.PastryId].Item2; updateIngredient.Count = model.ShopPastries[updateIngredient.PastryId].Item2;
model.ShopPastries.Remove(updateIngredient.PastryId); model.ShopPastries.Remove(updateIngredient.PastryId);
} }
var shop = context.Shops.First(x => x.Id == Id); context.SaveChanges();
foreach (var sp in model.ShopPastries)
{
context.ShopPastries.Add(new ShopPastry
{
Shop = shop,
Pastry = context.Pastrys.First(x => x.Id == sp.Key),
Count = sp.Value.Item2
});
context.SaveChanges();
}
_shopPastries = null;
} }
var shop = context.Shops.First(x => x.Id == Id);
foreach (var sp in model.ShopPastries)
{
context.ShopPastries.Add(new ShopPastry
{
Shop = shop,
Pastry = context.Pastrys.First(x => x.Id == sp.Key),
Count = sp.Value.Item2
});
context.SaveChanges();
}
_shopPastries = null;
} }
public ShopViewModel GetViewModel => new() public ShopViewModel GetViewModel => new()