99 lines
3.6 KiB
C#
99 lines
3.6 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 Sushi : ISushiModel
|
|||
|
{
|
|||
|
public int Id { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public string SushiName { get; set; } = string.Empty;
|
|||
|
|
|||
|
[Required]
|
|||
|
public double Price { get; set; }
|
|||
|
|
|||
|
private Dictionary<int, (IIngredientModel, int)>? _productIngredients = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, (IIngredientModel, int)> SushiIngredients
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_productIngredients == null)
|
|||
|
{
|
|||
|
_productIngredients = Ingredients
|
|||
|
.ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count));
|
|||
|
}
|
|||
|
return _productIngredients;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[ForeignKey("SushiId")]
|
|||
|
public virtual List<SushiIngredient> Ingredients { get; set; } = new();
|
|||
|
[ForeignKey("SushiId")]
|
|||
|
public virtual List<Order> Orders { get; set; } = new();
|
|||
|
|
|||
|
public static Sushi Create(SushiBarDatabase context, SushiBindingModel model)
|
|||
|
{
|
|||
|
return new Sushi()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
SushiName = model.SushiName,
|
|||
|
Price = model.Price,
|
|||
|
Ingredients = model.SushiIngredients.Select(x => new SushiIngredient
|
|||
|
{
|
|||
|
Ingredient = context.Ingredients.First(y => y.Id == x.Key),
|
|||
|
Count = x.Value.Item2
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public void Update(SushiBindingModel model)
|
|||
|
{
|
|||
|
SushiName = model.SushiName;
|
|||
|
Price = model.Price;
|
|||
|
}
|
|||
|
|
|||
|
public SushiViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
SushiName = SushiName,
|
|||
|
Price = Price,
|
|||
|
SushiIngredients = SushiIngredients
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateIngredients(SushiBarDatabase context, SushiBindingModel model)
|
|||
|
{
|
|||
|
var productIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList();
|
|||
|
if (productIngredients != null && productIngredients.Count > 0)
|
|||
|
{ // удалили те, которых нет в модели
|
|||
|
context.SushiIngredients.RemoveRange(productIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId)));
|
|||
|
context.SaveChanges();
|
|||
|
// обновили количество у существующих записей
|
|||
|
foreach (var updateIngredient in productIngredients)
|
|||
|
{
|
|||
|
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)
|
|||
|
{
|
|||
|
context.SushiIngredients.Add(new SushiIngredient
|
|||
|
{
|
|||
|
Sushi = product,
|
|||
|
Ingredient = context.Ingredients.First(x => x.Id == pc.Key),
|
|||
|
Count = pc.Value.Item2
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_productIngredients = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|