using FurnitureAssemblyDataModels.Models; using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.ViewModels; using FurnitureAssemFileImplement; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace FurnitureAssemblyFileImplement.Models { public class Shop : IShopModel { public int Id { get; private set; } public string ShopName { get; private set; } = string.Empty; public string Address { get; private set; } = string.Empty; public DateTime DateOpening { get; private set; } private Dictionary? _furnitures = null; public Dictionary Furnitures { get { if (_furnitures == null) { var source = DataFileSingleton.GetInstance(); _furnitures = CurrentCount.ToDictionary( x => x.Key, y => (source.Furnitures.FirstOrDefault(z => z.Id == y.Key)! as IFurnitureModel, y.Value)); } return _furnitures; } private set { } } public int MaxCount { get; private set; } public Dictionary CurrentCount { get; private set; } = new(); public static Shop? Create(ShopBindingModel model) { if (model == null) { return null; } return new Shop() { Id = model.Id, ShopName = model.ShopName, Address = model.Address, DateOpening = model.DateOpening, MaxCount = model.MaxCount, CurrentCount = model.Furnitures.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), ShopName = element.Element("ShopName")!.Value, Address = element.Element("Address")!.Value, DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), MaxCount = Convert.ToInt32(element.Element("MaxCount")!.Value), CurrentCount = element.Element("CurrentCount")!.Elements("CurrentCountFurniture") .ToDictionary( x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value) ) }; } public void Update(ShopBindingModel? model) { if (model == null) { return; } ShopName = model.ShopName; Address = model.Address; MaxCount = model.MaxCount; DateOpening = model.DateOpening; if (model.Furnitures.Count > 0) { CurrentCount = model.Furnitures.ToDictionary(x => x.Key, x => x.Value.Item2); _furnitures = null; } } public ShopViewModel GetViewModel => new() { Id = Id, ShopName = ShopName, Address = Address, DateOpening = DateOpening, MaxCount = MaxCount, Furnitures = Furnitures }; public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("ShopName", ShopName), new XElement("Address", Address), new XElement("DateOpening", DateOpening), new XElement("MaxCount", MaxCount), new XElement("CurrentCount", CurrentCount.Select( x => new XElement("CurrentCountFurniture", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); } }