diff --git a/AbstractShop/AbstractShop.sln b/AbstractShop/AbstractShop.sln index 66e3195..0667114 100644 --- a/AbstractShop/AbstractShop.sln +++ b/AbstractShop/AbstractShop.sln @@ -5,13 +5,15 @@ VisualStudioVersion = 17.2.32616.157 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopView", "AbstractShopView\AbstractShopView.csproj", "{30272FA6-ABB7-4436-AFC6-50478DF2B878}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopDataModels", "AbstractShopDataModels\AbstractShopDataModels.csproj", "{E47BFE5D-69E3-4A3F-85BE-251F10EAFA02}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopDataModels", "AbstractShopDataModels\AbstractShopDataModels.csproj", "{E47BFE5D-69E3-4A3F-85BE-251F10EAFA02}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopContracts", "AbstractShopContracts\AbstractShopContracts.csproj", "{9AD66AC2-51DF-4E16-83E9-35C1CA19E774}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopContracts", "AbstractShopContracts\AbstractShopContracts.csproj", "{9AD66AC2-51DF-4E16-83E9-35C1CA19E774}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopListImplement", "AbstractShopListImplement\AbstractShopListImplement.csproj", "{74283CE4-7CE6-42AC-AA8D-EE4497E08D61}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopListImplement", "AbstractShopListImplement\AbstractShopListImplement.csproj", "{74283CE4-7CE6-42AC-AA8D-EE4497E08D61}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopBusinessLogic", "AbstractShopBusinessLogic\AbstractShopBusinessLogic.csproj", "{9A10E59F-081B-4C4D-AB16-15A777B66502}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopBusinessLogic", "AbstractShopBusinessLogic\AbstractShopBusinessLogic.csproj", "{9A10E59F-081B-4C4D-AB16-15A777B66502}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopFileImplement", "AbstractShopFileImplement\AbstractShopFileImplement.csproj", "{3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {9A10E59F-081B-4C4D-AB16-15A777B66502}.Debug|Any CPU.Build.0 = Debug|Any CPU {9A10E59F-081B-4C4D-AB16-15A777B66502}.Release|Any CPU.ActiveCfg = Release|Any CPU {9A10E59F-081B-4C4D-AB16-15A777B66502}.Release|Any CPU.Build.0 = Release|Any CPU + {3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AbstractShop/AbstractShopFileImplement/AbstractShopFileImplement.csproj b/AbstractShop/AbstractShopFileImplement/AbstractShopFileImplement.csproj new file mode 100644 index 0000000..81fc427 --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/AbstractShopFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/AbstractShop/AbstractShopFileImplement/DataFileSingleton.cs b/AbstractShop/AbstractShopFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..17747ad --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/DataFileSingleton.cs @@ -0,0 +1,61 @@ +using AbstractShopFileImplement.Models; +using System.Xml.Linq; + +namespace AbstractShopFileImplement +{ + 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 Products { 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)!)!; + Products = LoadData(ProductFileName, "Product", x => Product.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); + } + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopFileImplement/Implements/ComponentStorage.cs b/AbstractShop/AbstractShopFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..02fb2ee --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,86 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; +using AbstractShopFileImplement.Models; + +namespace AbstractShopFileImplement.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/AbstractShop/AbstractShopFileImplement/Implements/OrderStorage.cs b/AbstractShop/AbstractShopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..cde0417 --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,40 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + throw new NotImplementedException(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(OrderSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + throw new NotImplementedException(); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopFileImplement/Implements/ProductStorage.cs b/AbstractShop/AbstractShopFileImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..923bcd1 --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/Implements/ProductStorage.cs @@ -0,0 +1,40 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopFileImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public ProductViewModel? Delete(ProductBindingModel model) + { + throw new NotImplementedException(); + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(ProductSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + throw new NotImplementedException(); + } + + public ProductViewModel? Update(ProductBindingModel model) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopFileImplement/Models/Component.cs b/AbstractShop/AbstractShopFileImplement/Models/Component.cs new file mode 100644 index 0000000..b042a5c --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/Models/Component.cs @@ -0,0 +1,66 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.ViewModels; +using AbstractShopDataModels.Models; +using System.Xml.Linq; + +namespace AbstractShopFileImplement.Models +{ + internal 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())); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopFileImplement/Models/Order.cs b/AbstractShop/AbstractShopFileImplement/Models/Order.cs new file mode 100644 index 0000000..0f9ab84 --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/Models/Order.cs @@ -0,0 +1,46 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.ViewModels; +using AbstractShopDataModels.Enums; +using AbstractShopDataModels.Models; +using System.Xml.Linq; + +namespace AbstractShopFileImplement.Models +{ + internal class Order : IOrderModel + { + public int ProductId => throw new NotImplementedException(); + + public int Count => throw new NotImplementedException(); + + public double Sum => throw new NotImplementedException(); + + public OrderStatus Status => throw new NotImplementedException(); + + public DateTime DateCreate => throw new NotImplementedException(); + + public DateTime? DateImplement => throw new NotImplementedException(); + + public int Id => throw new NotImplementedException(); + + public static Order? Create(OrderBindingModel? model) + { + return new Order(); + } + + public static Order? Create(XElement element) + { + return new Order(); + } + + public void Update(OrderBindingModel? model) + { + + } + + public OrderViewModel GetViewModel => new() + { + }; + + public XElement GetXElement => new("Order"); + } +} diff --git a/AbstractShop/AbstractShopFileImplement/Models/Product.cs b/AbstractShop/AbstractShopFileImplement/Models/Product.cs new file mode 100644 index 0000000..68fcedc --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/Models/Product.cs @@ -0,0 +1,93 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.ViewModels; +using AbstractShopDataModels.Models; +using System.Xml.Linq; + +namespace AbstractShopFileImplement.Models +{ + internal class Product : IProductModel + { + public int Id { get; private set; } + + public string ProductName { 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 Product? Create(ProductBindingModel? model) + { + if (model == null) + { + return null; + } + return new Product() + { + Id = model.Id, + ProductName = model.ProductName, + Price = model.Price, + Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Product? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Product() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ProductName = 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(ProductBindingModel? model) + { + if (model == null) + { + return; + } + ProductName = model.ProductName; + Price = model.Price; + Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _productComponents = null; + } + + public ProductViewModel GetViewModel => new() + { + Id = Id, + ProductName = ProductName, + Price = Price, + ProductComponents = ProductComponents + }; + + public XElement GetXElement => new("Product", + new XAttribute("Id", Id), + new XElement("ProductName", ProductName), + 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())); + } +} diff --git a/AbstractShop/AbstractShopView/AbstractShopView.csproj b/AbstractShop/AbstractShopView/AbstractShopView.csproj index 6f29fe8..aaf2d90 100644 --- a/AbstractShop/AbstractShopView/AbstractShopView.csproj +++ b/AbstractShop/AbstractShopView/AbstractShopView.csproj @@ -28,6 +28,7 @@ + diff --git a/AbstractShop/AbstractShopView/Program.cs b/AbstractShop/AbstractShopView/Program.cs index c58359a..812c020 100644 --- a/AbstractShop/AbstractShopView/Program.cs +++ b/AbstractShop/AbstractShopView/Program.cs @@ -1,7 +1,7 @@ using AbstractShopBusinessLogic.BusinessLogics; using AbstractShopContracts.BusinessLogicsContracts; using AbstractShopContracts.StoragesContracts; -using AbstractShopListImplement.Implements; +using AbstractShopFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;