80 lines
2.9 KiB
C#
80 lines
2.9 KiB
C#
using ShipyardContracts.BindingModels;
|
|
using ShipyardContracts.ViewModels;
|
|
using ShipyardDataModels.Models;
|
|
using System.Xml.Linq;
|
|
|
|
namespace ShipyardFileImplement.Models
|
|
{
|
|
public class Ship : IShipModel
|
|
{
|
|
public int Id { get; private set; }
|
|
public string ShipName { get; private set; } = string.Empty;
|
|
public double Price { get; private set; }
|
|
public Dictionary<int, int> Details { get; private set; } = new();
|
|
private Dictionary<int, (IDetailModel, int)>? _shipDetails = null;
|
|
public Dictionary<int, (IDetailModel, int)> ShipDetails
|
|
{
|
|
get
|
|
{
|
|
if (_shipDetails == null)
|
|
{
|
|
var source = DataFileSingleton.GetInstance();
|
|
_shipDetails = Details.ToDictionary(x => x.Key, y => ((source.Details.FirstOrDefault(z => z.Id == y.Key) as IDetailModel)!, y.Value));
|
|
}
|
|
return _shipDetails;
|
|
}
|
|
}
|
|
public static Ship? Create(ShipBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Ship()
|
|
{
|
|
Id = model.Id,
|
|
ShipName = model.ShipName,
|
|
Price = model.Price,
|
|
Details = model.ShipDetails.ToDictionary(x => x.Key, x => x.Value.Item2)
|
|
};
|
|
}
|
|
public static Ship? Create(XElement element)
|
|
{
|
|
if (element == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Ship()
|
|
{
|
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|
ShipName = element.Element("ShipName")!.Value,
|
|
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
|
Details = element.Element("ShipDetails")!.Elements("ShipDetail").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
|
|
};
|
|
}
|
|
public void Update(ShipBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
ShipName = model.ShipName;
|
|
Price = model.Price;
|
|
Details = model.ShipDetails.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
_shipDetails = null;
|
|
}
|
|
public ShipViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ShipName = ShipName,
|
|
Price = Price,
|
|
ShipDetails = ShipDetails
|
|
};
|
|
public XElement GetXElement => new("Ship",
|
|
new XAttribute("Id", Id),
|
|
new XElement("ShipName", ShipName),
|
|
new XElement("Price", Price.ToString()),
|
|
new XElement("ShipDetails", ShipDetails.Select(x => new XElement("ShipDetails",
|
|
new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
|
|
}
|
|
} |