diff --git a/JewelryStore/FormJewels.cs b/JewelryStore/FormJewels.cs index bea74a4..5f5f06f 100644 --- a/JewelryStore/FormJewels.cs +++ b/JewelryStore/FormJewels.cs @@ -36,6 +36,8 @@ namespace JewelryStore DataGridView.DataSource = list; DataGridView.Columns["Id"].Visible = false; DataGridView.Columns["JewelName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + DataGridView.Columns["JewelComponents"].Visible = false; + } _logger.LogInformation("Загрузка компонентов"); diff --git a/JewelryStore/JewelryStore.csproj b/JewelryStore/JewelryStore.csproj index 90bafab..8a1c665 100644 --- a/JewelryStore/JewelryStore.csproj +++ b/JewelryStore/JewelryStore.csproj @@ -8,14 +8,20 @@ enable + + + + + - + + diff --git a/JewelryStore/JewelryStore.sln b/JewelryStore/JewelryStore.sln index 08fec75..478a62b 100644 --- a/JewelryStore/JewelryStore.sln +++ b/JewelryStore/JewelryStore.sln @@ -5,13 +5,15 @@ VisualStudioVersion = 17.4.33122.133 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStore", "JewelryStore.csproj", "{F1AEB85A-E5E1-4B07-8D6B-E46870480FFA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreContracts", "..\JewelryStoreContracts\JewelryStoreContracts.csproj", "{763B1FD7-7D30-4689-869C-1FEAD240B29B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreContracts", "..\JewelryStoreContracts\JewelryStoreContracts.csproj", "{763B1FD7-7D30-4689-869C-1FEAD240B29B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreDataModels", "..\JewelryStoreDataModels\JewelryStoreDataModels.csproj", "{84BF2156-F821-46F4-8EED-F371084C0129}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreDataModels", "..\JewelryStoreDataModels\JewelryStoreDataModels.csproj", "{84BF2156-F821-46F4-8EED-F371084C0129}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreBusinessLogic", "..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj", "{DA57067F-F3ED-4B1C-B56F-52856FC2E93D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreBusinessLogic", "..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj", "{DA57067F-F3ED-4B1C-B56F-52856FC2E93D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreListImplement", "..\JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{B8C8AA30-FC16-4331-B456-CD16C3C97B25}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreListImplement", "..\JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{B8C8AA30-FC16-4331-B456-CD16C3C97B25}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreFileImplement", "..\JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{2906CD1C-4D78-4329-A2E9-8B67E624DD92}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {B8C8AA30-FC16-4331-B456-CD16C3C97B25}.Debug|Any CPU.Build.0 = Debug|Any CPU {B8C8AA30-FC16-4331-B456-CD16C3C97B25}.Release|Any CPU.ActiveCfg = Release|Any CPU {B8C8AA30-FC16-4331-B456-CD16C3C97B25}.Release|Any CPU.Build.0 = Release|Any CPU + {2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/JewelryStore/Program.cs b/JewelryStore/Program.cs index 4a4a47e..df72a59 100644 --- a/JewelryStore/Program.cs +++ b/JewelryStore/Program.cs @@ -1,7 +1,7 @@ using JewelryStoreContracts.BusinessLogicsContracts; using JewelryStoreContracts.StoragesContracts; using JewelryStoreBusinessLogic.BusinessLogics; -using JewelryStoreListImplement.Implements; +using JewelryStoreFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using System.Drawing; @@ -23,8 +23,8 @@ namespace JewelryStore { // To customize application configuration such as set high DPI //settings or default font, - // see https://aka.ms/applicationconfiguration. - ApplicationConfiguration.Initialize(); + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); diff --git a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj index 7e1df35..d6db721 100644 --- a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj +++ b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,6 +6,10 @@ enable + + + + diff --git a/JewelryStoreFileImplement/DataFileSingleton.cs b/JewelryStoreFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..73eeacf --- /dev/null +++ b/JewelryStoreFileImplement/DataFileSingleton.cs @@ -0,0 +1,54 @@ +using JewelryStoreFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace JewelryStoreFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string JewelFileName = "Jewel.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Jewels { 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 SaveJewels() => SaveData(Jewels, JewelFileName, "Jewels", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x =>Component.Create(x)!)!; + Jewels = LoadData(JewelFileName, "Jewel", x => Jewel.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/JewelryStoreFileImplement/Implements/ComponentStorage .cs b/JewelryStoreFileImplement/Implements/ComponentStorage .cs new file mode 100644 index 0000000..11ec35c --- /dev/null +++ b/JewelryStoreFileImplement/Implements/ComponentStorage .cs @@ -0,0 +1,88 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreFileImplement.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/JewelryStoreFileImplement/Implements/JewelStorage.cs b/JewelryStoreFileImplement/Implements/JewelStorage.cs new file mode 100644 index 0000000..5618d75 --- /dev/null +++ b/JewelryStoreFileImplement/Implements/JewelStorage.cs @@ -0,0 +1,88 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreFileImplement.Implements +{ + public class JewelStorage : IJewelStorage // TODO Lab_2 + { + private readonly DataFileSingleton _source; + public JewelStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return _source.Jewels.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(JewelSearchModel + model) + { + if (string.IsNullOrEmpty(model.JewelName)) + { + return new(); + } + return _source.Jewels + .Where(x => x.JewelName.Contains(model.JewelName)) + .Select(x => x.GetViewModel) + .ToList(); + + } + public JewelViewModel? GetElement(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue) + { + return null; + } + return _source.Jewels + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.JewelName) && x.JewelName == model.JewelName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public JewelViewModel? Insert(JewelBindingModel model) + { + model.Id = _source.Jewels.Count > 0 ? _source.Jewels.Max(x => x.Id) + 1 : 1; + var newJewel = Jewel.Create(model); + if (newJewel == null) + { + return null; + } + _source.Jewels.Add(newJewel); + _source.SaveJewels(); + return newJewel.GetViewModel; + } + + public JewelViewModel? Update(JewelBindingModel model) + { + var component = _source.Jewels.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + _source.SaveJewels(); + return component.GetViewModel; + + + } + public JewelViewModel? Delete(JewelBindingModel model) + { + var element = _source.Jewels.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + _source.Jewels.Remove(element); + _source.SaveJewels(); + return element.GetViewModel; + } + return null; + + } + } +} diff --git a/JewelryStoreFileImplement/Implements/OrderStorage.cs b/JewelryStoreFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..40ac71d --- /dev/null +++ b/JewelryStoreFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreFileImplement.Implements +{ + public class OrderStorage : IOrderStorage // TODO Lab_2 + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + 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; + } + + 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 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 List GetFullList() + { + return source.Orders.Select(x => x.GetViewModel).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 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; + } + } +} diff --git a/JewelryStoreFileImplement/JewelryStoreFileImplement.csproj b/JewelryStoreFileImplement/JewelryStoreFileImplement.csproj new file mode 100644 index 0000000..783cc4d --- /dev/null +++ b/JewelryStoreFileImplement/JewelryStoreFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/JewelryStoreFileImplement/Models/Component.cs b/JewelryStoreFileImplement/Models/Component.cs new file mode 100644 index 0000000..634fb12 --- /dev/null +++ b/JewelryStoreFileImplement/Models/Component.cs @@ -0,0 +1,71 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace JewelryStoreFileImplement.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/JewelryStoreFileImplement/Models/Jewel.cs b/JewelryStoreFileImplement/Models/Jewel.cs new file mode 100644 index 0000000..0f171bd --- /dev/null +++ b/JewelryStoreFileImplement/Models/Jewel.cs @@ -0,0 +1,95 @@ +using JewelryStoreDataModels.Models; +using JewelryStoreContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JewelryStoreContracts.BindingModels; +using System.Xml.Linq; + +namespace JewelryStoreFileImplement.Models +{ + public class Jewel: IJewelModel + { + public int Id { get; private set; } + public string JewelName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _productComponents = null; + + public Dictionary JewelComponents + { + 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 Jewel? Create(JewelBindingModel model) + { + if (model == null) + { + return null; + } + return new Jewel() + { + Id = model.Id, + JewelName = model.JewelName, + Price = model.Price, + Components = model.JewelComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + + public static Jewel? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Jewel() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + JewelName = element.Element("JewelName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = + element.Element("JewelComponents")!.Elements("JewelComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(JewelBindingModel model) + { + if (model == null) + { + return; + } + JewelName = model.JewelName; + Price = model.Price; + Components = model.JewelComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _productComponents = null; + } + public JewelViewModel GetViewModel => new() + { + Id = Id, + JewelName = JewelName, + Price = Price, + JewelComponents = JewelComponents + }; + public XElement GetXElement => new("Jewel", + new XAttribute("Id", Id), + new XElement("JewelName", JewelName), + new XElement("Price", Price.ToString()), + new XElement("JewelComponents", Components.Select(x =>new XElement("JewelComponent",new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + + } +} diff --git a/JewelryStoreFileImplement/Models/Order.cs b/JewelryStoreFileImplement/Models/Order.cs new file mode 100644 index 0000000..fc8fa48 --- /dev/null +++ b/JewelryStoreFileImplement/Models/Order.cs @@ -0,0 +1,112 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDataModels.Enums; +using JewelryStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace JewelryStoreFileImplement.Models +{ + public class Order : IOrderModel + { + public int JewelId { get; private set; } + + public string JewelName { 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 int Id { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + JewelId = model.JewelId, + JewelName = model.JewelName, + 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), + JewelId = Convert.ToInt32(element.Element("JewelId")!.Value), + JewelName = element.Element("JewelName")!.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; + } + JewelId = model.JewelId; + JewelName = model.JewelName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + JewelId = JewelId, + JewelName = JewelName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("JewelName", JewelName), + new XElement("JewelId", JewelId.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())); + } +}