2024-05-16 17:13:55 +04:00
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
2024-05-24 01:56:34 +04:00
|
|
|
|
using SushiBarDataModels.Models;
|
2024-05-16 17:13:55 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-05-15 20:43:24 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Models
|
|
|
|
|
{
|
2024-05-24 01:56:34 +04:00
|
|
|
|
public class Dish : IDishModel
|
2024-05-15 20:43:24 +04:00
|
|
|
|
{
|
|
|
|
|
[Key]
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string DishName { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string Category { get; set; } = string.Empty;
|
|
|
|
|
|
2024-05-24 01:56:34 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
|
2024-05-15 20:43:24 +04:00
|
|
|
|
[ForeignKey("DishId")]
|
2024-05-24 01:56:34 +04:00
|
|
|
|
public virtual List<DishIngredient> Ingredients { get; set; } = new();
|
2024-05-15 20:43:24 +04:00
|
|
|
|
|
|
|
|
|
[ForeignKey("DishId")]
|
2024-05-15 21:14:04 +04:00
|
|
|
|
public virtual List<ChequeItem> ChequeItems { get; set; } = new();
|
2024-05-16 17:13:55 +04:00
|
|
|
|
|
2024-05-24 01:56:34 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 17:13:55 +04:00
|
|
|
|
public static Dish Create(SushiBarDatabase Context, DishBindingModel Model)
|
|
|
|
|
{
|
|
|
|
|
return new Dish()
|
|
|
|
|
{
|
2024-05-24 01:56:34 +04:00
|
|
|
|
Id = Model.Id,
|
2024-05-16 17:13:55 +04:00
|
|
|
|
DishName = Model.DishName,
|
|
|
|
|
Category = Model.Category,
|
2024-05-24 01:56:34 +04:00
|
|
|
|
Price = Model.Price,
|
|
|
|
|
Ingredients = Model.DishIngredients.Select(x => new DishIngredient
|
2024-05-16 17:13:55 +04:00
|
|
|
|
{
|
2024-05-24 01:56:34 +04:00
|
|
|
|
Ingredient = Context.Ingredients.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2,
|
2024-05-16 17:13:55 +04:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(DishBindingModel Model)
|
|
|
|
|
{
|
|
|
|
|
DishName = Model.DishName;
|
|
|
|
|
Category = Model.Category;
|
2024-05-24 01:56:34 +04:00
|
|
|
|
Price = Model.Price;
|
2024-05-16 17:13:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DishViewModel ViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
DishName = DishName,
|
2024-05-24 01:56:34 +04:00
|
|
|
|
Price = Price,
|
2024-05-16 17:13:55 +04:00
|
|
|
|
Category = Category,
|
2024-05-24 01:56:34 +04:00
|
|
|
|
DishIngredients = DishIngredients,
|
2024-05-16 17:13:55 +04:00
|
|
|
|
};
|
|
|
|
|
|
2024-05-24 01:56:34 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2024-05-16 17:13:55 +04:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-24 01:56:34 +04:00
|
|
|
|
*/
|
2024-05-15 20:43:24 +04:00
|
|
|
|
}
|
|
|
|
|
}
|