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

View File

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