PIbd-22_Katysheva_N.E._Pizz.../Pizzeria/PizzeriaFileImplement/Models/Pizza.cs

81 lines
3.0 KiB
C#
Raw Normal View History

2024-03-12 21:53:13 +04:00
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Models;
using System.Xml.Linq;
2024-03-12 23:45:49 +04:00
namespace PizzeriaFileImplement.Models
2024-03-12 21:53:13 +04:00
{
public class Pizza : IPizzaModel
{
public int Id { get; private set; }
public string PizzaName { get; private set; } = string.Empty;
public double Price { get; private set; }
2024-03-12 22:05:31 +04:00
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)
{
2024-03-12 23:45:49 +04:00
var source = DataFileSingleton.GetInstance();
_PizzaComponents = Components.ToDictionary(x => x.Key, y =>((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,y.Value));
2024-03-12 22:05:31 +04:00
}
return _PizzaComponents;
}
}
public static Pizza? Create(PizzaBindingModel model)
2024-03-12 21:53:13 +04:00
{
if (model == null)
{
return null;
}
return new Pizza()
{
Id = model.Id,
PizzaName = model.PizzaName,
Price = model.Price,
2024-03-12 22:05:31 +04:00
Components = model.PizzaComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
2024-03-12 21:53:13 +04:00
};
}
2024-03-12 22:05:31 +04:00
public static Pizza? Create(XElement element)
2024-03-12 21:53:13 +04:00
{
if (element == null)
{
return null;
}
2024-03-12 22:05:31 +04:00
return new Pizza()
2024-03-12 21:53:13 +04:00
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
2024-03-12 22:05:31 +04:00
PizzaName = element.Element("PizzaName")!.Value,
2024-03-12 21:53:13 +04:00
Price = Convert.ToDouble(element.Element("Price")!.Value),
2024-03-12 22:05:31 +04:00
Components = element.Element("PizzaComponents")!.Elements("PizzaComponent").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x =>Convert.ToInt32(x.Element("Value")?.Value))
2024-03-12 21:53:13 +04:00
};
}
2024-03-12 22:05:31 +04:00
public void Update(PizzaBindingModel model)
2024-03-12 21:53:13 +04:00
{
if (model == null)
{
return;
}
PizzaName = model.PizzaName;
Price = model.Price;
2024-03-12 22:05:31 +04:00
Components = model.PizzaComponents.ToDictionary(x => x.Key, x =>x.Value.Item2);
_PizzaComponents = null;
2024-03-12 21:53:13 +04:00
}
public PizzaViewModel GetViewModel => new()
{
Id = Id,
PizzaName = PizzaName,
Price = Price,
PizzaComponents = PizzaComponents
};
2024-03-12 22:05:31 +04:00
public XElement GetXElement => new("Pizza",
2024-03-13 12:15:44 +04:00
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()));
2024-03-12 21:53:13 +04:00
}
2024-03-12 22:05:31 +04:00
2024-03-12 21:53:13 +04:00
}