Илья Федотов 4ec14f7a2b Base.02
2024-03-15 14:49:38 +04:00

58 lines
1.7 KiB
C#

using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using DinerDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace DinerFileImplement.Models
{
internal class Food : IFoodModel
{
public string ComponentName { get; set; } = string.Empty;
public double Price { get; set; }
public int ID { get; set; }
public static Food? Create(FoodBindingModel? model)
{
if (model == null) return null;
return new Food()
{
ID = model.ID,
ComponentName = model.ComponentName,
Price = model.Price,
};
}
public static Food? Create(XElement element) {
if (element == null) return null;
return new Food()
{
ID = Convert.ToInt32(element.Attribute("ID")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value)
};
}
public void Update(FoodBindingModel? model)
{
if (model == null) return;
ComponentName = model.ComponentName;
Price = model.Price;
}
public FoodViewModel GetViewModel => new()
{
ID = ID,
ComponentName = ComponentName,
Price = Price,
};
public XElement GetXElement => new("Food", new XAttribute("ID", ID),
new XElement("ComponentName", ComponentName),
new XElement("Price", Price.ToString()));
}
}