diff --git a/FurnitureAssembly/FurnitureAssembly.sln b/FurnitureAssembly/FurnitureAssembly.sln index 7f96c46..d43b05f 100644 --- a/FurnitureAssembly/FurnitureAssembly.sln +++ b/FurnitureAssembly/FurnitureAssembly.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyListImplem EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyDataModels", "FurnitureAssemblyDataModels\FurnitureAssemblyDataModels.csproj", "{F9199856-F724-49E7-A159-711AC00A7773}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyFileImplement", "FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{C4E1D51E-D2E2-43D5-8E86-ABD26A5522A3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {F9199856-F724-49E7-A159-711AC00A7773}.Debug|Any CPU.Build.0 = Debug|Any CPU {F9199856-F724-49E7-A159-711AC00A7773}.Release|Any CPU.ActiveCfg = Release|Any CPU {F9199856-F724-49E7-A159-711AC00A7773}.Release|Any CPU.Build.0 = Release|Any CPU + {C4E1D51E-D2E2-43D5-8E86-ABD26A5522A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C4E1D51E-D2E2-43D5-8E86-ABD26A5522A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C4E1D51E-D2E2-43D5-8E86-ABD26A5522A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C4E1D51E-D2E2-43D5-8E86-ABD26A5522A3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj b/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj index 6295619..bc87d38 100644 --- a/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj +++ b/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj @@ -17,6 +17,8 @@ + + diff --git a/FurnitureAssembly/FurnitureAssembly/Program.cs b/FurnitureAssembly/FurnitureAssembly/Program.cs index 6bef587..3f81912 100644 --- a/FurnitureAssembly/FurnitureAssembly/Program.cs +++ b/FurnitureAssembly/FurnitureAssembly/Program.cs @@ -1,6 +1,6 @@ using FurnitureAssemblyContracts.StoragesContracts; using Microsoft.Extensions.DependencyInjection; -using FurnitureAssemblyListImplement.Implements; +using FurnitureAssemblyFileImplement.Implements; using FurnitureAssemblyBusinessLogic.BusinessLogics; using FurnitureAssemblyContracts.BusinessLogicsContracts; using AbstractShopBusinessLogic.BusinessLogics; diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..585c244 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs @@ -0,0 +1,56 @@ +using FurnitureAssemblyFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FurnitureAssemblyFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string FurnitureFileName = "Furniture.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Furnitures { 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 SaveFurnitures() => SaveData(Furnitures, FurnitureFileName, "Furnitures", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Furnitures = LoadData(FurnitureFileName, "Furniture", x => Furniture.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/FurnitureAssembly/FurnitureAssemblyFileImplement/FurnitureAssemblyFileImplement.csproj b/FurnitureAssembly/FurnitureAssemblyFileImplement/FurnitureAssemblyFileImplement.csproj new file mode 100644 index 0000000..52299f0 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/FurnitureAssemblyFileImplement.csproj @@ -0,0 +1,15 @@ + + + + net6.0 + FurnitureAssemblyFileImplement_ + enable + enable + + + + + + + + diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ComponentStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..aa09bed --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,92 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyFileImplement.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/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs new file mode 100644 index 0000000..e06a0e2 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs @@ -0,0 +1,88 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyFileImplement.Implements +{ + public class FurnitureStorage : IFurnitureStorage + { + private readonly DataFileSingleton source; + public FurnitureStorage() + { + source = DataFileSingleton.GetInstance(); + } + public FurnitureViewModel? GetElement(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.FurnitureName) && !model.Id.HasValue) + { + return null; + } + + return source.Furnitures.FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.FurnitureName)) + { + return new(); + } + return source.Furnitures + .Where(x => x.FurnitureName.Contains(model.FurnitureName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Furnitures.Select(x => x.GetViewModel).ToList(); + } + + public FurnitureViewModel? Insert(FurnitureBindingModel model) + { + model.Id = source.Furnitures.Count > 0 ? source.Furnitures.Max(x => + x.Id) + 1 : 1; + var newFurniture = Furniture.Create(model); + if (newFurniture == null) + { + return null; + } + source.Furnitures.Add(newFurniture); + source.SaveFurnitures(); + return newFurniture.GetViewModel; + } + + public FurnitureViewModel? Update(FurnitureBindingModel model) + { + var furniture = source.Furnitures.FirstOrDefault(x => x.Id == model.Id); + if (furniture == null) + { + return null; + } + furniture.Update(model); + source.SaveFurnitures(); + return furniture.GetViewModel; + } + + public FurnitureViewModel? Delete(FurnitureBindingModel model) + { + var element = source.Furnitures.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Furnitures.Remove(element); + source.SaveFurnitures(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..f275041 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,97 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetViewModel(x)) + .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 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 element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return GetViewModel(element); + } + return null; + } + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var furniture = source.Furnitures.FirstOrDefault(x => x.Id == order.FurnitureId); + if (furniture != null) + { + viewModel.FurnitureName = furniture.FurnitureName; + } + return viewModel; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Component.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Component.cs new file mode 100644 index 0000000..7036dec --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System.Xml.Linq; + +namespace FurnitureAssemblyFileImplement.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/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Furniture.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Furniture.cs new file mode 100644 index 0000000..80ad7dc --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Furniture.cs @@ -0,0 +1,90 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Models; +using System.Xml.Linq; + +namespace FurnitureAssemblyFileImplement.Models +{ + public class Furniture : IFurnitureModel + { + public int Id { get; private set; } + public string FurnitureName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _furnitureComponents = null; + public Dictionary FurnitureComponents + { + get + { + if (_furnitureComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _furnitureComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _furnitureComponents; + } + } + + public static Furniture? Create(FurnitureBindingModel model) + { + if (model == null) + { + return null; + } + return new Furniture() + { + Id = model.Id, + FurnitureName = model.FurnitureName, + Price = model.Price, + Components = model.FurnitureComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Furniture? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Furniture() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + FurnitureName = element.Element("FurnitureName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("FurnitureComponents")!.Elements("FurnitureComponent").ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(FurnitureBindingModel model) + { + if (model == null) + { + return; + } + FurnitureName = model.FurnitureName; + Price = model.Price; + Components = model.FurnitureComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _furnitureComponents = null; + } + + public FurnitureViewModel GetViewModel => new() + { + Id = Id, + FurnitureName = FurnitureName, + Price = Price, + FurnitureComponents = FurnitureComponents + }; + public XElement GetXElement => new("Furniture", + new XAttribute("Id", Id), + new XElement("FurnitureName", FurnitureName), + new XElement("Price", Price.ToString()), + new XElement("FurnitureComponents", Components.Select( + x => new XElement("FurnitureComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Order.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Order.cs new file mode 100644 index 0000000..2d15e13 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Order.cs @@ -0,0 +1,106 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Enums; +using FurnitureAssemblyDataModels.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FurnitureAssemblyFileImplement.Models +{ + public class Order : IOrderModel + { + public int FurnitureId { 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() + { + Id = model.Id, + FurnitureId = model.FurnitureId, + 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), + FurnitureId = Convert.ToInt32(element.Element("FurnitureId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null), + }; + DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); + + order.DateImplement = dateImpl; + + if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status)) + { + return null; + } + order.Status = status; + return order; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + FurnitureId = FurnitureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("FurnitureId", FurnitureId), + 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())); + } +}