PIbd-21_Putincev_D.M._FoodO.../FoodOrders/FoodOrdersDatabaseImplement/Models/Dish.cs

109 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using FoodOrdersDataModels.Models;
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using System.Runtime.Serialization;
namespace FoodOrdersDatabaseImplement.Models
{
[DataContract]
public class Dish : IDishModel
{
[DataMember]
public int Id { get; private set; }
[DataMember]
[Required]
public string DishName { get; private set; } = string.Empty;
[DataMember]
[Required]
public double Price { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _dishComponents = null;
[DataMember]
[NotMapped]
public Dictionary<int, (IComponentModel, int)> DishComponents
{
get
{
if (_dishComponents == null)
{
_dishComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _dishComponents;
}
}
[ForeignKey("DishId")]
public virtual List<DishComponent> Components { get; set; } = new();
[ForeignKey("DishId")]
public virtual List<Order> Orders { get; set; } = new();
public static Dish? Create(FoodOrdersDatabase context, DishBindingModel model)
{
var components = context.Components;
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)
{ // удалили те, которых нет в модели
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.Dishs.First(x => x.Id == Id);
foreach (var pc in model.DishComponents)
{
context.DishComponents.Add(new DishComponent
{
Dish = dish,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_dishComponents = null;
}
}
}