PIbd-21_Raspaev_N.I._FoodOr.../FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs

100 lines
3.6 KiB
C#
Raw Normal View History

2023-02-27 21:51:16 +04:00
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FoodOrdersDatabaseImplement.Models
{
public class Dish : IDishModel
{
public int Id { get; set; }
[Required]
public string DishName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _dishComponents = null;
2023-03-23 18:18:58 +04:00
//??
2023-02-27 21:51:16 +04:00
[NotMapped]
public Dictionary<int, (IComponentModel, int)> DishComponents
{
get
{
if (_dishComponents == null)
{
2023-03-23 18:18:58 +04:00
_dishComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
2023-02-27 21:51:16 +04:00
}
return _dishComponents;
}
}
[ForeignKey("DishId")]
public virtual List<DishComponent> Components { get; set; } = new();
2023-03-28 16:41:11 +04:00
[ForeignKey("DishId")]
public virtual List<Order> Orders { get; set; } = new();
2023-02-27 21:51:16 +04:00
public static Dish Create(FoodOrdersDatabase context, DishBindingModel model)
{
return new Dish()
{
Id = model.Id,
DishName = model.DishName,
Price = model.Price,
Components = model.DishComponents.Select(x => new DishComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(DishBindingModel model)
{
DishName = model.DishName;
Price = model.Price;
}
public DishViewModel GetViewModel => new()
{
Id = Id,
DishName = DishName,
Price = Price,
DishComponents = DishComponents
};
public void UpdateComponents(FoodOrdersDatabase context, DishBindingModel model)
{
var dishComponents = context.DishComponents.Where(rec => rec.DishId == model.Id).ToList();
if (dishComponents != null && dishComponents.Count > 0)
2023-03-23 18:18:58 +04:00
{ // удалили те в бд, которых нет в модели
2023-02-27 21:51:16 +04:00
context.DishComponents.RemoveRange(dishComponents.Where(rec => !model.DishComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in dishComponents)
{
updateComponent.Count = model.DishComponents[updateComponent.ComponentId].Item2;
model.DishComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var dish = context.Dishes.First(x => x.Id == Id);
2023-03-23 18:18:58 +04:00
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
foreach (var dc in model.DishComponents)
2023-02-27 21:51:16 +04:00
{
context.DishComponents.Add(new DishComponent
{
Dish = dish,
2023-03-23 18:18:58 +04:00
Component = context.Components.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
2023-02-27 21:51:16 +04:00
});
context.SaveChanges();
}
_dishComponents = null;
}
}
}