From 279944d062b473fe44c5c951f0d6c5c9cbf9ed5e Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Fri, 23 Feb 2024 22:40:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractLawFirmFileImplement.csproj | 14 +++ .../DataFileSingleton.cs | 58 +++++++++++ .../Implements/ComponentStorage.cs | 79 +++++++++++++++ .../Implements/DocumentStorage.cs | 83 ++++++++++++++++ .../Implements/OrderStorage.cs | 93 ++++++++++++++++++ .../Models/Component.cs | 64 ++++++++++++ .../Models/Document.cs | 90 +++++++++++++++++ .../Models/Order.cs | 98 +++++++++++++++++++ LawFirm/LawFirm.sln | 10 +- LawFirm/LawFirmView/LawFirmView.csproj | 1 + LawFirm/LawFirmView/Program.cs | 2 +- 11 files changed, 589 insertions(+), 3 deletions(-) create mode 100644 LawFirm/AbstractLawFirmFileImplement/AbstractLawFirmFileImplement.csproj create mode 100644 LawFirm/AbstractLawFirmFileImplement/DataFileSingleton.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Implements/ComponentStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Implements/DocumentStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Implements/OrderStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Models/Component.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Models/Document.cs create mode 100644 LawFirm/AbstractLawFirmFileImplement/Models/Order.cs diff --git a/LawFirm/AbstractLawFirmFileImplement/AbstractLawFirmFileImplement.csproj b/LawFirm/AbstractLawFirmFileImplement/AbstractLawFirmFileImplement.csproj new file mode 100644 index 0000000..f54052f --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/AbstractLawFirmFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/LawFirm/AbstractLawFirmFileImplement/DataFileSingleton.cs b/LawFirm/AbstractLawFirmFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..664959f --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/DataFileSingleton.cs @@ -0,0 +1,58 @@ +using AbstractLawFirmFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractLawFirmFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string DocumentFileName = "Document.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 SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.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/AbstractLawFirmFileImplement/Implements/ComponentStorage.cs b/LawFirm/AbstractLawFirmFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..ab1e6d2 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,79 @@ +using AbstractLawFirmContracts.BindingModels.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + 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/AbstractLawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/AbstractLawFirmFileImplement/Implements/DocumentStorage.cs new file mode 100644 index 0000000..a570e87 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/Implements/DocumentStorage.cs @@ -0,0 +1,83 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.Implements +{ + public class DocumentStorage : IDocumentStorage + { + private readonly DataFileSingleton source; + + public DocumentStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) + { + return null; + } + return source.Documents.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + return source.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.Documents.Select(x => x.GetViewModel).ToList(); + } + + public DocumentViewModel? Insert(DocumentBindingModel model) + { + model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; + var newDoc = Document.Create(model); + if (newDoc == null) + { + return null; + } + source.Documents.Add(newDoc); + source.SaveDocuments(); + return newDoc.GetViewModel; + } + + public DocumentViewModel? Update(DocumentBindingModel model) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveDocuments(); + return document.GetViewModel; + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + source.Documents.Remove(document); + source.SaveDocuments(); + return document.GetViewModel; + } + + } +} diff --git a/LawFirm/AbstractLawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/AbstractLawFirmFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..5dc4b6c --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,93 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return GetViewModel(source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetViewModel(order); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + source.Orders.Remove(order); + source.SaveOrders(); + return GetViewModel(order); + } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) + { + viewModel.DocumentName = document.DocumentName; + } + return viewModel; + } + } +} diff --git a/LawFirm/AbstractLawFirmFileImplement/Models/Component.cs b/LawFirm/AbstractLawFirmFileImplement/Models/Component.cs new file mode 100644 index 0000000..2ff458b --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/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/AbstractLawFirmFileImplement/Models/Document.cs b/LawFirm/AbstractLawFirmFileImplement/Models/Document.cs new file mode 100644 index 0000000..1e2175d --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/Models/Document.cs @@ -0,0 +1,90 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmDataModels.Models; +using AbstractLawFirmContracts.ViewModels; +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? _documentComponents = null; + public Dictionary DocumentComponents + { + get + { + if (_documentComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _documentComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _documentComponents; + } + } + 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("DocumentName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("DocumentComponents")!.Elements("DocumentComponent").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); + _documentComponents = null; + } + public DocumentViewModel GetViewModel => new() + { + Id = Id, + DocumentName = DocumentName, + Price = Price, + DocumentComponents = DocumentComponents + }; + public XElement GetXElement => new("Document", + new XAttribute("Id", Id), + new XElement("DocumentName", DocumentName), + new XElement("Price", Price.ToString()), + new XElement("DocumentComponents", Components.Select(x => + new XElement("DocumentComponent", new XElement("Key", x.Key),new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/LawFirm/AbstractLawFirmFileImplement/Models/Order.cs b/LawFirm/AbstractLawFirmFileImplement/Models/Order.cs new file mode 100644 index 0000000..351d37b --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImplement/Models/Order.cs @@ -0,0 +1,98 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Enums; +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 Order : IOrderModel + { + public int DocumentId { 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 + { + DocumentId = model.DocumentId, + 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), + DocumentId = Convert.ToInt32(element.Element("DocumentId")!.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() + { + DocumentId = DocumentId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("DocumentId", DocumentId.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/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index 09da4ff..f8713d1 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", "AbstractLawFirmFileImplement\AbstractLawFirmFileImplement.csproj", "{5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}" 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 + {5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}.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..7ff567d 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -15,6 +15,7 @@ + diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index ab37d8c..cffdc50 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -1,7 +1,7 @@ using AbstractLawFirmBusinessLogic.BusinessLogic; using AbstractLawFirmContracts.BusinessLogicsContracts; using AbstractLawFirmContracts.StoragesContracts; -using AbstractLawFirmListImplement.Implements; +using AbstractLawFirmFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;