ISEbd_21_Kuklew_M.I._Softwa.../SoftwareInstallationFileImplement/Models/Package.cs

95 lines
3.4 KiB
C#
Raw Normal View History

2024-04-07 17:05:30 +04:00
using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Models;
using System.Xml.Linq;
namespace SoftwareInstallationFileImplement.Models
{
public class Package : IPackageModel
{
public int Id { get; private set; }
2024-05-12 14:38:32 +04:00
2024-04-07 17:05:30 +04:00
public string PackageName { get; private set; } = string.Empty;
2024-05-12 14:38:32 +04:00
2024-04-07 17:05:30 +04:00
public double Price { get; private set; }
2024-05-12 14:38:32 +04:00
2024-04-07 17:05:30 +04:00
public Dictionary<int, int> Components { get; private set; } = new();
2024-05-12 14:38:32 +04:00
private Dictionary<int, (IComponentModel, int)>? _iceCreamComponents = null;
2024-04-07 17:05:30 +04:00
public Dictionary<int, (IComponentModel, int)> PackageComponents
{
get
{
2024-05-12 14:38:32 +04:00
if (_iceCreamComponents == null)
2024-04-07 17:05:30 +04:00
{
var source = DataFileSingleton.GetInstance();
2024-05-12 14:38:32 +04:00
_iceCreamComponents = Components.ToDictionary(x => x.Key,
y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
2024-04-07 17:05:30 +04:00
}
2024-05-12 14:38:32 +04:00
return _iceCreamComponents;
2024-04-07 17:05:30 +04:00
}
}
2024-05-12 14:38:32 +04:00
public static Package? Create(PackageBindingModel? model)
2024-04-07 17:05:30 +04:00
{
if (model == null)
{
return null;
}
return new Package()
{
Id = model.Id,
PackageName = model.PackageName,
Price = model.Price,
2024-05-12 14:38:32 +04:00
Components = model.PackageComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
2024-04-07 17:05:30 +04:00
};
}
2024-05-12 14:38:32 +04:00
2024-04-07 17:05:30 +04:00
public static Package? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Package()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PackageName = element.Element("PackageName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
2024-05-12 14:38:32 +04:00
Components = element.Element("PackageComponents")!.Elements("PackageComponent")
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
2024-04-07 17:05:30 +04:00
};
}
2024-05-12 14:38:32 +04:00
public void Update(PackageBindingModel? model)
2024-04-07 17:05:30 +04:00
{
if (model == null)
{
return;
}
PackageName = model.PackageName;
Price = model.Price;
2024-05-12 14:38:32 +04:00
Components = model.PackageComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_iceCreamComponents = null;
2024-04-07 17:05:30 +04:00
}
2024-05-12 14:38:32 +04:00
2024-04-07 17:05:30 +04:00
public PackageViewModel GetViewModel => new()
{
Id = Id,
PackageName = PackageName,
Price = Price,
PackageComponents = PackageComponents
};
2024-05-12 14:38:32 +04:00
public XElement GetXElement => new("Package",
new XAttribute("Id", Id),
new XElement("PackageName", PackageName),
new XElement("Price", Price.ToString()),
new XElement("PackageComponents",
Components.Select(x => new XElement("PackageComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
2024-04-07 17:05:30 +04:00
}
}