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

66 lines
2.1 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;
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<int, (IComponentModel, int)> PizzaComponents { get; private set; } = new Dictionary<int, (IComponentModel, int)>();
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
};
}
}