From 5a9a99b1b6c20c085a2e051107245ef126ca4a13 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 20 Feb 2024 21:52:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton1.cs | 12 +++++ FishFactoryListImplement/Models/Canned.cs | 49 ++++++++++++++++--- FishFactoryListImplement/Models/Component.cs | 21 ++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 FishFactoryListImplement/DataFileSingleton1.cs diff --git a/FishFactoryListImplement/DataFileSingleton1.cs b/FishFactoryListImplement/DataFileSingleton1.cs new file mode 100644 index 0000000..b906ed1 --- /dev/null +++ b/FishFactoryListImplement/DataFileSingleton1.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryListImplement +{ + internal class DataFileSingleton1 + { + } +} diff --git a/FishFactoryListImplement/Models/Canned.cs b/FishFactoryListImplement/Models/Canned.cs index f9433fe..7d9cf53 100644 --- a/FishFactoryListImplement/Models/Canned.cs +++ b/FishFactoryListImplement/Models/Canned.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace FishFactoryListImplement.Models { @@ -14,11 +15,21 @@ namespace FishFactoryListImplement.Models public int Id { get; private set; } public string CannedName { get; private set; } = string.Empty; public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _cannedComponents = null; public Dictionary CannedComponents { - get; - private set; - } = new Dictionary(); + get + { + if (_cannedComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _cannedComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _cannedComponents; + } + } public static Canned? Create(CannedBindingModel? model) { if (model == null) @@ -30,9 +41,26 @@ namespace FishFactoryListImplement.Models Id = model.Id, CannedName = model.CannedName, Price = model.Price, - CannedComponents = model.CannedComponents + Components = model.CannedComponents.ToDictionary(x => x.Key, x => x.Value.Item2) }; } + + public static Canned? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Canned() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + CannedName = element.Element("CannedName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("CannedComponents")!.Elements("CannedComponent").ToDictionary + (x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value))}; + } + public void Update(CannedBindingModel? model) { if (model == null) @@ -41,7 +69,9 @@ namespace FishFactoryListImplement.Models } CannedName = model.CannedName; Price = model.Price; - CannedComponents = model.CannedComponents; + Components = model.CannedComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _cannedComponents = null; + } public CannedViewModel GetViewModel => new() { @@ -50,6 +80,13 @@ namespace FishFactoryListImplement.Models Price = Price, CannedComponents = CannedComponents }; - + public XElement GetXElement => new("Canned", + new XAttribute("Id", Id), + new XElement("CannedName", CannedName), + new XElement("Price", Price.ToString()), + new XElement("CannedComponents", Components.Select(x => + new XElement("CannedComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } } diff --git a/FishFactoryListImplement/Models/Component.cs b/FishFactoryListImplement/Models/Component.cs index 5d38db3..d9fa737 100644 --- a/FishFactoryListImplement/Models/Component.cs +++ b/FishFactoryListImplement/Models/Component.cs @@ -1,6 +1,7 @@ using FishFactoryContracts.BindingModels; using FishFactoryContracts.ViewModels; using FishFactoryDataModel.Models; +using System.Xml.Linq; namespace FishFactoryListImplement.Models { @@ -22,6 +23,21 @@ namespace FishFactoryListImplement.Models Cost = model.Cost }; } + + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel? model) { if (model == null) @@ -37,5 +53,10 @@ namespace FishFactoryListImplement.Models ComponentName = ComponentName, Cost = Cost }; + + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); } } \ No newline at end of file