using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ComputersShopContracts.BindingModels; using ComputersShopContracts.ViewModels; using ComputersShopDataModels.Models; using System.Xml.Linq; namespace ComputersShopFileImplements.Models { public class Computer: IComputerModel { public int Id { get; private set; } public string ComputerName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Components { get; private set; } = new(); private Dictionary? _computerComponents = null; public Dictionary ComputerComponents { get { if (_computerComponents == null) { var source = DataFileSingleton.GetInstance(); _computerComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); } return _computerComponents; } } public static Computer? Create(ComputerBindingModel model) { if (model == null) { return null; } return new Computer() { Id = model.Id, ComputerName = model.ComputerName, Price = model.Price, Components = model.ComputerComponents.ToDictionary(x => x.Key, x => x.Value.Item2) }; } public static Computer? Create(XElement element) { if (element == null) { return null; } return new Computer() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ComputerName = element.Element("ComputerName")!.Value, Price = Convert.ToDouble(element.Element("Price")!.Value), Components = element.Element("ComputerComponents")!.Elements("ComputerComponent") .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } public void Update(ComputerBindingModel model) { if (model == null) { return; } ComputerName = model.ComputerName; Price = model.Price; Components = model.ComputerComponents.ToDictionary(x => x.Key, x => x.Value.Item2); _computerComponents = null; } public ComputerViewModel GetViewModel => new() { Id = Id, ComputerName = ComputerName, Price = Price, ComputerComponents = ComputerComponents }; public XElement GetXElement => new("Computer", new XAttribute("Id", Id), new XElement("ComputerName", ComputerName), new XElement("Price", Price.ToString()), new XElement("ComputerComponents", Components.Select(x => new XElement("ComputerComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); } }