2024-02-27 14:27:20 +04:00
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDataModels.Models;
|
2024-06-18 21:09:07 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-02-27 14:27:20 +04:00
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarFileImplement.Models
|
|
|
|
|
{
|
2024-06-18 21:09:07 +04:00
|
|
|
|
[DataContract]
|
|
|
|
|
public class Sushi : ISushiModel
|
2024-02-27 14:27:20 +04:00
|
|
|
|
{
|
2024-06-18 21:09:07 +04:00
|
|
|
|
[DataMember]
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
[DataMember]
|
|
|
|
|
public string SushiName { get; private set; } = string.Empty;
|
|
|
|
|
[DataMember]
|
|
|
|
|
public double Price { get; private set; }
|
2024-02-27 14:27:20 +04:00
|
|
|
|
public Dictionary<int, int> Components { get; private set; } = new();
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _sushiComponents = null;
|
|
|
|
|
public Dictionary<int, (IComponentModel, int)> SushiComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_sushiComponents == null)
|
|
|
|
|
{
|
|
|
|
|
var source = DataFileSingleton.GetInstance();
|
|
|
|
|
_sushiComponents = Components.ToDictionary(x => x.Key, y =>
|
|
|
|
|
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
|
|
|
|
|
y.Value));
|
|
|
|
|
}
|
|
|
|
|
return _sushiComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static Sushi? Create(SushiBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Sushi()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
SushiName = model.SushiName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.SushiComponents.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),
|
|
|
|
|
Components = element.Element("SushiComponents")!.Elements("SushiComponent").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;
|
|
|
|
|
Components = model.SushiComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
|
|
|
_sushiComponents = null;
|
|
|
|
|
}
|
|
|
|
|
public SushiViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
SushiName = SushiName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
SushiComponents = SushiComponents
|
|
|
|
|
};
|
|
|
|
|
public XElement GetXElement => new("Sushi",
|
|
|
|
|
new XAttribute("Id", Id),
|
|
|
|
|
new XElement("SushiName", SushiName),
|
|
|
|
|
new XElement("Price", Price.ToString()),
|
|
|
|
|
new XElement("SushiComponents", Components.Select(x =>
|
|
|
|
|
new XElement("SushiComponent",
|
|
|
|
|
new XElement("Key", x.Key),
|
|
|
|
|
new XElement("Value", x.Value))).ToArray()));
|
|
|
|
|
}
|
|
|
|
|
}
|