diff --git a/FlowerShopFileImplement/Component.cs b/FlowerShopFileImplement/Component.cs new file mode 100644 index 0000000..9353091 --- /dev/null +++ b/FlowerShopFileImplement/Component.cs @@ -0,0 +1,60 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; + +using System.Xml.Linq; + +namespace FlowerShopFileImplement.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/FlowerShopFileImplement/ComponentStorage.cs b/FlowerShopFileImplement/ComponentStorage.cs new file mode 100644 index 0000000..98765f2 --- /dev/null +++ b/FlowerShopFileImplement/ComponentStorage.cs @@ -0,0 +1,92 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopFileImplement.Models; +using FlowerShopFileImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopFileImplement.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/FlowerShopFileImplement/DataFileSingleton.cs b/FlowerShopFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..c39bd1a --- /dev/null +++ b/FlowerShopFileImplement/DataFileSingleton.cs @@ -0,0 +1,51 @@ +using FlowerShopFileImplement.Models; +using System.Xml.Linq; + +namespace FlowerShopFileImplement.Implements; + +internal class DataFileSingleton +{ + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string FlowerFileName = "Product.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Flowers { 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 SaveFlowers() => SaveData(Flowers, FlowerFileName, + "Flowers", x => x.GetXElement); + public void SaveOrders() { } + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Flowers = LoadData(FlowerFileName, "Flower", x => Flower.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); + } + } +} diff --git a/FlowerShopFileImplement/Flower.cs b/FlowerShopFileImplement/Flower.cs new file mode 100644 index 0000000..fce4568 --- /dev/null +++ b/FlowerShopFileImplement/Flower.cs @@ -0,0 +1,95 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System.Xml.Linq; +namespace FlowerShopFileImplement.Models +{ + public class Flower : IFlowerModel + { + public int Id { get; private set; } + public string FlowerName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _flowerComponents = + null; + public Dictionary FlowerComponents + { + get + { + if (_flowerComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _flowerComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _flowerComponents; + } + } + public static Flower? Create(FlowerBindingModel model) + { + if (model == null) + { + return null; + } + return new Flower() + { + Id = model.Id, + FlowerName = model.FlowerName, + Price = model.Price, + Components = model.FlowerComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Flower? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Flower() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + FlowerName = element.Element("FlowerName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = + element.Element("FlowerComponents")!.Elements("FlowerComponent") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(FlowerBindingModel model) + { + if (model == null) + { + return; + } + FlowerName = model.FlowerName; + Price = model.Price; + Components = model.FlowerComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _flowerComponents = null; + } + public FlowerViewModel GetViewModel => new() + { + Id = Id, + FlowerName = FlowerName, + Price = Price, + FlowerComponents = FlowerComponents + }; + public XElement GetXElement => new("Flower", + new XAttribute("Id", Id), + new XElement("FlowerName", FlowerName), + new XElement("Price", Price.ToString()), + new XElement("FlowerComponents", Components.Select(x => + new XElement("FlowerComponent", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + } +} + diff --git a/FlowerShopFileImplement/FlowerShopFileImplement.csproj b/FlowerShopFileImplement/FlowerShopFileImplement.csproj new file mode 100644 index 0000000..ca6fa62 --- /dev/null +++ b/FlowerShopFileImplement/FlowerShopFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/FlowerShopFileImplement/FlowerStorage.cs b/FlowerShopFileImplement/FlowerStorage.cs new file mode 100644 index 0000000..c06d89a --- /dev/null +++ b/FlowerShopFileImplement/FlowerStorage.cs @@ -0,0 +1,94 @@ +using FlowerShopFileImplement.Models; +using FlowerShopFileImplement.Implements; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopFileImplement.Implements +{ + public class FlowerStorage : IFlowerStorage + { + private readonly DataFileSingleton source; + public FlowerStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Flowers + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(FlowerSearchModel model) + { + if (string.IsNullOrEmpty(model.FlowerName)) + { + return new(); + } + return source.Flowers + .Where(x => x.FlowerName.Contains(model.FlowerName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public FlowerViewModel? GetElement(FlowerSearchModel model) + { + if (string.IsNullOrEmpty(model.FlowerName) && !model.Id.HasValue) + { + return null; + } + return source.Flowers + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.FlowerName) && x.FlowerName == + model.FlowerName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public FlowerViewModel? Insert(FlowerBindingModel model) + { + model.Id = source.Flowers.Count > 0 ? source.Flowers.Max(x => x.Id) + 1 : 1; + var newFlower = Flower.Create(model); + if (newFlower == null) + { + return null; + } + source.Flowers.Add(newFlower); + source.SaveFlowers(); + return newFlower.GetViewModel; + } + + public FlowerViewModel? Update(FlowerBindingModel model) + { + var iceCream = source.Flowers.FirstOrDefault(x => x.Id == + model.Id); + if (iceCream == null) + { + return null; + } + iceCream.Update(model); + source.SaveFlowers(); + return iceCream.GetViewModel; + } + public FlowerViewModel? Delete(FlowerBindingModel model) + { + var iceCream = source.Flowers.FirstOrDefault(x => x.Id == + model.Id); + if (iceCream != null) + { + source.Flowers.Remove(iceCream); + source.SaveFlowers(); + return iceCream.GetViewModel; + } + return null; + } + + } +} diff --git a/FlowerShopFileImplement/Order.cs b/FlowerShopFileImplement/Order.cs new file mode 100644 index 0000000..fde0ad5 --- /dev/null +++ b/FlowerShopFileImplement/Order.cs @@ -0,0 +1,83 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Enums; +using FlowerShopDataModels; +using System.Xml.Linq; + +namespace FlowerShopFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int FlowerId { 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; } + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + FlowerId = model.FlowerId, + 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; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + FlowerId = Convert.ToInt32(element.Element("FlowerId")!.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.ToString()), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + FlowerId = FlowerId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("FlowerId", FlowerId), + new XElement("Sum", Sum.ToString()), + new XElement("Count", Count), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + + } +} diff --git a/FlowerShopFileImplement/OrderStorage.cs b/FlowerShopFileImplement/OrderStorage.cs new file mode 100644 index 0000000..5c2f3b7 --- /dev/null +++ b/FlowerShopFileImplement/OrderStorage.cs @@ -0,0 +1,104 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopFileImplement.Models; +using FlowerShopFileImplement.Implements; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Orders + .Select(x => AccessIceCreamStorage(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 => AccessIceCreamStorage(x.GetViewModel)) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return AccessIceCreamStorage(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 AccessIceCreamStorage(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 AccessIceCreamStorage(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 AccessIceCreamStorage(element.GetViewModel); + } + return null; + } + + public OrderViewModel AccessIceCreamStorage(OrderViewModel model) + { + if (model == null) + return null; + foreach (var flower in source.Flowers) + { + if (flower.Id == model.FlowerId) + { + model.FlowerName = flower.FlowerName; + break; + } + } + return model; + } + + } +} diff --git a/ProjectFlowerShop.sln b/ProjectFlowerShop.sln index 075c8e0..0bcd78c 100644 --- a/ProjectFlowerShop.sln +++ b/ProjectFlowerShop.sln @@ -7,11 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjectFlowerShop", "Projec EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopDataModels", "FlowerShopDataModels\FlowerShopDataModels.csproj", "{9E7C9D26-3932-4020-893D-1757DB2048B6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopContracts", "FlowerShopContracts\FlowerShopContracts.csproj", "{843D517F-A9AE-4AF9-90C5-DB3E11D576E7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopContracts", "FlowerShopContracts\FlowerShopContracts.csproj", "{843D517F-A9AE-4AF9-90C5-DB3E11D576E7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopBusinessLogic", "FlowerShopBusinessLogic\FlowerShopBusinessLogic.csproj", "{EA8DFC63-7280-4160-8EF8-DDBAF3B64F31}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopBusinessLogic", "FlowerShopBusinessLogic\FlowerShopBusinessLogic.csproj", "{EA8DFC63-7280-4160-8EF8-DDBAF3B64F31}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{62DAA9A0-9A71-4117-8D06-8825E1678D9D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{62DAA9A0-9A71-4117-8D06-8825E1678D9D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{0CFC7A18-2E56-4D0B-80AD-F52893222027}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {62DAA9A0-9A71-4117-8D06-8825E1678D9D}.Debug|Any CPU.Build.0 = Debug|Any CPU {62DAA9A0-9A71-4117-8D06-8825E1678D9D}.Release|Any CPU.ActiveCfg = Release|Any CPU {62DAA9A0-9A71-4117-8D06-8825E1678D9D}.Release|Any CPU.Build.0 = Release|Any CPU + {0CFC7A18-2E56-4D0B-80AD-F52893222027}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0CFC7A18-2E56-4D0B-80AD-F52893222027}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0CFC7A18-2E56-4D0B-80AD-F52893222027}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0CFC7A18-2E56-4D0B-80AD-F52893222027}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ProjectFlowerShop/Program.cs b/ProjectFlowerShop/Program.cs index d423dcb..3c7ecd9 100644 --- a/ProjectFlowerShop/Program.cs +++ b/ProjectFlowerShop/Program.cs @@ -1,7 +1,7 @@ using FlowerShopBusinessLogic.BusinessLogic; using FlowerShopContracts.BusinessLogicsContracts; using FlowerShopContracts.StoragesContracts; -using FlowerShopListImplement.Implements; +using FlowerShopFileImplement.Implements; using ProjectFlowerShop; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -45,7 +45,7 @@ namespace ProjectFlowerShop services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); } diff --git a/ProjectFlowerShop/ProjectFlowerShop.csproj b/ProjectFlowerShop/ProjectFlowerShop.csproj index 337ad0f..b135f64 100644 --- a/ProjectFlowerShop/ProjectFlowerShop.csproj +++ b/ProjectFlowerShop/ProjectFlowerShop.csproj @@ -17,6 +17,7 @@ +