diff --git a/Diner/Diner.sln b/Diner/Diner.sln index 04f27db..2895709 100644 --- a/Diner/Diner.sln +++ b/Diner/Diner.sln @@ -5,13 +5,15 @@ VisualStudioVersion = 17.7.34221.43 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerView", "DinerView\DinerView.csproj", "{7FC291E6-FE0A-4D25-B46F-724FB599F26F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerDataModels", "DinerDataModels\DinerDataModels.csproj", "{2AFAC265-13C0-432E-8F33-ABF764FD0877}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerDataModels", "DinerDataModels\DinerDataModels.csproj", "{2AFAC265-13C0-432E-8F33-ABF764FD0877}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerContracts", "DinerContracts\DinerContracts.csproj", "{89BA6B4F-1F08-4327-B88B-E48335742088}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerContracts", "DinerContracts\DinerContracts.csproj", "{89BA6B4F-1F08-4327-B88B-E48335742088}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerListImplement", "DinerListImplement\DinerListImplement.csproj", "{33BCF269-AF60-4E7E-9F3B-2ECEDCEBF8F9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "DinerListImplement\DinerListImplement.csproj", "{33BCF269-AF60-4E7E-9F3B-2ECEDCEBF8F9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DineryBusinessLogic", "DineryBusinessLogic\DineryBusinessLogic.csproj", "{2920D9E3-AE18-4914-BD43-C04D9123FBD7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DineryBusinessLogic", "DineryBusinessLogic\DineryBusinessLogic.csproj", "{2920D9E3-AE18-4914-BD43-C04D9123FBD7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerShopImplement\DinerFileImplement.csproj", "{E46007E3-F3EC-4551-A203-19361E7B1B8D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {2920D9E3-AE18-4914-BD43-C04D9123FBD7}.Debug|Any CPU.Build.0 = Debug|Any CPU {2920D9E3-AE18-4914-BD43-C04D9123FBD7}.Release|Any CPU.ActiveCfg = Release|Any CPU {2920D9E3-AE18-4914-BD43-C04D9123FBD7}.Release|Any CPU.Build.0 = Release|Any CPU + {E46007E3-F3EC-4551-A203-19361E7B1B8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E46007E3-F3EC-4551-A203-19361E7B1B8D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E46007E3-F3EC-4551-A203-19361E7B1B8D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E46007E3-F3EC-4551-A203-19361E7B1B8D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Diner/DinerShopImplement/DataFileSingleton.cs b/Diner/DinerShopImplement/DataFileSingleton.cs new file mode 100644 index 0000000..3da5ad3 --- /dev/null +++ b/Diner/DinerShopImplement/DataFileSingleton.cs @@ -0,0 +1,59 @@ +using DinerFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DinerFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + + private readonly string FoodFileName = "Food.xml"; + + private readonly string OrderFileName = "Order.xml"; + + private readonly string SnackFileName = "Snack.xml"; + + public List Foods { get; set; } + public List Orders { get; private set; } + public List Snacks { get; private set; } + + private DataFileSingleton() { + Foods = LoadData(FoodFileName, "Food", x => Food.Create(x)!)!; + Snacks = LoadData(OrderFileName, "Order", x => Snack.Create(x)!)!; + Orders = new List(); + } + public static DataFileSingleton GetSingleton() { + if (instance == null) instance = new DataFileSingleton(); + return instance; + } + public void SaveFoods() => SaveData(Foods, FoodFileName, "Foods", + x => x.GetXElement); + public void SaveSnacks() => SaveData(Snacks, SnackFileName, "Snacks", + x => x.GetXElement); + public void SaveOrder() => SaveData(Orders, OrderFileName, "Orders", + x => x.GetXElement); + 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); + } + } + 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(); + } + } +} diff --git a/Diner/DinerShopImplement/DinerFileImplement.csproj b/Diner/DinerShopImplement/DinerFileImplement.csproj new file mode 100644 index 0000000..cae5b14 --- /dev/null +++ b/Diner/DinerShopImplement/DinerFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/Diner/DinerShopImplement/Models/Food.cs b/Diner/DinerShopImplement/Models/Food.cs new file mode 100644 index 0000000..89aeb3b --- /dev/null +++ b/Diner/DinerShopImplement/Models/Food.cs @@ -0,0 +1,57 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + internal class Food : IFoodModel + { + public string ComponentName { get; private set; } = string.Empty; + + public double Price { get; set; } + + public int ID { get; private set; } + + public static Food? Create(FoodBindingModel? model) + { + if (model == null) return null; + return new Food() + { + ID = model.ID, + ComponentName = model.ComponentName, + Price = model.Price, + }; + } + public static Food? Create(XElement element) { + if (element == null) return null; + return new Food() + { + ID = Convert.ToInt32(element.Attribute("ID")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value) + }; + } + public void Update(FoodBindingModel? model) + { + if (model == null) return; + ComponentName = model.ComponentName; + Price = model.Price; + } + public FoodViewModel GetViewModel => new() + { + ID = ID, + ComponentName = ComponentName, + Price = Price, + }; + + public XElement GetXElement => new("Food", new XAttribute("ID", ID), + new XElement("CompoenntName", ComponentName), + new XElement("Price", Price.ToString())); + } +} diff --git a/Diner/DinerShopImplement/Models/Order.cs b/Diner/DinerShopImplement/Models/Order.cs new file mode 100644 index 0000000..78b8b87 --- /dev/null +++ b/Diner/DinerShopImplement/Models/Order.cs @@ -0,0 +1,82 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Enums; +using DinerDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DinerFileImplement.Models +{ + internal class Order : IOrderModel + { + public int ProductID { get; private set; } + + public int Count { get; set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; set; } + + public DateTime DateCreate { get; private set; } + + public DateTime? DateImplement { get; set; } + + public int ID { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) return null; + return new Order() + { + ID = model.ID, + ProductID = model.ProductID, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public static Order? Create(XElement element) { + if (element == null) return null; + return new Order() + { + ID = Convert.ToInt32(element.Attribute("ID")!.Value), + ProductID = Convert.ToInt32(element.Element("ProductName")!.Value), + Count = Convert.ToInt32(element.Element("Price")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) return; + Status = model.Status; + DateImplement = model?.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + ID = ID, + ProductID = ProductID, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new("Order", new XAttribute("ID", ID), + new XElement("ProductID", ProductID), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + + } +} diff --git a/Diner/DinerShopImplement/Models/Snack.cs b/Diner/DinerShopImplement/Models/Snack.cs new file mode 100644 index 0000000..0b3e0f8 --- /dev/null +++ b/Diner/DinerShopImplement/Models/Snack.cs @@ -0,0 +1,79 @@ +using DinerContracts.BindingModels; +using DinerContracts.ViewModels; +using DinerDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using System.Xml.XPath; + +namespace DinerFileImplement.Models +{ + internal class Snack : ISnackModel + { + public string ProductName { get; private set; } = string.Empty; + + public double Price { get; private set; } + + public Dictionary Components { get; private set; } = new(); + private Dictionary? _productComponents = null; + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) { + } + return _productComponents; + } + } + public int ID { get; private set; } + + public static Snack? Create(SnackBindingModel? model) + { + if (model == null) return null; + return new Snack() + { + ID = model.ID, + ProductName = model.ProductName, + Price = model.Price, + Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Snack? Create(XElement element) + { + if (element == null) return null; + return new Snack() + { + ID = Convert.ToInt32(element.Attribute("ID")!.Value), + ProductName = element.Element("ProductName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("ProductComponents")!.Elements("ProductComponent") + .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(SnackBindingModel? model) + { + if (model == null) return; + ProductName = model.ProductName; + Price = model.Price; + Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _productComponents = null; + } + public SnackViewModel GetViewModel => new() + { + ID = ID, + ProductName = ProductName, + Price = Price, + ProductComponents = ProductComponents + }; + public XElement GetXElement => new("Sncak", new XAttribute("ID", ID), + new XElement("ProductName", ProductName), + new XElement("Price", Price.ToString()), + new XElement("ProductComponents", Components.Select(x => new XElement("ProductComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +}