2023-02-27 22:16:28 +04:00
|
|
|
|
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; }
|
|
|
|
|
|
2023-02-27 22:16:28 +04:00
|
|
|
|
private Dictionary<int, (IIngredientModel, int)>? _sushiIngredients = null;
|
2023-02-27 22:16:28 +04:00
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IIngredientModel, int)> SushiIngredients
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-02-27 22:16:28 +04:00
|
|
|
|
if (_sushiIngredients == null)
|
2023-02-27 22:16:28 +04:00
|
|
|
|
{
|
2023-02-27 22:16:28 +04:00
|
|
|
|
_sushiIngredients = Ingredients
|
2023-02-27 22:16:28 +04:00
|
|
|
|
.ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count));
|
|
|
|
|
}
|
2023-02-27 22:16:28 +04:00
|
|
|
|
return _sushiIngredients;
|
2023-02-27 22:16:28 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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)
|
|
|
|
|
{
|
2023-02-27 22:16:28 +04:00
|
|
|
|
var sushiIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList();
|
|
|
|
|
if (sushiIngredients != null && sushiIngredients.Count > 0)
|
2023-02-27 22:16:28 +04:00
|
|
|
|
{ // удалили те, которых нет в модели
|
2023-02-27 22:16:28 +04:00
|
|
|
|
context.SushiIngredients.RemoveRange(sushiIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId)));
|
2023-02-27 22:16:28 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
2023-02-27 22:16:28 +04:00
|
|
|
|
foreach (var updateIngredient in sushiIngredients)
|
2023-02-27 22:16:28 +04:00
|
|
|
|
{
|
|
|
|
|
updateIngredient.Count = model.SushiIngredients[updateIngredient.IngredientId].Item2;
|
|
|
|
|
model.SushiIngredients.Remove(updateIngredient.IngredientId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2023-03-10 16:44:26 +04:00
|
|
|
|
var sushi = context.ListSushi.First(x => x.Id == Id);
|
2023-02-27 22:16:28 +04:00
|
|
|
|
foreach (var si in model.SushiIngredients)
|
2023-02-27 22:16:28 +04:00
|
|
|
|
{
|
|
|
|
|
context.SushiIngredients.Add(new SushiIngredient
|
|
|
|
|
{
|
2023-02-27 22:16:28 +04:00
|
|
|
|
Sushi = sushi,
|
|
|
|
|
Ingredient = context.Ingredients.First(x => x.Id == si.Key),
|
|
|
|
|
Count = si.Value.Item2
|
2023-02-27 22:16:28 +04:00
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2023-02-27 22:16:28 +04:00
|
|
|
|
_sushiIngredients = null;
|
2023-02-27 22:16:28 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|