PIbd-22_Shabunov_O.A._SushiBar/SushiBarDatabaseImplement/Models/Dish.cs
2024-05-24 01:56:34 +04:00

152 lines
5.3 KiB
C#

using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SushiBarDatabaseImplement.Models
{
public class Dish : IDishModel
{
[Key]
public int Id { get; set; }
[Required]
public string DishName { get; set; } = string.Empty;
[Required]
public string Category { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[ForeignKey("DishId")]
public virtual List<DishIngredient> Ingredients { get; set; } = new();
[ForeignKey("DishId")]
public virtual List<ChequeItem> ChequeItems { get; set; } = new();
private Dictionary<int, (IIngredientModel, int)>? _dishIngredients = null;
[NotMapped]
public Dictionary<int, (IIngredientModel, int)> DishIngredients
{
get
{
if (_dishIngredients == null)
{
_dishIngredients = Ingredients.ToDictionary(DishIngredient => DishIngredient.IngredientId, DishIngredient =>
(DishIngredient.Ingredient as IIngredientModel, DishIngredient.Count));
}
return _dishIngredients;
}
}
public static Dish Create(SushiBarDatabase Context, DishBindingModel Model)
{
return new Dish()
{
Id = Model.Id,
DishName = Model.DishName,
Category = Model.Category,
Price = Model.Price,
Ingredients = Model.DishIngredients.Select(x => new DishIngredient
{
Ingredient = Context.Ingredients.First(y => y.Id == x.Key),
Count = x.Value.Item2,
}).ToList()
};
}
public void Update(DishBindingModel Model)
{
DishName = Model.DishName;
Category = Model.Category;
Price = Model.Price;
}
public DishViewModel ViewModel => new()
{
Id = Id,
DishName = DishName,
Price = Price,
Category = Category,
DishIngredients = DishIngredients,
};
public void UpdateIngredients(SushiBarDatabase Context, DishBindingModel Model)
{
var IngredientsForThisDish = Context.DishIngredients.Where(x => x.DishId == Model.Id).ToList();
if (IngredientsForThisDish.Count > 0)
{
// Delete DishIngredient records for selected dish if there is no record with such ingredient in passed model
Context.DishIngredients.RemoveRange(IngredientsForThisDish.Where(x => !Model.DishIngredients.ContainsKey(x.IngredientId)));
Context.SaveChanges();
foreach (var DishIngredientToUpdate in IngredientsForThisDish)
{
DishIngredientToUpdate.Count = Model.DishIngredients[DishIngredientToUpdate.IngredientId].Item2;
Model.DishIngredients.Remove(DishIngredientToUpdate.IngredientId);
}
Context.SaveChanges();
}
var Dish = Context.Dishes.First(x => x.Id == Id);
foreach (var DishIngredient in Model.DishIngredients)
{
Context.DishIngredients.Add(new DishIngredient
{
Dish = Dish,
Ingredient = Context.Ingredients.First(x => x.Id == DishIngredient.Key),
Count = DishIngredient.Value.Item2
});
Context.SaveChanges();
}
_dishIngredients = null;
}
/*
public void UpdateIngredients(SushiBarDatabase Context, DishBindingModel Model)
{
var IngredientsForThisDish = Context.DishIngredients.Where(x => x.DishId == Model.Id).ToList();
if (IngredientsForThisDish.Count > 0)
{
// Delete DishIngredient records for selected dish if there is no record with such ingredient in passed model
var UsedComponentIds = Model.Ingredients.Select(x => x.IngredientId).ToList();
Context.DishIngredients.RemoveRange(IngredientsForThisDish.Where(x => !UsedComponentIds.Contains(x.IngredientId)));
Context.SaveChanges();
foreach (var DishIngredientToUpdate in IngredientsForThisDish)
{
DishIngredientToUpdate.Count = Model.Ingredients.First(x => x.IngredientId == DishIngredientToUpdate.IngredientId).Count;
Model.Ingredients.Remove(Model.Ingredients.First(x => x.IngredientId == DishIngredientToUpdate.IngredientId));
}
Context.SaveChanges();
}
var Dish = Context.Dishes.First(x => x.Id == Id);
foreach (var DishIngredient in Model.Ingredients)
{
Context.DishIngredients.Add(new DishIngredient
{
Dish = Dish,
Ingredient = Context.Ingredients.First(x => x.Id == DishIngredient.IngredientId),
Count = DishIngredient.Count
});
Context.SaveChanges();
}
}
*/
}
}