101 lines
3.3 KiB
C#
101 lines
3.3 KiB
C#
using FoodOrdersContracts.BindingModels;
|
|
using FoodOrdersContracts.ViewModels;
|
|
using FoodOrdersDataModels.Models;
|
|
using System.Runtime.Serialization;
|
|
using System.Xml.Linq;
|
|
|
|
namespace FoodOrdersFileImplement.Models
|
|
{
|
|
[DataContract]
|
|
public class Dish : IDishModel
|
|
{
|
|
[DataMember]
|
|
public int Id { get; private set; }
|
|
|
|
[DataMember]
|
|
public string DishName { get; private set; } = string.Empty;
|
|
|
|
[DataMember]
|
|
public double Price { get; private set; }
|
|
|
|
public Dictionary<int, int> Components { get; private set; } = new();
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _DishComponents = null;
|
|
|
|
[DataMember]
|
|
public Dictionary<int, (IComponentModel, int)> DishComponents
|
|
{
|
|
get
|
|
{
|
|
if (_DishComponents == null)
|
|
{
|
|
var source = DataFileSingleton.GetInstance();
|
|
_DishComponents = Components.ToDictionary(x => x.Key,
|
|
y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
|
|
}
|
|
return _DishComponents;
|
|
}
|
|
}
|
|
|
|
public static Dish? Create(DishBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Dish()
|
|
{
|
|
Id = model.Id,
|
|
DishName = model.DishName,
|
|
Price = model.Price,
|
|
Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
|
|
};
|
|
}
|
|
|
|
public static Dish? Create(XElement element)
|
|
{
|
|
if (element == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Dish()
|
|
{
|
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|
DishName = element.Element("DishName")!.Value,
|
|
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
|
Components = element.Element("DishComponents")!.Elements("DishComponent")
|
|
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
|
|
};
|
|
}
|
|
|
|
public void Update(DishBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
DishName = model.DishName;
|
|
Price = model.Price;
|
|
Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
_DishComponents = null;
|
|
}
|
|
|
|
public DishViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
DishName = DishName,
|
|
Price = Price,
|
|
DishComponents = DishComponents
|
|
};
|
|
|
|
public XElement GetXElement => new("Dish",
|
|
new XAttribute("Id", Id),
|
|
new XElement("DishName", DishName),
|
|
new XElement("Price", Price.ToString()),
|
|
new XElement("DishComponents",
|
|
Components.Select(x => new XElement("DishComponent",
|
|
new XElement("Key", x.Key),
|
|
new XElement("Value", x.Value))).ToArray()));
|
|
}
|
|
}
|