using SoftwareInstallationContracts.BindingModels; using SoftwareInstallationContracts.ViewModels; using SoftwareInstallationDataModels.Models; using SoftwareInstallationDataModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace SoftwareInstallationFileImplement { public class Shop : IShopModel { public string Name { get; private set; } = string.Empty; public string Address { get; private set; } = string.Empty; public DateTime DateOpening { get; private set; } public Dictionary Packages { get; private set; } = new(); public Dictionary _Packages = null; public Dictionary ShopPackages { get { if (_Packages == null) { var source = DataFileSingleton.GetInstance(); _Packages = Packages.ToDictionary(x => x.Key, y => ((source.Packages.FirstOrDefault(z => z.Id == y.Key) as IPackageModel)!, y.Value)); } return _Packages; } } public int Id { get; private set; } public int PackageMaxCount { get; private set; } public static Shop? Create(ShopBindingModel? model) { if (model == null) { return null; } return new Shop() { Id = model.Id, Name = model.Name, Address = model.Address, PackageMaxCount = model.PackageMaxCount, DateOpening = model.DateOpening, Packages = model.ShopPackages.ToDictionary(x => x.Key, x => x.Value.Item2) }; } public static Shop? Create(XElement element) { if (element == null) { return null; } return new() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), Name = element.Element("ShopName")!.Value, Address = element.Element("ShopAddress")!.Value, DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), PackageMaxCount = Convert.ToInt32(element.Element("PackageMaxCount")!.Value), Packages = element.Element("ShopPackages")!.Elements("Package").ToDictionary( x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } public void Update(ShopBindingModel? model) { if (model == null) { return; } Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; PackageMaxCount = model.PackageMaxCount; Packages = model.ShopPackages.ToDictionary(x => x.Key, x => x.Value.Item2); _Packages = null; } public ShopViewModel GetViewModel => new() { Id = Id, Name = Name, Address = Address, ShopPackages = ShopPackages, DateOpening = DateOpening, PackageMaxCount = PackageMaxCount, }; public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("ShopName", Name), new XElement("ShopAddress", Address), new XElement("DateOpening", DateOpening), new XElement("PackageMaxCount", PackageMaxCount), new XElement("ShopPackages", Packages .Select(x => new XElement("Package", new XElement("Key", x.Key), new XElement("Value", x.Value)) ).ToArray())); } }