2023-02-12 14:57:32 +04:00
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDataModels.Models;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarFileImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Sushi : ISushiModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
public string SushiName { get; private set; } = string.Empty;
|
|
|
|
|
public double Price { get; private set; }
|
|
|
|
|
public Dictionary<int, int> Ingredients { get; private set; } = new();
|
2023-02-27 22:16:28 +04:00
|
|
|
|
private Dictionary<int, (IIngredientModel, int)>? _sushiIngredients = null;
|
2023-02-12 14:57:32 +04:00
|
|
|
|
public Dictionary<int, (IIngredientModel, int)> SushiIngredients
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-02-27 22:16:28 +04:00
|
|
|
|
if (_sushiIngredients == null)
|
2023-02-12 14:57:32 +04:00
|
|
|
|
{
|
|
|
|
|
var source = DataFileSingleton.GetInstance();
|
2023-02-27 22:16:28 +04:00
|
|
|
|
_sushiIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value));
|
2023-02-12 14:57:32 +04:00
|
|
|
|
}
|
2023-02-27 22:16:28 +04:00
|
|
|
|
return _sushiIngredients;
|
2023-02-12 14:57:32 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static Sushi? Create(SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Sushi()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
SushiName = model.SushiName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Ingredients = model.SushiIngredients.ToDictionary(x => x.Key, x => x.Value.Item2)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public static Sushi? Create(XElement element)
|
|
|
|
|
{
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Sushi()
|
|
|
|
|
{
|
|
|
|
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|
|
|
|
SushiName = element.Element("SushiName")!.Value,
|
|
|
|
|
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
|
|
|
|
Ingredients = element.Element("SushiIngredients")!.Elements("SushiIngredient").ToDictionary(x =>
|
|
|
|
|
Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SushiName = model.SushiName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
Ingredients = model.SushiIngredients.ToDictionary(x => x.Key, x =>
|
|
|
|
|
x.Value.Item2);
|
2023-02-27 22:16:28 +04:00
|
|
|
|
_sushiIngredients = null;
|
2023-02-12 14:57:32 +04:00
|
|
|
|
}
|
|
|
|
|
public SushiViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
SushiName = SushiName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
SushiIngredients = SushiIngredients
|
|
|
|
|
};
|
|
|
|
|
public XElement GetXElement => new("Sushi",
|
|
|
|
|
new XAttribute("Id", Id),
|
|
|
|
|
new XElement("SushiName", SushiName),
|
|
|
|
|
new XElement("Price", Price.ToString()),
|
|
|
|
|
new XElement("SushiIngredients", Ingredients.Select(x => new XElement("SushiIngredient",
|
|
|
|
|
new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
|
|
|
|
|
}
|
|
|
|
|
}
|