From 3de75dc783fe5b3647b07e4fcf6ab5e4456058fa Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Sun, 12 Mar 2023 18:00:46 +0400 Subject: [PATCH] models done --- ...ctSoftwareInstallationFileImplement.csproj | 14 +++ .../DataFileSingleton.cs | 50 ++++++++++ .../Models/Order.cs | 90 ++++++++++++++++++ .../Models/Package.cs | 92 +++++++++++++++++++ .../Models/Software.cs | 64 +++++++++++++ SoftwareInstallation/SoftwareInstallation.sln | 8 +- 6 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationFileImplement/AbstractSoftwareInstallationFileImplement.csproj create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationFileImplement/DataFileSingleton.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Order.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Package.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Software.cs diff --git a/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/AbstractSoftwareInstallationFileImplement.csproj b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/AbstractSoftwareInstallationFileImplement.csproj new file mode 100644 index 0000000..abac8cd --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/AbstractSoftwareInstallationFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/DataFileSingleton.cs b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..46b31f5 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/DataFileSingleton.cs @@ -0,0 +1,50 @@ +using AbstractSoftwareInstallationFileImplement.Models; +using System.Xml.Linq; + +namespace AbstractSoftwareInstallationFileImplement +{ + internal class DataFileSingleton + { + + private static DataFileSingleton? instance; + private readonly string SoftwareFileName = "Software.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string PackageFileName = "Package.xml"; + public List Softwares { get; private set; } + public List Orders { get; private set; } + public List Packages { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveSoftwares() => SaveData(Softwares, SoftwareFileName, "Softwares", x => x.GetXElement); + public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Softwares = LoadData(SoftwareFileName, "Software", x => Software.Create(x)!)!; + Packages = LoadData(PackageFileName, "Package", x => Package.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/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Order.cs b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Order.cs new file mode 100644 index 0000000..0d5b520 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Order.cs @@ -0,0 +1,90 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDataModels; +using AbstractSoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractSoftwareInstallationFileImplement.Models +{ + internal class Order : IOrderModel + { + public int PackageId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int Id { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) return null; + return new Order + { + PackageId = model.PackageId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public static Order? Create(XElement element) + { + if (element == null) return null; + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PackageId = Convert.ToInt32(element.Element("PackageId")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : 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, + PackageId = PackageId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("PackageId", PackageId.ToString()), + 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/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Package.cs b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Package.cs new file mode 100644 index 0000000..6cff938 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Package.cs @@ -0,0 +1,92 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractSoftwareInstallationFileImplement.Models +{ + internal class Package : IPackageModel + { + public string PackageName { get; private set; } = String.Empty; + public int Id { get; private set; } + public double Price { get; private set; } + public Dictionary Softwares { get; private set; } = new(); + private Dictionary _packageSoftware = null; + public Dictionary PackageSoftware + { + get + { + if (_packageSoftware == null) + { + var source = DataFileSingleton.GetInstance(); + _packageSoftware = Softwares.ToDictionary( + x => x.Key, + y => ((source.Softwares.FirstOrDefault(z => z.Id == y.Key) as ISoftwareModel)!, y.Value) + ); + } + return _packageSoftware; + } + } + public static Package? Create(PackageBindingModel model) + { + if (model == null) + { + return null; + } + return new Package() + { + Id = model.Id, + PackageName = model.PackageName, + Price = model.Price, + Softwares = model.PackageSoftware.ToDictionary(x => x.Key, + x => x.Value.Item2) + }; + } + public static Package? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Package() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PackageName = element.Element("PackageName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Softwares = element.Element("PackageSoftwares")!.Elements("PackageSoftware").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + + } + public void Update(PackageBindingModel? model) + { + if (model == null) + { + return; + } + PackageName = model.PackageName; + Price = model.Price; + Softwares = model.PackageSoftware.ToDictionary(x => x.Key, x => x.Value.Item2); + } + public PackageViewModel GetViewModel => new() + { + Id = Id, + PackageName = PackageName, + Price = Price, + PackageSoftware = PackageSoftware + }; + public XElement GetXElement => new("Software", + new XAttribute("Id", Id), + new XElement("PackageName", PackageName), + new XElement("Price", Price.ToString())); + + + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Software.cs b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Software.cs new file mode 100644 index 0000000..2107792 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Models/Software.cs @@ -0,0 +1,64 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractSoftwareInstallationFileImplement.Models +{ + internal class Software : ISoftwareModel + { + public int Id { get; private set; } + public string SoftwareName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Software? Create(SoftwareBindingModel model) + { + if (model == null) + { + return null; + } + return new Software() + { + Id = model.Id, + SoftwareName = model.SoftwareName, + Cost = model.Cost + }; + } + public static Software? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Software() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SoftwareName = element.Element("SoftwareName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(SoftwareBindingModel model) + { + if (model == null) + { + return; + } + SoftwareName = model.SoftwareName; + Cost = model.Cost; + } + public SoftwareViewModel GetViewModel => new() + { + Id = Id, + SoftwareName = SoftwareName, + Cost = Cost + }; + public XElement GetXElement => new("Software", + new XAttribute("Id", Id), + new XElement("SoftwareName", SoftwareName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/SoftwareInstallation/SoftwareInstallation.sln b/SoftwareInstallation/SoftwareInstallation.sln index 7d3feaa..1a4bc7c 100644 --- a/SoftwareInstallation/SoftwareInstallation.sln +++ b/SoftwareInstallation/SoftwareInstallation.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallatio EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallationBusinessLogic", "AbstractSoftwareInstallationBusinessLogic\AbstractSoftwareInstallationBusinessLogic.csproj", "{76E33F5D-6D55-4C28-B26B-9F33B10BA3EF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractSoftwareInstallationListImplement", "AbstractSoftwareInstallationListImplement\AbstractSoftwareInstallationListImplement.csproj", "{31AD2872-9651-476A-9868-C4404FEEB0E4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallationListImplement", "AbstractSoftwareInstallationListImplement\AbstractSoftwareInstallationListImplement.csproj", "{31AD2872-9651-476A-9868-C4404FEEB0E4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractSoftwareInstallationFileImplement", "AbstractSoftwareInstallationFileImplement\AbstractSoftwareInstallationFileImplement.csproj", "{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {31AD2872-9651-476A-9868-C4404FEEB0E4}.Debug|Any CPU.Build.0 = Debug|Any CPU {31AD2872-9651-476A-9868-C4404FEEB0E4}.Release|Any CPU.ActiveCfg = Release|Any CPU {31AD2872-9651-476A-9868-C4404FEEB0E4}.Release|Any CPU.Build.0 = Release|Any CPU + {BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE