using PizzeriaContracts.BindingModels; using PizzeriaContracts.ViewModels; using PizzeriaDataModels.Models; using System.Xml.Linq; namespace AbstractShopFileImplement.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 PizzaComponents { get; private set; } = new Dictionary(); public static Pizza? Create(PizzaBindingModel? model) { if (model == null) { return null; } return new Pizza() { Id = model.Id, PizzaName = model.PizzaName, Price = model.Price, PizzaComponents = model.PizzaComponents }; } public static Product? Create(XElement element) { if (element == null) { return null; } return new Product() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ProductName = element.Element("ProductName")!.Value, Price = Convert.ToDouble(element.Element("Price")!.Value), Components = element.Element("ProductComponents")!.Elements("ProductComponent") .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; PizzaComponents = model.PizzaComponents; } public PizzaViewModel GetViewModel => new() { Id = Id, PizzaName = PizzaName, Price = Price, PizzaComponents = PizzaComponents }; } }