From 851a875b0865cf7388d9deb33c5f933c4248ba78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A7=D1=83=D0=B1?= =?UTF-8?q?=D1=8B=D0=BA=D0=B8=D0=BD=D0=B0?= Date: Wed, 13 Mar 2024 10:15:47 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=BE=D0=BC=D0=B0=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=87=D1=82=D0=BE-=D1=82=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfectioneryFileImplement/Component.cs | 60 ----------- .../ConfectioneryFileImplement.csproj | 14 --- .../DataFileSilgleton.cs | 61 ----------- .../ConfectioneryFileImplement/Pastry.cs | 102 ------------------ 4 files changed, 237 deletions(-) delete mode 100644 Confectionery/ConfectioneryFileImplement/Component.cs delete mode 100644 Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj delete mode 100644 Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs delete mode 100644 Confectionery/ConfectioneryFileImplement/Pastry.cs diff --git a/Confectionery/ConfectioneryFileImplement/Component.cs b/Confectionery/ConfectioneryFileImplement/Component.cs deleted file mode 100644 index 63e6867..0000000 --- a/Confectionery/ConfectioneryFileImplement/Component.cs +++ /dev/null @@ -1,60 +0,0 @@ -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 deleted file mode 100644 index 54e27de..0000000 --- a/Confectionery/ConfectioneryFileImplement/ConfectioneryFileImplement.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - - diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs deleted file mode 100644 index 06fb502..0000000 --- a/Confectionery/ConfectioneryFileImplement/DataFileSilgleton.cs +++ /dev/null @@ -1,61 +0,0 @@ -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 deleted file mode 100644 index 20602d5..0000000 --- a/Confectionery/ConfectioneryFileImplement/Pastry.cs +++ /dev/null @@ -1,102 +0,0 @@ -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())); - - } -}