PIbd-22_Katysheva_N.E._Pizz.../Pizzeria/PizzeriaFileImplement/Models/Pizza.cs
2024-03-26 23:16:58 +04:00

81 lines
3.0 KiB
C#

using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Models;
using System.Xml.Linq;
namespace PizzeriaFileImplement.Models
{
public class Pizza : IPizzaModel
{
public int Id { get; private set; }
public string PizzaName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _PizzaComponents = null;
public Dictionary<int, (IComponentModel, int)> PizzaComponents
{
get
{
if (_PizzaComponents == null)
{
var source = DataFileSingleton.GetInstance();
_PizzaComponents = Components.ToDictionary(x => x.Key, y =>((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,y.Value));
}
return _PizzaComponents;
}
}
public static Pizza? Create(PizzaBindingModel model)
{
if (model == null)
{
return null;
}
return new Pizza()
{
Id = model.Id,
PizzaName = model.PizzaName,
Price = model.Price,
Components = model.PizzaComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Pizza? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Pizza()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PizzaName = element.Element("PizzaName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("PizzaComponents")!.Elements("PizzaComponent").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x =>Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(PizzaBindingModel model)
{
if (model == null)
{
return;
}
PizzaName = model.PizzaName;
Price = model.Price;
Components = model.PizzaComponents.ToDictionary(x => x.Key, x =>x.Value.Item2);
_PizzaComponents = null;
}
public PizzaViewModel GetViewModel => new()
{
Id = Id,
PizzaName = PizzaName,
Price = Price,
PizzaComponents = PizzaComponents
};
public XElement GetXElement => new("Pizza",
new XAttribute("Id", Id),
new XElement("PizzaName", PizzaName),
new XElement("Price", Price.ToString()),
new XElement("PizzaComponents", Components.Select(x => new XElement("PizzaComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}