This commit is contained in:
dasha 2023-02-27 22:16:28 +04:00
parent c1f92ed0c6
commit 46b8e09d6a
2 changed files with 19 additions and 19 deletions

View File

@ -16,19 +16,19 @@ namespace SushiBarDatabaseImplement.Models
[Required]
public double Price { get; set; }
private Dictionary<int, (IIngredientModel, int)>? _productIngredients = null;
private Dictionary<int, (IIngredientModel, int)>? _sushiIngredients = null;
[NotMapped]
public Dictionary<int, (IIngredientModel, int)> SushiIngredients
{
get
{
if (_productIngredients == null)
if (_sushiIngredients == null)
{
_productIngredients = Ingredients
_sushiIngredients = Ingredients
.ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count));
}
return _productIngredients;
return _sushiIngredients;
}
}
@ -68,31 +68,31 @@ namespace SushiBarDatabaseImplement.Models
public void UpdateIngredients(SushiBarDatabase context, SushiBindingModel model)
{
var productIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList();
if (productIngredients != null && productIngredients.Count > 0)
var sushiIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList();
if (sushiIngredients != null && sushiIngredients.Count > 0)
{ // удалили те, которых нет в модели
context.SushiIngredients.RemoveRange(productIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId)));
context.SushiIngredients.RemoveRange(sushiIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateIngredient in productIngredients)
foreach (var updateIngredient in sushiIngredients)
{
updateIngredient.Count = model.SushiIngredients[updateIngredient.IngredientId].Item2;
model.SushiIngredients.Remove(updateIngredient.IngredientId);
}
context.SaveChanges();
}
var product = context.SushiList.First(x => x.Id == Id);
foreach (var pc in model.SushiIngredients)
var sushi = context.SushiList.First(x => x.Id == Id);
foreach (var si in model.SushiIngredients)
{
context.SushiIngredients.Add(new SushiIngredient
{
Sushi = product,
Ingredient = context.Ingredients.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
Sushi = sushi,
Ingredient = context.Ingredients.First(x => x.Id == si.Key),
Count = si.Value.Item2
});
context.SaveChanges();
}
_productIngredients = null;
_sushiIngredients = null;
}
}
}

View File

@ -11,17 +11,17 @@ namespace SushiBarFileImplement.Models
public string SushiName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Ingredients { get; private set; } = new();
private Dictionary<int, (IIngredientModel, int)>? _productIngredients = null;
private Dictionary<int, (IIngredientModel, int)>? _sushiIngredients = null;
public Dictionary<int, (IIngredientModel, int)> SushiIngredients
{
get
{
if (_productIngredients == null)
if (_sushiIngredients == null)
{
var source = DataFileSingleton.GetInstance();
_productIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value));
_sushiIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value));
}
return _productIngredients;
return _sushiIngredients;
}
}
public static Sushi? Create(SushiBindingModel model)
@ -63,7 +63,7 @@ namespace SushiBarFileImplement.Models
Price = model.Price;
Ingredients = model.SushiIngredients.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_productIngredients = null;
_sushiIngredients = null;
}
public SushiViewModel GetViewModel => new()
{