From 4cc264503a4566ee313f88f33649a5777b635dd0 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Tue, 12 Mar 2024 21:53:02 +0400 Subject: [PATCH] First --- .../AbstractLawFirmFileImplement.csproj | 14 +++ .../DataFileSingleton.cs | 62 +++++++++++ .../Implements/ComponentStorage.cs | 90 +++++++++++++++ .../Models/Component.cs | 64 +++++++++++ .../Models/Document.cs | 103 ++++++++++++++++++ LawFirm/LawFirm.sln | 10 +- LawFirm/LawFirmView/LawFirmView.csproj | 1 + 7 files changed, 342 insertions(+), 2 deletions(-) create mode 100644 LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj create mode 100644 LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs diff --git a/LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj b/LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj new file mode 100644 index 0000000..f54052f --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs new file mode 100644 index 0000000..20958a4 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using AbstractLawFirmFileImplement.Models; + +namespace AbstractLawFirmFileImpliment +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string ProductFileName = "Product.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Documents { 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 SaveProducts() => SaveData(Products, ProductFileName, + "Products", x => x.GetXElement); + public void SaveOrders() { } + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => + Component.Create(x)!)!; + Documents = LoadData(ProductFileName, "Product", x => + Document.Create(x)!)!; + Orders = new List(); + } + 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/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs new file mode 100644 index 0000000..c07e161 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs @@ -0,0 +1,90 @@ +using AbstractLawFirmContracts.BindingModels.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmFileImplement.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.Implements +{ + public class ComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == + model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => + x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == + model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs new file mode 100644 index 0000000..2ff458b --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs @@ -0,0 +1,64 @@ +using AbstractLawFirmContracts.BindingModels.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractLawFirmFileImplement.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())); + } +} diff --git a/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs new file mode 100644 index 0000000..4f52077 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs @@ -0,0 +1,103 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractLawFirmFileImplement.Models +{ + public class Document : IDocumentModel + { + public int Id { get; private set; } + public string DocumentName { 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) + { + var source = DataFileSingleton.GetInstance(); + _productComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _productComponents; + } + } + public static Document? Create(DocumentBindingModel model) + { + if (model == null) + { + return null; + } + return new Document() + { + Id = model.Id, + DocumentName = model.DocumentName, + Price = model.Price, + Components = model.DocumentComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Document? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Document() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DocumentName = 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(DocumentBindingModel model) + { + if (model == null) + { + return; + } + DocumentName = model.DocumentName; + Price = model.Price; + Components = model.DocumentComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _productComponents = null; + } + public DocumentViewModel GetViewModel => new() + { + Id = Id, + DocumentName = DocumentName, + Price = Price, + DocumentComponents = ProductComponents + }; + public XElement GetXElement => new("Product", + new XAttribute("Id", Id), + new XElement("ProductName", DocumentName), + 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())); + + public Dictionary DocumentComponents => throw new NotImplementedException(); + } +} diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index 09da4ff..e41b11c 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmDataModels", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmContracts", "AbstractLawFirmContracts\AbstractLawFirmContracts\AbstractLawFirmContracts.csproj", "{C0155B2E-5974-4835-996B-6FDF0F5BC06B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmBusinessLogic", "AbstractLawFirmBusinessLogic\AbstractLawFirmBusinessLogic.csproj", "{E48808B1-5381-4774-97B6-CDB107DCF98C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmBusinessLogic", "AbstractLawFirmBusinessLogic\AbstractLawFirmBusinessLogic.csproj", "{E48808B1-5381-4774-97B6-CDB107DCF98C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmListImplement", "AbstractLawFirmListImplement\AbstractLawFirmListImplement.csproj", "{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmListImplement", "AbstractLawFirmListImplement\AbstractLawFirmListImplement.csproj", "{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmFileImplement", "AbstractLawFirmFileImpliment\AbstractLawFirmFileImplement.csproj", "{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Debug|Any CPU.Build.0 = Debug|Any CPU {86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Release|Any CPU.ActiveCfg = Release|Any CPU {86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Release|Any CPU.Build.0 = Release|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index b6cfc13..c62edf7 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -15,6 +15,7 @@ +