From 0d9216f0282bbc6620d2c856530a215f1e165c54 Mon Sep 17 00:00:00 2001 From: Anatoliy Date: Sat, 4 May 2024 19:42:41 +0400 Subject: [PATCH] PIbd22_Kuznetsov_A.V._lab2 --- SewingDresses/SewingDresses.sln | 10 ++- .../DataFileSingleton.cs | 49 +++++++++++ .../Implements/ComponentStorage.cs | 73 +++++++++++++++++ .../Implements/DressStorage.cs | 71 ++++++++++++++++ .../Implements/OrderStorage.cs | 77 +++++++++++++++++ .../Models/Component.cs | 58 +++++++++++++ .../Models/Dress.cs | 78 ++++++++++++++++++ .../Models/Order.cs | 82 +++++++++++++++++++ .../SewingDressesFileImplement.csproj | 14 ++++ .../SewingDressesView/DressesForm.cs | 2 +- .../SewingDressesView/MainForm.Designer.cs | 1 + SewingDresses/SewingDressesView/MainForm.cs | 5 ++ SewingDresses/SewingDressesView/Program.cs | 2 +- .../SewingDressesView.csproj | 1 + 14 files changed, 519 insertions(+), 4 deletions(-) create mode 100644 SewingDresses/SewingDressesFileImplement/DataFileSingleton.cs create mode 100644 SewingDresses/SewingDressesFileImplement/Implements/ComponentStorage.cs create mode 100644 SewingDresses/SewingDressesFileImplement/Implements/DressStorage.cs create mode 100644 SewingDresses/SewingDressesFileImplement/Implements/OrderStorage.cs create mode 100644 SewingDresses/SewingDressesFileImplement/Models/Component.cs create mode 100644 SewingDresses/SewingDressesFileImplement/Models/Dress.cs create mode 100644 SewingDresses/SewingDressesFileImplement/Models/Order.cs create mode 100644 SewingDresses/SewingDressesFileImplement/SewingDressesFileImplement.csproj diff --git a/SewingDresses/SewingDresses.sln b/SewingDresses/SewingDresses.sln index 84768ba..9fc1da9 100644 --- a/SewingDresses/SewingDresses.sln +++ b/SewingDresses/SewingDresses.sln @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesDataModels", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesView", "SewingDressesView\SewingDressesView.csproj", "{E39E6495-2356-4AF8-8C24-C2C5E113341B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SewingDressesBusinessLogic", "SewingDressesBusinessLogic\SewingDressesBusinessLogic.csproj", "{3A0DDBD6-0799-4047-A316-C1EEC0F24DFE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesBusinessLogic", "SewingDressesBusinessLogic\SewingDressesBusinessLogic.csproj", "{3A0DDBD6-0799-4047-A316-C1EEC0F24DFE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SewingDressesListImplement", "SewingDressesListImplement\SewingDressesListImplement.csproj", "{0F2DC551-6221-4471-BBAA-C5C65E45E66C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SewingDressesListImplement", "SewingDressesListImplement\SewingDressesListImplement.csproj", "{0F2DC551-6221-4471-BBAA-C5C65E45E66C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SewingDressesFileImplement", "SewingDressesFileImplement\SewingDressesFileImplement.csproj", "{340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {0F2DC551-6221-4471-BBAA-C5C65E45E66C}.Debug|Any CPU.Build.0 = Debug|Any CPU {0F2DC551-6221-4471-BBAA-C5C65E45E66C}.Release|Any CPU.ActiveCfg = Release|Any CPU {0F2DC551-6221-4471-BBAA-C5C65E45E66C}.Release|Any CPU.Build.0 = Release|Any CPU + {340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {340D02B6-5B03-47A2-8498-BB7DBBCB7F5B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SewingDresses/SewingDressesFileImplement/DataFileSingleton.cs b/SewingDresses/SewingDressesFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..27d6d67 --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/DataFileSingleton.cs @@ -0,0 +1,49 @@ +using SewingDressesFileImplement.Models; +using System.Xml.Linq; + +namespace SewingDressesFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string DressFileName = "Dress.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Dresses { get; private set;} + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + 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); + } + } + 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(); + + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveDresses() => SaveData(Dresses, DressFileName, "Dresses", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Dresses = LoadData(DressFileName, "Dress", x => Dress.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + } +} diff --git a/SewingDresses/SewingDressesFileImplement/Implements/ComponentStorage.cs b/SewingDresses/SewingDressesFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..a780984 --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,73 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.SearchModels; +using SewingDressesContracts.StoragesContracts; +using SewingDressesContracts.ViewModels; +using SewingDressesFileImplement.Models; + +namespace SewingDressesFileImplement.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 component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + source.Components.Remove(component); + source.SaveComponents(); + return component.GetViewModel; + } + } +} diff --git a/SewingDresses/SewingDressesFileImplement/Implements/DressStorage.cs b/SewingDresses/SewingDressesFileImplement/Implements/DressStorage.cs new file mode 100644 index 0000000..fea356d --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/Implements/DressStorage.cs @@ -0,0 +1,71 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.SearchModels; +using SewingDressesContracts.StoragesContracts; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Models; +using SewingDressesFileImplement.Models; + + +namespace SewingDressesFileImplement.Implements +{ + public class DressStorage : IDressStorage + { + private readonly DataFileSingleton source; + public DressStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Dresses.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(DressSearchModel model) + { + return source.Dresses.Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public DressViewModel? GetElement(DressSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Dresses.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DressName) && x.DressName == model.DressName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public DressViewModel? Insert(DressBindingModel model) + { + model.Id = source.Dresses.Count > 0 ? source.Dresses.Max(x => x.Id) + 1 : 1; + var newDress = Dress.Create(model); + if (newDress == null) + { + return null; + } + source.Dresses.Add(newDress); + source.SaveDresses(); + return newDress.GetViewModel; + } + public DressViewModel? Update(DressBindingModel model) + { + var component = source.Dresses.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveDresses(); + return component.GetViewModel; + } + public DressViewModel? Delete(DressBindingModel model) + { + var component = source.Dresses.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + source.Dresses.Remove(component); + source.SaveDresses(); + return component.GetViewModel; + } + } +} diff --git a/SewingDresses/SewingDressesFileImplement/Implements/OrderStorage.cs b/SewingDresses/SewingDressesFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..baccd42 --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,77 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.SearchModels; +using SewingDressesContracts.StoragesContracts; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Models; +using SewingDressesFileImplement.Models; + + +namespace SewingDressesFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => AcessDressesStorage(x.GetViewModel)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + return source.Orders.Where(x => x.Id == model.Id) + .Select(x => AcessDressesStorage( 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 component = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveOrders(); + return component.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var component = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + source.Orders.Remove(component); + source.SaveOrders(); + return component.GetViewModel; + } + public OrderViewModel AcessDressesStorage(OrderViewModel model) + { + var component = source.Dresses.FirstOrDefault(x => x.Id == model.DressId); + model.DressName = component.DressName; + return model; + } + } +} diff --git a/SewingDresses/SewingDressesFileImplement/Models/Component.cs b/SewingDresses/SewingDressesFileImplement/Models/Component.cs new file mode 100644 index 0000000..8338296 --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/Models/Component.cs @@ -0,0 +1,58 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Models; +using System.Xml.Linq; + +namespace SewingDressesFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; private 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, new System.Globalization.CultureInfo("en-US")) + }; + } + + 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/SewingDresses/SewingDressesFileImplement/Models/Dress.cs b/SewingDresses/SewingDressesFileImplement/Models/Dress.cs new file mode 100644 index 0000000..3315aed --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/Models/Dress.cs @@ -0,0 +1,78 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Models; +using System.Xml.Linq; + + +namespace SewingDressesFileImplement.Models +{ + public class Dress : IDressModel + { + public int Id { get; private set; } + public string DressName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _dressComponents = null; + public Dictionary DressComponents + { + get + { + if (_dressComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _dressComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + } + return _dressComponents; + } + } + public static Dress? Create(DressBindingModel model) + { + if (model == null) + { + return null; + } + return new Dress() + { + Id = model.Id, + DressName = model.DressName, + Price = model.Price, + Components = model.DressComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Dress? Create(XElement element) + { + if (element == null) + { return null; } + return new Dress() + { + Id = Convert.ToInt32(element.Attribute("Id")?.Value), + DressName = element.Element("DressName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value, new System.Globalization.CultureInfo("en-US")), + Components = element.Element("DressComponents")!.Elements("DressComponent").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(DressBindingModel model) + { + if (model == null) + { + return; + } + DressName = model.DressName; + Price = model.Price; + Components = model.DressComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _dressComponents = null; + } + public DressViewModel GetViewModel => new() + { + Id = Id, + DressName = DressName, + Price = Price, + DressComponents = DressComponents + }; + public XElement GetXElement => new("Dress", + new XAttribute("Id", Id), + new XElement("DressName", DressName), + new XElement("Price", Price), + new XElement("DressComponents", Components.Select(x => new XElement("DressComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/SewingDresses/SewingDressesFileImplement/Models/Order.cs b/SewingDresses/SewingDressesFileImplement/Models/Order.cs new file mode 100644 index 0000000..08d0a00 --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/Models/Order.cs @@ -0,0 +1,82 @@ +using SewingDressesContracts.BindingModels; +using SewingDressesContracts.ViewModels; +using SewingDressesDataModels.Enums; +using SewingDressesDataModels.Models; +using System.Data; +using System.Xml.Linq; + +namespace SewingDressesFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int DressId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } + 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, + DressId = model.DressId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + var dateImplement = element.Element("DateImplement")?.Value; + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DressId = Convert.ToInt32(element.Element("DressId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value, new System.Globalization.CultureInfo("en-US")), + Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")?.Value), + DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement) + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + DressId = DressId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("DressId", DressId), + new XElement("Count", Count), + new XElement("Sum", Sum), + new XElement("Status", Status - OrderStatus.Принят), + new XElement("DateCreate", DateCreate), + new XElement("DateImplement", DateImplement)); + } +} diff --git a/SewingDresses/SewingDressesFileImplement/SewingDressesFileImplement.csproj b/SewingDresses/SewingDressesFileImplement/SewingDressesFileImplement.csproj new file mode 100644 index 0000000..df23651 --- /dev/null +++ b/SewingDresses/SewingDressesFileImplement/SewingDressesFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/SewingDresses/SewingDressesView/DressesForm.cs b/SewingDresses/SewingDressesView/DressesForm.cs index 117fc30..8f8f1be 100644 --- a/SewingDresses/SewingDressesView/DressesForm.cs +++ b/SewingDresses/SewingDressesView/DressesForm.cs @@ -99,8 +99,8 @@ namespace SewingDressesView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["DressComponents"].Visible = false; dataGridView.Columns["DressName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["DressComponents"].Visible = false; } _logger.LogInformation("Load components"); } diff --git a/SewingDresses/SewingDressesView/MainForm.Designer.cs b/SewingDresses/SewingDressesView/MainForm.Designer.cs index cee4ca6..a107745 100644 --- a/SewingDresses/SewingDressesView/MainForm.Designer.cs +++ b/SewingDresses/SewingDressesView/MainForm.Designer.cs @@ -148,6 +148,7 @@ MainMenuStrip = menuStrip1; Name = "MainForm"; Text = "Магазин пошива одежды"; + Load += MainForm_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); menuStrip1.ResumeLayout(false); menuStrip1.PerformLayout(); diff --git a/SewingDresses/SewingDressesView/MainForm.cs b/SewingDresses/SewingDressesView/MainForm.cs index 5ff9c56..a5d837f 100644 --- a/SewingDresses/SewingDressesView/MainForm.cs +++ b/SewingDresses/SewingDressesView/MainForm.cs @@ -141,5 +141,10 @@ namespace SewingDressesView { LoadData(); } + + private void MainForm_Load(object sender, EventArgs e) + { + LoadData(); + } } } diff --git a/SewingDresses/SewingDressesView/Program.cs b/SewingDresses/SewingDressesView/Program.cs index 34e4274..c8ebbb3 100644 --- a/SewingDresses/SewingDressesView/Program.cs +++ b/SewingDresses/SewingDressesView/Program.cs @@ -1,7 +1,7 @@ using SewingDressesBusinessLogic.BusinessLogic; using SewingDressesContracts.BusinessLogicsContracts; using SewingDressesContracts.StoragesContracts; -using SewingDressesListImplement.Implements; +using SewingDressesFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/SewingDresses/SewingDressesView/SewingDressesView.csproj b/SewingDresses/SewingDressesView/SewingDressesView.csproj index 3b367a9..48c6708 100644 --- a/SewingDresses/SewingDressesView/SewingDressesView.csproj +++ b/SewingDresses/SewingDressesView/SewingDressesView.csproj @@ -11,6 +11,7 @@ +