PIbd-21_Balberova_D.N._Sush.../SushiBar/SushiBarFileImplement/Models/Ingredient.cs

60 lines
1.8 KiB
C#

using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.Xml.Linq;
namespace SushiBarFileImplement.Models
{
public class Ingredient : IIngredientModel
{
public int Id { get; private set; }
public string IngredientName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Ingredient? Create(IngredientBindingModel model)
{
if (model == null)
{
return null;
}
return new Ingredient()
{
Id = model.Id,
IngredientName = model.IngredientName,
Cost = model.Cost
};
}
public static Ingredient? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Ingredient()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
IngredientName = element.Element("IngredientName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(IngredientBindingModel model)
{
if (model == null)
{
return;
}
IngredientName = model.IngredientName;
Cost = model.Cost;
}
public IngredientViewModel GetViewModel => new()
{
Id = Id,
IngredientName = IngredientName,
Cost = Cost
};
public XElement GetXElement => new("Ingredient",
new XAttribute("Id", Id),
new XElement("IngredientName", IngredientName),
new XElement("Cost", Cost.ToString()));
}
}