diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index a7f5d85..5b7648e 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -9,9 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryContracts", "ConfectionaryContracts\ConfectioneryContracts.csproj", "{4BC782E2-5108-4C55-9F76-ACE5A56B8735}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{2822ACD8-8B94-4BF2-8C55-478677FDD1FE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{2822ACD8-8B94-4BF2-8C55-478677FDD1FE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{EE30482F-8998-4178-8412-421A02F6A6B4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{EE30482F-8998-4178-8412-421A02F6A6B4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Confectionery/ConfectioneryFileImplement/Component.cs b/Confectionery/ConfectioneryFileImplement/Component.cs new file mode 100644 index 0000000..63e6867 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Component.cs @@ -0,0 +1,60 @@ +using System.ComponentModel; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + 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) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + 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 diff --git a/Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj b/Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj new file mode 100644 index 0000000..54e27de --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs new file mode 100644 index 0000000..06fb502 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs @@ -0,0 +1,61 @@ +using ConfectioneryFileImplement.Models; +using System.Xml.Linq; + +namespace PastryShopFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + + private readonly string ComponentFileName = "Component.xml"; + + private readonly string OrderFileName = "Order.xml"; + + private readonly string PastryFileName = "Pastry.xml"; + + public List Components { get; private set; } + + public List Orders { get; private set; } + + public List Pastrys { get; private set; } + + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + + public void SavePastrys() => SaveData(Pastrys, PastryFileName, "Pastrys", x => x.GetXElement); + + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Pastrys = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/Confectionery/ConfectioneryFileImplement/Pastry.cs b/Confectionery/ConfectioneryFileImplement/Pastry.cs new file mode 100644 index 0000000..20602d5 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Pastry.cs @@ -0,0 +1,102 @@ +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; + + +namespace ConfectioneryFileImplement.Models +{ + public class Pastry : IPastryModel + { + public int Id { get; private set; } + public string PastryName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _PastryComponents = + null; + public Dictionary PastryComponents + { + get + { + if (_PastryComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _PastryComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _PastryComponents; + } + } + public static Pastry? Create(PastryBindingModel model) + { + if (model == null) + { + return null; + } + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Pastry? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pastry() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PastryName = element.Element("PastryName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = +element.Element("PastryComponents")!.Elements("PastryComponent") + .ToDictionary(x => +Convert.ToInt32(x.Element("Key")?.Value), x => +Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PastryBindingModel model) + { + if (model == null) + { + return; + } + PastryName = model.PastryName; + Price = model.Price; + Components = model.PastryComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _PastryComponents = null; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + public XElement GetXElement => new("Pastry", + new XAttribute("Id", Id), + new XElement("PastryName", PastryName), + new XElement("Price", Price.ToString()), + new XElement("PastryComponents", Components.Select(x => + new XElement("PastryComponent", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + + } +} diff --git a/Confectionery/ConfectioneryListImplement/Models/Component.cs b/Confectionery/ConfectioneryListImplement/Models/Component.cs index 9683495..0602b42 100644 --- a/Confectionery/ConfectioneryListImplement/Models/Component.cs +++ b/Confectionery/ConfectioneryListImplement/Models/Component.cs @@ -44,4 +44,4 @@ namespace ConfectioneryListImplement.Models }; } -} +} \ No newline at end of file