95 lines
3.1 KiB
C#
95 lines
3.1 KiB
C#
|
using CarRepairShopContracts.BindingModels;
|
|||
|
using CarRepairShopContracts.ViewModels;
|
|||
|
using CarRepairShopDataModels.Models;
|
|||
|
using System.Xml.Linq;
|
|||
|
|
|||
|
namespace CarRepairShopFileImplement.Models
|
|||
|
{
|
|||
|
public class Repair : IRepairModel
|
|||
|
{
|
|||
|
public int Id { get; private set; }
|
|||
|
public string RepairName { get; private set; } = string.Empty;
|
|||
|
public double Price { get; private set; }
|
|||
|
public Dictionary<int, int> Components { get; private set; } = new();
|
|||
|
private Dictionary<int, (IComponentModel, int)>? _repairComponents = null;
|
|||
|
public Dictionary<int, (IComponentModel, int)> RepairComponents
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_repairComponents == null)
|
|||
|
{
|
|||
|
var source = DataFileSingleton.GetInstance();
|
|||
|
_repairComponents = Components.ToDictionary(x => x.Key, y =>
|
|||
|
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
|
|||
|
y.Value));
|
|||
|
}
|
|||
|
return _repairComponents;
|
|||
|
}
|
|||
|
}
|
|||
|
public static Repair? Create(RepairBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Repair()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
RepairName = model.RepairName,
|
|||
|
Price = model.Price,
|
|||
|
Components = model.RepairComponents.ToDictionary(x => x.Key, x
|
|||
|
=> x.Value.Item2)
|
|||
|
};
|
|||
|
}
|
|||
|
public static Repair? Create(XElement element)
|
|||
|
{
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Repair()
|
|||
|
{
|
|||
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|||
|
RepairName = element.Element("RepairName")!.Value,
|
|||
|
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
|||
|
Components =
|
|||
|
element.Element("RepairComponents")!.Elements("RepairComponent")
|
|||
|
.ToDictionary(x =>
|
|||
|
Convert.ToInt32(x.Element("Key")?.Value), x =>
|
|||
|
Convert.ToInt32(x.Element("Value")?.Value))
|
|||
|
};
|
|||
|
}
|
|||
|
public void Update(RepairBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
RepairName = model.RepairName;
|
|||
|
Price = model.Price;
|
|||
|
Components = model.RepairComponents.ToDictionary(x => x.Key, x =>
|
|||
|
x.Value.Item2);
|
|||
|
_repairComponents = null;
|
|||
|
}
|
|||
|
public RepairViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
RepairName = RepairName,
|
|||
|
Price = Price,
|
|||
|
RepairComponents = RepairComponents
|
|||
|
};
|
|||
|
public XElement GetXElement => new("Repair",
|
|||
|
new XAttribute("Id", Id),
|
|||
|
new XElement("RepairName", RepairName),
|
|||
|
new XElement("Price", Price.ToString()),
|
|||
|
new XElement("RepairComponents", Components.Select(x =>
|
|||
|
new XElement("RepairComponent",
|
|||
|
|
|||
|
new XElement("Key", x.Key),
|
|||
|
|
|||
|
new XElement("Value", x.Value)))
|
|||
|
|
|||
|
.ToArray()));
|
|||
|
}
|
|||
|
}
|