91 lines
3.2 KiB
C#
91 lines
3.2 KiB
C#
using SoftwareInstallationContracts.BindingModels;
|
|
using SoftwareInstallationContracts.ViewModels;
|
|
using SoftwareInstallationDataModels.Models;
|
|
using System.Xml.Linq;
|
|
|
|
namespace SoftwareInstallationFileImplement.Models
|
|
{
|
|
public class Software : IPackageModel
|
|
{
|
|
public int Id { get; private set; }
|
|
public string SoftwareName { 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)>? _SoftwareComponents =
|
|
null;
|
|
public Dictionary<int, (IComponentModel, int)> SoftwareComponents
|
|
{
|
|
get
|
|
{
|
|
if (_SoftwareComponents == null)
|
|
{
|
|
var source = DataFileSingleton.GetInstance();
|
|
_SoftwareComponents = Components.ToDictionary(x => x.Key, y =>
|
|
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
|
|
y.Value));
|
|
}
|
|
return _SoftwareComponents;
|
|
}
|
|
}
|
|
public static Software? Create(PackageBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Software()
|
|
{
|
|
Id = model.Id,
|
|
SoftwareName = model.SoftwareName,
|
|
Price = model.Price,
|
|
Components = model.SoftwareComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
|
|
};
|
|
}
|
|
public static Software? Create(XElement element)
|
|
{
|
|
if (element == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Software()
|
|
{
|
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|
SoftwareName = element.Element("SoftwareName")!.Value,
|
|
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
|
Components =
|
|
element.Element("SoftwareComponents")!.Elements("PackageComponent")
|
|
.ToDictionary(x =>
|
|
Convert.ToInt32(x.Element("Key")?.Value), x =>
|
|
Convert.ToInt32(x.Element("Value")?.Value))
|
|
};
|
|
}
|
|
public void Update(PackageBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
SoftwareName = model.SoftwareName;
|
|
Price = model.Price;
|
|
Components = model.SoftwareComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
_SoftwareComponents = null;
|
|
}
|
|
public PackageViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
SoftwareName = SoftwareName,
|
|
Price = Price,
|
|
SoftwareComponents = SoftwareComponents
|
|
};
|
|
public XElement GetXElement => new("Software",
|
|
new XAttribute("Id", Id),
|
|
new XElement("SoftwareName", SoftwareName),
|
|
new XElement("Price", Price.ToString()),
|
|
new XElement("SoftwareComponents", Components.Select(x =>
|
|
new XElement("PackageComponent",
|
|
new XElement("Key", x.Key),
|
|
new XElement("Value", x.Value)))
|
|
.ToArray()));
|
|
}
|
|
}
|