using PlumbingRepairContracts.BindingModels; using PlumbingRepairContracts.ViewModels; using PlumbingRepairDataModels.Models; using System.Xml.Linq; namespace PlumbingRepairFileImplement.Models { public class Store : IStoreModel { public string StoreName { get; private set; } = string.Empty; public string StoreAdress { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary Works { get; private set; } = new(); public Dictionary _storeWorks = null; public int Id { get; private set; } public int WorkMaxCount { get; private set; } public Dictionary StoreWorks { get { if (_storeWorks == null) { var source = DataFileSingleton.GetInstance(); _storeWorks = Works.ToDictionary(x => x.Key, y => ((source.Works.FirstOrDefault(z => z.Id == y.Key) as IWorkModel)!, y.Value)); } return _storeWorks; } } public static Store? Create(StoreBindingModel? model) { if (model == null) { return null; } return new Store() { Id = model.Id, StoreName = model.StoreName, StoreAdress = model.StoreAdress, WorkMaxCount = model.WorkMaxCount, OpeningDate = model.OpeningDate, Works = model.StoreWorks.ToDictionary(x => x.Key, x => x.Value.Item2) }; } public static Store? Create(XElement element) { if (element == null) { return null; } return new() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), StoreName = element.Element("StoreName")!.Value, StoreAdress = element.Element("StoreAdress")!.Value, OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), WorkMaxCount = Convert.ToInt32(element.Element("WorkMaxCount")!.Value), Works = element.Element("StoreWorks")!.Elements("Work").ToDictionary( x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } public void Update(StoreBindingModel? model) { if (model == null) { return; } StoreName = model.StoreName; StoreAdress = model.StoreAdress; OpeningDate = model.OpeningDate; WorkMaxCount = model.WorkMaxCount; Works = model.StoreWorks.ToDictionary(x => x.Key, x => x.Value.Item2); _storeWorks = null; } public StoreViewModel GetViewModel => new() { Id = Id, StoreName = StoreName, StoreAdress = StoreAdress, StoreWorks = StoreWorks, OpeningDate = OpeningDate, WorkMaxCount = WorkMaxCount, }; public XElement GetXElement => new("Store", new XAttribute("Id", Id), new XElement("StoreName", StoreName), new XElement("StoreAdress", StoreAdress), new XElement("OpeningDate", OpeningDate), new XElement("WorkMaxCount", WorkMaxCount), new XElement("StoreWorks", Works .Select(x => new XElement("Work", new XElement("Key", x.Key), new XElement("Value", x.Value)) ).ToArray())); } }