From f61ff192afe0c5ae819dae8ec41134248cfad8ef Mon Sep 17 00:00:00 2001 From: georgiy semikolenov Date: Thu, 23 Mar 2023 14:41:26 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PlumbingRepair/PlumbingRepair.sln | 8 +- .../DataFileSingletone.cs | 56 ++++++++++ .../Implements/ComponentStorage.cs | 89 ++++++++++++++++ .../Implements/OrderStorage.cs | 89 ++++++++++++++++ .../Implements/WorkStorage.cs | 89 ++++++++++++++++ .../Models/Component.cs | 59 +++++++++++ .../Models/Order.cs | 100 ++++++++++++++++++ .../PlumbingRepairFileImplemet/Models/Work.cs | 75 +++++++++++++ .../PlumbingRepairFileImplement.csproj | 14 +++ .../PlumbingRepairView/FormWorks.Designer.cs | 1 + .../PlumbingRepairView.csproj | 1 + PlumbingRepair/PlumbingRepairView/Program.cs | 2 +- 12 files changed, 581 insertions(+), 2 deletions(-) create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/DataFileSingletone.cs create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/Implements/ComponentStorage.cs create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/Implements/OrderStorage.cs create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/Implements/WorkStorage.cs create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/Models/Component.cs create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/Models/Order.cs create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/Models/Work.cs create mode 100644 PlumbingRepair/PlumbingRepairFileImplemet/PlumbingRepairFileImplement.csproj diff --git a/PlumbingRepair/PlumbingRepair.sln b/PlumbingRepair/PlumbingRepair.sln index 0b09086..3c80525 100644 --- a/PlumbingRepair/PlumbingRepair.sln +++ b/PlumbingRepair/PlumbingRepair.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairBusinessLogic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairListImplement", "PlumbingRepairListImplement\PlumbingRepairListImplement.csproj", "{111DF058-BDAC-479D-AB5F-1AC10A75FEA1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlumbingRepairView", "PlumbingRepairView\PlumbingRepairView.csproj", "{A67F7409-283B-47B5-A6ED-E9550F9C7185}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlumbingRepairView", "PlumbingRepairView\PlumbingRepairView.csproj", "{A67F7409-283B-47B5-A6ED-E9550F9C7185}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlumbingRepairFileImplement", "PlumbingRepairFileImplemet\PlumbingRepairFileImplement.csproj", "{F114881C-8CBB-4747-BF61-993E764752A3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {A67F7409-283B-47B5-A6ED-E9550F9C7185}.Debug|Any CPU.Build.0 = Debug|Any CPU {A67F7409-283B-47B5-A6ED-E9550F9C7185}.Release|Any CPU.ActiveCfg = Release|Any CPU {A67F7409-283B-47B5-A6ED-E9550F9C7185}.Release|Any CPU.Build.0 = Release|Any CPU + {F114881C-8CBB-4747-BF61-993E764752A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F114881C-8CBB-4747-BF61-993E764752A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F114881C-8CBB-4747-BF61-993E764752A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F114881C-8CBB-4747-BF61-993E764752A3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PlumbingRepair/PlumbingRepairFileImplemet/DataFileSingletone.cs b/PlumbingRepair/PlumbingRepairFileImplemet/DataFileSingletone.cs new file mode 100644 index 0000000..2f5e914 --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/DataFileSingletone.cs @@ -0,0 +1,56 @@ +using PlumbingRepairFileImplement.Models; +using System.Xml.Linq; + +namespace PlumbingRepairFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string WorkFileName = "Work.xml"; + + public List Components { get; private set; } + public List Orders { get; private set; } + public List Works { 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 SaveWorks() => SaveData(Works, WorkFileName, "Works", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Works = LoadData(WorkFileName, "Work", x => Work.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); + } + } + } +} \ No newline at end of file diff --git a/PlumbingRepair/PlumbingRepairFileImplemet/Implements/ComponentStorage.cs b/PlumbingRepair/PlumbingRepairFileImplemet/Implements/ComponentStorage.cs new file mode 100644 index 0000000..53f467f --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using PlumbingRepairContracts.BindingModels; +using PlumbingRepairContracts.SearchModels; +using PlumbingRepairContracts.StoragesContracts; +using PlumbingRepairContracts.ViewModels; +using PlumbingRepairFileImplement.Models; + +namespace PlumbingRepairFileImplement.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; + } + } +} \ No newline at end of file diff --git a/PlumbingRepair/PlumbingRepairFileImplemet/Implements/OrderStorage.cs b/PlumbingRepair/PlumbingRepairFileImplemet/Implements/OrderStorage.cs new file mode 100644 index 0000000..a8ffae6 --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/Implements/OrderStorage.cs @@ -0,0 +1,89 @@ +using PlumbingRepairContracts.BindingModels; +using PlumbingRepairContracts.SearchModels; +using PlumbingRepairContracts.StoragesContracts; +using PlumbingRepairContracts.ViewModels; +using PlumbingRepairFileImplement.Models; + +namespace PlumbingRepairFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Orders.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + return source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + 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 newOrder.GetViewModel; + } + + 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 order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + + return element.GetViewModel; + } + + return null; + } + } +} \ No newline at end of file diff --git a/PlumbingRepair/PlumbingRepairFileImplemet/Implements/WorkStorage.cs b/PlumbingRepair/PlumbingRepairFileImplemet/Implements/WorkStorage.cs new file mode 100644 index 0000000..3d14b5b --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/Implements/WorkStorage.cs @@ -0,0 +1,89 @@ +using PlumbingRepairContracts.BindingModels; +using PlumbingRepairContracts.SearchModels; +using PlumbingRepairContracts.StoragesContracts; +using PlumbingRepairContracts.ViewModels; +using PlumbingRepairFileImplement.Models; + +namespace PlumbingRepairFileImplement.Implements +{ + public class WorkStorage : IWorkStorage + { + private readonly DataFileSingleton source; + + public WorkStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Works.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(WorkSearchModel model) + { + if (string.IsNullOrEmpty(model.WorkName)) + { + return new(); + } + + return source.Works.Where(x => x.WorkName.Contains(model.WorkName)).Select(x => x.GetViewModel).ToList(); + } + + public WorkViewModel? GetElement(WorkSearchModel model) + { + if (string.IsNullOrEmpty(model.WorkName) && !model.Id.HasValue) + { + return null; + } + + return source.Works.FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkName) && x.WorkName == model.WorkName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public WorkViewModel? Insert(WorkBindingModel model) + { + model.Id = source.Works.Count > 0 ? source.Works.Max(x => x.Id) + 1 : 1; + var newWork = Work.Create(model); + + if (newWork == null) + { + return null; + } + + source.Works.Add(newWork); + source.SaveWorks(); + + return newWork.GetViewModel; + } + + public WorkViewModel? Update(WorkBindingModel model) + { + var work = source.Works.FirstOrDefault(x => x.Id == model.Id); + + if (work == null) + { + return null; + } + + work.Update(model); + source.SaveWorks(); + + return work.GetViewModel; + } + + public WorkViewModel? Delete(WorkBindingModel model) + { + var element = source.Works.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Works.Remove(element); + source.SaveWorks(); + + return element.GetViewModel; + } + + return null; + } + } +} \ No newline at end of file diff --git a/PlumbingRepair/PlumbingRepairFileImplemet/Models/Component.cs b/PlumbingRepair/PlumbingRepairFileImplemet/Models/Component.cs new file mode 100644 index 0000000..64ebdcf --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/Models/Component.cs @@ -0,0 +1,59 @@ +using PlumbingRepairContracts.BindingModels; +using PlumbingRepairContracts.ViewModels; +using PlumbingRepairDataModels.Models; +using System.Xml.Linq; + +namespace PlumbingRepairFileImplement.Models +{ + public class Component + { + 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/PlumbingRepair/PlumbingRepairFileImplemet/Models/Order.cs b/PlumbingRepair/PlumbingRepairFileImplemet/Models/Order.cs new file mode 100644 index 0000000..a19a1f9 --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/Models/Order.cs @@ -0,0 +1,100 @@ +using PlumbingRepairContracts.BindingModels; +using PlumbingRepairContracts.ViewModels; +using PlumbingRepairDataModels.Enums; +using PlumbingRepairDataModels.Models; +using System.Xml.Linq; + +namespace PlumbingRepairFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int WorkId { get; private set; } + public string WorkName { get; private set; } = string.Empty; + 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 static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + WorkId = model.WorkId, + WorkName = model.WorkName, + 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; + } + + var order = new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + WorkId = Convert.ToInt32(element.Element("WorkId")!.Value), + WorkName = element.Element("WorkName")!.Value, + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null) + }; + + DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); + order.DateImplement = dateImpl; + + return order; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + WorkId = model.WorkId; + WorkName = model.WorkName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + WorkId = WorkId, + WorkName = WorkName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("WorkName", WorkName), + new XElement("WorkId", WorkId.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())); + } +} \ No newline at end of file diff --git a/PlumbingRepair/PlumbingRepairFileImplemet/Models/Work.cs b/PlumbingRepair/PlumbingRepairFileImplemet/Models/Work.cs new file mode 100644 index 0000000..3b955f0 --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/Models/Work.cs @@ -0,0 +1,75 @@ +using PlumbingRepairContracts.BindingModels; +using PlumbingRepairContracts.ViewModels; +using PlumbingRepairDataModels.Models; +using System.Xml.Linq; + +namespace PlumbingRepairFileImplement.Models +{ + public class Work : IWorkModel + { + public int Id { get; set; } + public string WorkName { get; set; } = String.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + public Dictionary? _workComponents=null; + public Dictionary WorkComponents + { + get + { + if (_workComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _workComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _workComponents; + } + } + public static Work? Create(WorkBindingModel model) + { + if (model == null) + { + return null; + } + return new Work() + { + Id = model.Id, + WorkName = model.WorkName, + Price = model.Price, + Components = model.WorkComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Work? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Work() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + WorkName = element.Element("WorkName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("WorkComponents")!.Elements("WorkComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(WorkBindingModel model) + { + if(model == null) + { + return; + } + WorkName = model.WorkName; + Price = model.Price; + Components = model.WorkComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _workComponents = null; + } + public WorkViewModel GetViewModel => new() + { + Id = Id, + WorkName = WorkName, + Price = Price, + WorkComponents = WorkComponents + }; + public XElement GetXElement => new("Work", new XAttribute("Id", Id), new XElement("WorkName", WorkName), new XElement("Price", Price.ToString()), new XElement("WorkComponents", Components.Select(x => new XElement("WorkComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/PlumbingRepair/PlumbingRepairFileImplemet/PlumbingRepairFileImplement.csproj b/PlumbingRepair/PlumbingRepairFileImplemet/PlumbingRepairFileImplement.csproj new file mode 100644 index 0000000..d1a8bdc --- /dev/null +++ b/PlumbingRepair/PlumbingRepairFileImplemet/PlumbingRepairFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/PlumbingRepair/PlumbingRepairView/FormWorks.Designer.cs b/PlumbingRepair/PlumbingRepairView/FormWorks.Designer.cs index 5231cdb..41177f4 100644 --- a/PlumbingRepair/PlumbingRepairView/FormWorks.Designer.cs +++ b/PlumbingRepair/PlumbingRepairView/FormWorks.Designer.cs @@ -98,6 +98,7 @@ this.Controls.Add(this.dataGridView); this.Name = "FormWorks"; this.Text = "Работы"; + this.Load += new System.EventHandler(this.FormComponents_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/PlumbingRepair/PlumbingRepairView/PlumbingRepairView.csproj b/PlumbingRepair/PlumbingRepairView/PlumbingRepairView.csproj index 5e182e3..ebe3048 100644 --- a/PlumbingRepair/PlumbingRepairView/PlumbingRepairView.csproj +++ b/PlumbingRepair/PlumbingRepairView/PlumbingRepairView.csproj @@ -16,6 +16,7 @@ + diff --git a/PlumbingRepair/PlumbingRepairView/Program.cs b/PlumbingRepair/PlumbingRepairView/Program.cs index 14d3f5a..b4f54db 100644 --- a/PlumbingRepair/PlumbingRepairView/Program.cs +++ b/PlumbingRepair/PlumbingRepairView/Program.cs @@ -1,6 +1,6 @@ using PlumbingRepairContracts.BusinesLogicsContracts; using PlumbingRepairContracts.StoragesContracts; -using PlumbingRepairListImplement.Implements; +using PlumbingRepairFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;