From 2b8f5bfb61ddd821d466878bdd47e6496184122d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sun, 12 Feb 2023 14:58:15 +0400 Subject: [PATCH 1/6] =?UTF-8?q?=D1=87=D1=82=D0=BE-=D1=82=D0=BE=20=D0=B2?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B1=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FoodOrders/FoodOrders.sln | 10 +- FoodOrders/FoodOrders/FoodOrdersView.csproj | 1 + FoodOrders/FoodOrders/FormCreateOrder.cs | 4 +- FoodOrders/FoodOrders/Program.cs | 2 +- .../DataFileSingleton.cs | 57 +++++++++ .../FoodOrdersFileImplement.csproj | 14 +++ .../Implements/ComponentStorage.cs | 77 ++++++++++++ .../Implements/DishStorage.cs | 99 +++++++++++++++ .../Implements/OrderStorage.cs | 118 ++++++++++++++++++ .../Models/Component.cs | 58 +++++++++ .../FoodOrdersFileImplement/Models/Dish.cs | 95 ++++++++++++++ .../FoodOrdersFileImplement/Models/Order.cs | 68 ++++++++++ .../Implements/OrderStorage.cs | 6 +- 13 files changed, 601 insertions(+), 8 deletions(-) create mode 100644 FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs create mode 100644 FoodOrders/FoodOrdersFileImplement/FoodOrdersFileImplement.csproj create mode 100644 FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs create mode 100644 FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs create mode 100644 FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs create mode 100644 FoodOrders/FoodOrdersFileImplement/Models/Component.cs create mode 100644 FoodOrders/FoodOrdersFileImplement/Models/Dish.cs create mode 100644 FoodOrders/FoodOrdersFileImplement/Models/Order.cs diff --git a/FoodOrders/FoodOrders.sln b/FoodOrders/FoodOrders.sln index 68f3899..57bc319 100644 --- a/FoodOrders/FoodOrders.sln +++ b/FoodOrders/FoodOrders.sln @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersDataModels", "Foo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersContracts", "FoodOrdersContracts\FoodOrdersContracts.csproj", "{5527E07B-9B94-4735-93FB-49267F0B8038}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersBusinessLogic", "FoodOrdersBusinessLogic\FoodOrdersBusinessLogic.csproj", "{4C44A99E-7D02-4D07-9714-9414423B560E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersBusinessLogic", "FoodOrdersBusinessLogic\FoodOrdersBusinessLogic.csproj", "{4C44A99E-7D02-4D07-9714-9414423B560E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersListImplement", "FoodOrdersListImplement\FoodOrdersListImplement.csproj", "{A4012AB3-40FB-4FF6-A55A-E593AB80A153}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersListImplement", "FoodOrdersListImplement\FoodOrdersListImplement.csproj", "{A4012AB3-40FB-4FF6-A55A-E593AB80A153}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersFileImplement", "FoodOrdersFileImplement\FoodOrdersFileImplement.csproj", "{E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {A4012AB3-40FB-4FF6-A55A-E593AB80A153}.Debug|Any CPU.Build.0 = Debug|Any CPU {A4012AB3-40FB-4FF6-A55A-E593AB80A153}.Release|Any CPU.ActiveCfg = Release|Any CPU {A4012AB3-40FB-4FF6-A55A-E593AB80A153}.Release|Any CPU.Build.0 = Release|Any CPU + {E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FoodOrders/FoodOrders/FoodOrdersView.csproj b/FoodOrders/FoodOrders/FoodOrdersView.csproj index d32249f..87aba5f 100644 --- a/FoodOrders/FoodOrders/FoodOrdersView.csproj +++ b/FoodOrders/FoodOrders/FoodOrdersView.csproj @@ -17,6 +17,7 @@ + diff --git a/FoodOrders/FoodOrders/FormCreateOrder.cs b/FoodOrders/FoodOrders/FormCreateOrder.cs index 14654f4..8ea24c8 100644 --- a/FoodOrders/FoodOrders/FormCreateOrder.cs +++ b/FoodOrders/FoodOrders/FormCreateOrder.cs @@ -45,12 +45,12 @@ namespace FoodOrdersView try { int id = Convert.ToInt32(comboBoxDish.SelectedValue); - var product = _logicS.ReadElement(new DishSearchModel + var dish = _logicS.ReadElement(new DishSearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); + textBoxSum.Text = Math.Round(count * (dish?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } catch (Exception ex) diff --git a/FoodOrders/FoodOrders/Program.cs b/FoodOrders/FoodOrders/Program.cs index 8ab0839..d33e584 100644 --- a/FoodOrders/FoodOrders/Program.cs +++ b/FoodOrders/FoodOrders/Program.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using FoodOrdersView; -using FoodOrdersListImplement.Implements; +using FoodOrdersFileImplement.Implements; using FoodOrdersBusinessLogic.BusinessLogics; namespace FoodOrders diff --git a/FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs b/FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..bbed037 --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs @@ -0,0 +1,57 @@ +using FoodOrdersFileImplement.Models; +using System.Xml.Linq; +namespace FoodOrdersFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string DishFileName = "Dish.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Dishes { 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 SaveDishes() => SaveData(Dishes, DishFileName, + "Dishes", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x + => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => + Component.Create(x)!)!; + Dishes = LoadData(DishFileName, "Dish", x => + Dish.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/FoodOrders/FoodOrdersFileImplement/FoodOrdersFileImplement.csproj b/FoodOrders/FoodOrdersFileImplement/FoodOrdersFileImplement.csproj new file mode 100644 index 0000000..51e14c2 --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/FoodOrdersFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..161c77f --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,77 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.SearchModels; +using FoodOrdersContracts.StoragesContracts; +using FoodOrdersContracts.ViewModels; +using FoodOrdersFileImplement.Models; +using FoodOrdersFileImplement; + +namespace FoodOrdersFileImplement.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.DishName) && !model.Id.HasValue) + { + return null; + } + return source.Dishes + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (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/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs new file mode 100644 index 0000000..b2c9824 --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs @@ -0,0 +1,99 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.SearchModels; +using FoodOrdersContracts.StoragesContracts; +using FoodOrdersContracts.ViewModels; +using FoodOrdersFileImplement.Models; +using FoodOrdersFileImplement; + +namespace FoodOrdersListImplement.Implements +{ + public class DishStorage : IDishStorage + { + private readonly DataFileSingleton _source; + public DishStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(DishSearchModel + model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.DishName)) + { + return result; + } + foreach (var dish in _source.Dishes) + { + if (dish.DishName.Contains(model.DishName)) + { + result.Add(dish.GetViewModel); + } + } + return result; + } + public DishViewModel? GetElement(DishSearchModel model) + { + if (string.IsNullOrEmpty(model.DishName) && !model.Id.HasValue) + { + return null; + } + foreach (var dish in _source.Dishes) + { + if ((!string.IsNullOrEmpty(model.DishName) && + dish.DishName == model.DishName) || + (model.Id.HasValue && dish.Id == model.Id)) + { + return dish.GetViewModel; + } + } + return null; + } + public DishViewModel? Insert(DishBindingModel model) + { + model.Id = 1; + foreach (var dish in _source.Dishes) + { + if (model.Id <= dish.Id) + { + model.Id = dish.Id + 1; + } + } + var newDish = Dish.Create(model); + if (newDish == null) + { + return null; + } + _source.Dishes.Add(newDish); + return newDish.GetViewModel; + } + public DishViewModel? Update(DishBindingModel model) + { + foreach (var dish in _source.Dishes) + { + if (dish.Id == model.Id) + { + dish.Update(model); + return dish.GetViewModel; + } + } + return null; + } + public DishViewModel? Delete(DishBindingModel model) + { + for (int i = 0; i < _source.Dishes.Count; ++i) + { + if (_source.Dishes[i].Id == model.Id) + { + var element = _source.Dishes[i]; + _source.Dishes.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..04785c4 --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,118 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.SearchModels; +using FoodOrdersContracts.StoragesContracts; +using FoodOrdersContracts.ViewModels; +using FoodOrdersFileImplement; + +namespace FoodOrdersListImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton _source; + public OrderStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(GetViewModel(order)); + } + return result; + } + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + if (!model.Id.HasValue) + { + return result; + } + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(GetViewModel(order)); + } + } + return result; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { + if (model.Id.HasValue && order.Id == model.Id) + { + return GetViewModel(order); + } + } + return null; + } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + foreach (var iceCream in _source.Dish) + { + if (iceCream.Id == order.DishId) + { + viewModel.DishName = iceCream.DishName; + break; + } + } + return viewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return GetViewModel(element); + } + } + return null; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return GetViewModel(order); + } + } + return null; + } + } +} diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Component.cs b/FoodOrders/FoodOrdersFileImplement/Models/Component.cs new file mode 100644 index 0000000..8719134 --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/Models/Component.cs @@ -0,0 +1,58 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDataModels.Models; +using System.Xml.Linq; +namespace FoodOrdersFileImplement.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())); + } +} \ No newline at end of file diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs new file mode 100644 index 0000000..c5fd8e7 --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs @@ -0,0 +1,95 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDataModels.Models; +using FoodOrdersFileImplement; +using System.Xml.Linq; +namespace FoodOrdersFileImplement.Models +{ + public class Dish : IDishModel + { + public int Id { get; private set; } + public string DishName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _dishComponents = + null; + public Dictionary DishComponents + { + get + { + if (_dishComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _dishComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _dishComponents; + } + } + public static Dish? Create(DishBindingModel model) + { + if (model == null) + { + return null; + } + return new Dish() + { + Id = model.Id, + DishName = model.DishName, + Price = model.Price, + Components = model.DishComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Dish? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Dish() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DishName = element.Element("DishName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = + element.Element("DishComponents")!.Elements("DishComponent") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(DishBindingModel model) + { + if (model == null) + { + return; + } + DishName = model.DishName; + Price = model.Price; + Components = model.DishComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _dishComponents = null; + } + public DishViewModel GetViewModel => new() + { + Id = Id, + DishName = DishName, + Price = Price, + DishComponents = DishComponents + }; + public XElement GetXElement => new("Dish", + new XAttribute("Id", Id), + new XElement("DishName", DishName), + new XElement("Price", Price.ToString()), + new XElement("DishComponents", Components.Select(x => + new XElement("DishComponent", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + } +} diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Order.cs b/FoodOrders/FoodOrdersFileImplement/Models/Order.cs new file mode 100644 index 0000000..38e5bc7 --- /dev/null +++ b/FoodOrders/FoodOrdersFileImplement/Models/Order.cs @@ -0,0 +1,68 @@ +using FoodOrdersContracts.BindingModels; +using FoodOrdersContracts.ViewModels; +using FoodOrdersDataModels.Enums; +using FoodOrdersDataModels.Models; +using System.Reflection.Metadata; +using System.Xml.Linq; + +namespace FoodOrdersFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int DishId { 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(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DishId = Convert.ToInt32(element.Element("DocumentId")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = Convert.ToDateTime(element.Element("DateCreate")!.Value) + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + DishId = DishId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("DishId", DishId.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()) + ); + } +} diff --git a/FoodOrders/FoodOrdersListImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersListImplement/Implements/OrderStorage.cs index fbb7cd0..c9b2696 100644 --- a/FoodOrders/FoodOrdersListImplement/Implements/OrderStorage.cs +++ b/FoodOrders/FoodOrdersListImplement/Implements/OrderStorage.cs @@ -63,11 +63,11 @@ namespace FoodOrdersListImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var iceCream in _source.Dish) + foreach (var dish in _source.Dish) { - if (iceCream.Id == order.DishId) + if (dish.Id == order.DishId) { - viewModel.DishName = iceCream.DishName; + viewModel.DishName = dish.DishName; break; } } From 1ceb6d90898cc8ccb3422faa769fa079bbc3fc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sun, 12 Feb 2023 15:28:46 +0400 Subject: [PATCH 2/6] =?UTF-8?q?=D0=BD=D0=B5=20=D0=B4=D0=BE=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D1=86=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F?= =?UTF-8?q?=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ComponentStorage.cs | 29 ++++--- .../Implements/DishStorage.cs | 80 +++++++------------ .../Implements/OrderStorage.cs | 73 ++++++----------- .../FoodOrdersFileImplement/Models/Dish.cs | 4 +- .../FoodOrdersFileImplement/Models/Order.cs | 37 +++++++-- 5 files changed, 98 insertions(+), 125 deletions(-) diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs index 161c77f..3fceecd 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs @@ -9,14 +9,14 @@ namespace FoodOrdersFileImplement.Implements { public class ComponentStorage : IComponentStorage { - private readonly DataFileSingleton source; + private readonly DataFileSingleton _source; public ComponentStorage() { - source = DataFileSingleton.GetInstance(); + _source = DataFileSingleton.GetInstance(); } public List GetFullList() { - return source.Components.Select(x => x.GetViewModel).ToList(); + return _source.Components.Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(ComponentSearchModel model) @@ -25,50 +25,49 @@ namespace FoodOrdersFileImplement.Implements { return new(); } - return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + return _source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) { - if (string.IsNullOrEmpty(model.DishName) && !model.Id.HasValue) + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) { return null; } - return source.Dishes - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + 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 => + 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(); + _source.Components.Add(newComponent); + _source.SaveComponents(); return newComponent.GetViewModel; } public ComponentViewModel? Update(ComponentBindingModel model) { - var component = source.Components.FirstOrDefault(x => x.Id == + var component = _source.Components.FirstOrDefault(x => x.Id == model.Id); if (component == null) { return null; } component.Update(model); - source.SaveComponents(); + _source.SaveComponents(); return component.GetViewModel; } public ComponentViewModel? Delete(ComponentBindingModel model) { - var element = source.Components.FirstOrDefault(x => x.Id == + var element = _source.Components.FirstOrDefault(x => x.Id == model.Id); if (element != null) { - source.Components.Remove(element); - source.SaveComponents(); + _source.Components.Remove(element); + _source.SaveComponents(); return element.GetViewModel; } return null; diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs index b2c9824..4688721 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs @@ -3,9 +3,9 @@ using FoodOrdersContracts.SearchModels; using FoodOrdersContracts.StoragesContracts; using FoodOrdersContracts.ViewModels; using FoodOrdersFileImplement.Models; -using FoodOrdersFileImplement; +using System.Reflection.Metadata; -namespace FoodOrdersListImplement.Implements +namespace FoodOrdersFileImplement.Implements { public class DishStorage : IDishStorage { @@ -16,24 +16,15 @@ namespace FoodOrdersListImplement.Implements } public List GetFullList() { - return source.Components.Select(x => x.GetViewModel).ToList(); + return _source.Dishes.Select(x => x.GetViewModel).ToList(); } - public List GetFilteredList(DishSearchModel - model) + public List GetFilteredList(DishSearchModel model) { - var result = new List(); if (string.IsNullOrEmpty(model.DishName)) { - return result; + return new(); } - foreach (var dish in _source.Dishes) - { - if (dish.DishName.Contains(model.DishName)) - { - result.Add(dish.GetViewModel); - } - } - return result; + return _source.Dishes.Where(x => x.DishName.Contains(model.DishName)).Select(x => x.GetViewModel).ToList(); } public DishViewModel? GetElement(DishSearchModel model) { @@ -41,59 +32,44 @@ namespace FoodOrdersListImplement.Implements { return null; } - foreach (var dish in _source.Dishes) - { - if ((!string.IsNullOrEmpty(model.DishName) && - dish.DishName == model.DishName) || - (model.Id.HasValue && dish.Id == model.Id)) - { - return dish.GetViewModel; - } - } - return null; + return _source.Dishes + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public DishViewModel? Insert(DishBindingModel model) { - model.Id = 1; - foreach (var dish in _source.Dishes) - { - if (model.Id <= dish.Id) - { - model.Id = dish.Id + 1; - } - } - var newDish = Dish.Create(model); - if (newDish == null) + model.Id = _source.Dishes.Count > 0 ? _source.Dishes.Max(x => x.Id) + 1 : 1; + var newDoc = Dish.Create(model); + if (newDoc == null) { return null; } - _source.Dishes.Add(newDish); - return newDish.GetViewModel; + _source.Dishes.Add(newDoc); + _source.SaveDishes(); + return newDoc.GetViewModel; } + public DishViewModel? Update(DishBindingModel model) { - foreach (var dish in _source.Dishes) + var dish = _source.Dishes.FirstOrDefault(x => x.Id == model.Id); + if (dish == null) { - if (dish.Id == model.Id) - { - dish.Update(model); - return dish.GetViewModel; - } + return null; } - return null; + dish.Update(model); + _source.SaveDishes(); + return dish.GetViewModel; } + public DishViewModel? Delete(DishBindingModel model) { - for (int i = 0; i < _source.Dishes.Count; ++i) + var document = _source.Dishes.FirstOrDefault(x => x.Id == model.Id); + if (document == null) { - if (_source.Dishes[i].Id == model.Id) - { - var element = _source.Dishes[i]; - _source.Dishes.RemoveAt(i); - return element.GetViewModel; - } + return null; } - return null; + document.Update(model); + _source.SaveDishes(); + return document.GetViewModel; } } } \ No newline at end of file diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs index 04785c4..4feef54 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs @@ -3,8 +3,9 @@ using FoodOrdersContracts.SearchModels; using FoodOrdersContracts.StoragesContracts; using FoodOrdersContracts.ViewModels; using FoodOrdersFileImplement; +using FoodOrdersFileImplement.Models; -namespace FoodOrdersListImplement.Implements +namespace FoodOrdersFileImplement.Implements { public class OrderStorage : IOrderStorage { @@ -15,28 +16,15 @@ namespace FoodOrdersListImplement.Implements } public List GetFullList() { - var result = new List(); - foreach (var order in _source.Orders) - { - result.Add(GetViewModel(order)); - } - return result; + return _source.Orders.Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(OrderSearchModel model) { - var result = new List(); if (!model.Id.HasValue) { - return result; + return new(); } - foreach (var order in _source.Orders) - { - if (order.Id == model.Id) - { - result.Add(GetViewModel(order)); - } - } - return result; + return _source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -45,20 +33,14 @@ namespace FoodOrdersListImplement.Implements { return null; } - foreach (var order in _source.Orders) - { - if (model.Id.HasValue && order.Id == model.Id) - { - return GetViewModel(order); - } - } - return null; + return _source.Orders + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var iceCream in _source.Dish) + foreach (var iceCream in _source.Dishes) { if (iceCream.Id == order.DishId) { @@ -71,48 +53,39 @@ namespace FoodOrdersListImplement.Implements public OrderViewModel? Delete(OrderBindingModel model) { - for (int i = 0; i < _source.Orders.Count; ++i) + var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { - if (_source.Orders[i].Id == model.Id) - { - var element = _source.Orders[i]; - _source.Orders.RemoveAt(i); - return GetViewModel(element); - } + return null; } - return null; + order.Update(model); + _source.SaveOrders(); + return order.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { - model.Id = 1; - foreach (var order in _source.Orders) - { - if (model.Id <= order.Id) - { - model.Id = order.Id + 1; - } - } + 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); - return GetViewModel(newOrder); + _source.SaveOrders(); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { - foreach (var order in _source.Orders) + var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { - if (order.Id == model.Id) - { - order.Update(model); - return GetViewModel(order); - } + return null; } - return null; + order.Update(model); + _source.SaveOrders(); + return order.GetViewModel; } } } diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs index c5fd8e7..cbe04fb 100644 --- a/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs +++ b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs @@ -19,9 +19,9 @@ namespace FoodOrdersFileImplement.Models { if (_dishComponents == null) { - var source = DataFileSingleton.GetInstance(); + var _source = DataFileSingleton.GetInstance(); _dishComponents = Components.ToDictionary(x => x.Key, y => - ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + ((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); } return _dishComponents; diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Order.cs b/FoodOrders/FoodOrdersFileImplement/Models/Order.cs index 38e5bc7..695c825 100644 --- a/FoodOrders/FoodOrdersFileImplement/Models/Order.cs +++ b/FoodOrders/FoodOrdersFileImplement/Models/Order.cs @@ -23,15 +23,40 @@ namespace FoodOrdersFileImplement.Models { return null; } - return new Order() + var order = new Order() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), - DishId = Convert.ToInt32(element.Element("DocumentId")!.Value), - Sum = Convert.ToDouble(element.Element("Sum")!.Value), + DishId = Convert.ToInt32(element.Element("DishId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), - Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), - DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), - DateImplement = Convert.ToDateTime(element.Element("DateCreate")!.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 static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + Id = model.Id, + DishId = model.DishId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, }; } public void Update(OrderBindingModel? model) From 433410161d7edec3d91a7ecb0bd339960cbe7ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sun, 12 Feb 2023 15:34:57 +0400 Subject: [PATCH 3/6] =?UTF-8?q?2=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FoodOrdersFileImplement/Implements/OrderStorage.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs index 4feef54..1024b80 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs @@ -16,7 +16,7 @@ namespace FoodOrdersFileImplement.Implements } public List GetFullList() { - return _source.Orders.Select(x => x.GetViewModel).ToList(); + return _source.Orders.Select(x => GetViewModel(x)).ToList(); } public List GetFilteredList(OrderSearchModel model) { @@ -24,7 +24,7 @@ namespace FoodOrdersFileImplement.Implements { return new(); } - return _source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + return _source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -40,11 +40,11 @@ namespace FoodOrdersFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var iceCream in _source.Dishes) + foreach (var dish in _source.Dishes) { - if (iceCream.Id == order.DishId) + if (dish.Id == order.DishId) { - viewModel.DishName = iceCream.DishName; + viewModel.DishName = dish.DishName; break; } } From 584401e1d64b30807709fd34f7974b875097bc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sun, 12 Feb 2023 15:55:52 +0400 Subject: [PATCH 4/6] fix --- .../Implements/OrderStorage.cs | 1 - .../FoodOrdersFileImplement/Models/Dish.cs | 6 +++--- .../FoodOrdersFileImplement/Models/Order.cs | 17 +++++------------ 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs index 1024b80..f27af04 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs @@ -2,7 +2,6 @@ using FoodOrdersContracts.SearchModels; using FoodOrdersContracts.StoragesContracts; using FoodOrdersContracts.ViewModels; -using FoodOrdersFileImplement; using FoodOrdersFileImplement.Models; namespace FoodOrdersFileImplement.Implements diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs index cbe04fb..fb355f3 100644 --- a/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs +++ b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs @@ -84,11 +84,11 @@ namespace FoodOrdersFileImplement.Models new XElement("DishName", DishName), new XElement("Price", Price.ToString()), new XElement("DishComponents", Components.Select(x => - new XElement("DishComponent", + new XElement("DishComponent", - new XElement("Key", x.Key), + new XElement("Key", x.Key), - new XElement("Value", x.Value))) + new XElement("Value", x.Value))) .ToArray())); } diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Order.cs b/FoodOrders/FoodOrdersFileImplement/Models/Order.cs index 695c825..4dafdb7 100644 --- a/FoodOrders/FoodOrdersFileImplement/Models/Order.cs +++ b/FoodOrders/FoodOrdersFileImplement/Models/Order.cs @@ -23,23 +23,16 @@ namespace FoodOrdersFileImplement.Models { return null; } - var order = new Order() + return new Order() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), DishId = Convert.ToInt32(element.Element("DishId")!.Value), - Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), - DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) }; - 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 static Order? Create(OrderBindingModel? model) From 4b938e90ca046db8e45849a13d5912d73fb0bf71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sun, 12 Mar 2023 10:35:52 +0400 Subject: [PATCH 5/6] fix --- .../DataFileSingleton.cs | 27 ++++------ .../Implements/ComponentStorage.cs | 14 +++--- .../Implements/DishStorage.cs | 3 +- .../Implements/OrderStorage.cs | 3 +- .../Models/Component.cs | 10 ++-- .../FoodOrdersFileImplement/Models/Dish.cs | 49 +++++++++---------- 6 files changed, 47 insertions(+), 59 deletions(-) diff --git a/FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs b/FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs index bbed037..b3e5803 100644 --- a/FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs +++ b/FoodOrders/FoodOrdersFileImplement/DataFileSingleton.cs @@ -19,37 +19,28 @@ namespace FoodOrdersFileImplement } return instance; } - public void SaveComponents() => SaveData(Components, ComponentFileName, - "Components", x => x.GetXElement); - public void SaveDishes() => SaveData(Dishes, DishFileName, - "Dishes", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x - => x.GetXElement); + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveDishes() => SaveData(Dishes, DishFileName, "Dishes", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); private DataFileSingleton() { - Components = LoadData(ComponentFileName, "Component", x => - Component.Create(x)!)!; - Dishes = LoadData(DishFileName, "Dish", x => - Dish.Create(x)!)!; + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Dishes = LoadData(DishFileName, "Dish", x => Dish.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } - private static List? LoadData(string filename, string xmlNodeName, - Func selectFunction) + 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 XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); } return new List(); } - private static void SaveData(List data, string filename, string - xmlNodeName, Func selectFunction) + 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); + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); } } } diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs index 3fceecd..c7b5a12 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs @@ -18,8 +18,7 @@ namespace FoodOrdersFileImplement.Implements { return _source.Components.Select(x => x.GetViewModel).ToList(); } - public List GetFilteredList(ComponentSearchModel - model) + public List GetFilteredList(ComponentSearchModel model) { if (string.IsNullOrEmpty(model.ComponentName)) { @@ -27,6 +26,8 @@ namespace FoodOrdersFileImplement.Implements } return _source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); } + + //FirstOrDefault выбирается первый или ничего, то есть вернёт первое совпадение или null public ComponentViewModel? GetElement(ComponentSearchModel model) { if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) @@ -37,8 +38,7 @@ namespace FoodOrdersFileImplement.Implements } public ComponentViewModel? Insert(ComponentBindingModel model) { - model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => - x.Id) + 1 : 1; + model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => x.Id) + 1 : 1; var newComponent = Component.Create(model); if (newComponent == null) { @@ -50,8 +50,7 @@ namespace FoodOrdersFileImplement.Implements } public ComponentViewModel? Update(ComponentBindingModel model) { - var component = _source.Components.FirstOrDefault(x => x.Id == - model.Id); + var component = _source.Components.FirstOrDefault(x => x.Id == model.Id); if (component == null) { return null; @@ -62,8 +61,7 @@ namespace FoodOrdersFileImplement.Implements } public ComponentViewModel? Delete(ComponentBindingModel model) { - var element = _source.Components.FirstOrDefault(x => x.Id == - model.Id); + var element = _source.Components.FirstOrDefault(x => x.Id == model.Id); if (element != null) { _source.Components.Remove(element); diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs index 4688721..6dd0909 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/DishStorage.cs @@ -32,8 +32,7 @@ namespace FoodOrdersFileImplement.Implements { return null; } - return _source.Dishes - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return _source.Dishes.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public DishViewModel? Insert(DishBindingModel model) { diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs index f27af04..88289ba 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs @@ -32,8 +32,7 @@ namespace FoodOrdersFileImplement.Implements { return null; } - return _source.Orders - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return _source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } private OrderViewModel GetViewModel(Order order) diff --git a/FoodOrders/FoodOrdersFileImplement/Models/Component.cs b/FoodOrders/FoodOrdersFileImplement/Models/Component.cs index 8719134..951dad9 100644 --- a/FoodOrders/FoodOrdersFileImplement/Models/Component.cs +++ b/FoodOrders/FoodOrdersFileImplement/Models/Component.cs @@ -50,9 +50,11 @@ namespace FoodOrdersFileImplement.Models ComponentName = ComponentName, Cost = Cost }; - public XElement GetXElement => new("Component", - new XAttribute("Id", Id), - new XElement("ComponentName", ComponentName), - new XElement("Cost", Cost.ToString())); + 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/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs index fb355f3..46cded5 100644 --- a/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs +++ b/FoodOrders/FoodOrdersFileImplement/Models/Dish.cs @@ -8,11 +8,16 @@ namespace FoodOrdersFileImplement.Models public class Dish : IDishModel { public int Id { get; private set; } + public string DishName { get; private set; } = string.Empty; + public double Price { get; private set; } + + //словарь для файла, так как нам в файле нужно хранить просто id компонента и его количество public Dictionary Components { get; private set; } = new(); - private Dictionary? _dishComponents = - null; + + private Dictionary? _dishComponents = null; + public Dictionary DishComponents { get @@ -20,9 +25,7 @@ namespace FoodOrdersFileImplement.Models if (_dishComponents == null) { var _source = DataFileSingleton.GetInstance(); - _dishComponents = Components.ToDictionary(x => x.Key, y => - ((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, - y.Value)); + _dishComponents = Components.ToDictionary(x => x.Key, y => ((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); } return _dishComponents; } @@ -38,8 +41,7 @@ namespace FoodOrdersFileImplement.Models Id = model.Id, DishName = model.DishName, Price = model.Price, - Components = model.DishComponents.ToDictionary(x => x.Key, x - => x.Value.Item2) + Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2) }; } public static Dish? Create(XElement element) @@ -53,11 +55,7 @@ namespace FoodOrdersFileImplement.Models Id = Convert.ToInt32(element.Attribute("Id")!.Value), DishName = element.Element("DishName")!.Value, Price = Convert.ToDouble(element.Element("Price")!.Value), - Components = - element.Element("DishComponents")!.Elements("DishComponent") - .ToDictionary(x => - Convert.ToInt32(x.Element("Key")?.Value), x => - Convert.ToInt32(x.Element("Value")?.Value)) + Components = element.Element("DishComponents")!.Elements("DishComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } public void Update(DishBindingModel model) @@ -68,8 +66,9 @@ namespace FoodOrdersFileImplement.Models } DishName = model.DishName; Price = model.Price; - Components = model.DishComponents.ToDictionary(x => x.Key, x => - x.Value.Item2); + Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + //обнуляем словарь, чтобы в случае обновления, у нас был в дальнейшем сформирован актуальный словарь + // с помощью get метода _dishComponents = null; } public DishViewModel GetViewModel => new() @@ -79,17 +78,17 @@ namespace FoodOrdersFileImplement.Models Price = Price, DishComponents = DishComponents }; - public XElement GetXElement => new("Dish", - new XAttribute("Id", Id), - new XElement("DishName", DishName), - new XElement("Price", Price.ToString()), - new XElement("DishComponents", Components.Select(x => - new XElement("DishComponent", - new XElement("Key", x.Key), - - new XElement("Value", x.Value))) - - .ToArray())); + public XElement GetXElement => new( + "Dish", + new XAttribute("Id", Id), + new XElement("DishName", DishName), + new XElement("Price", Price.ToString()), + new XElement("DishComponents", Components.Select(x => + new XElement("DishComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray() + ) + ); } } From bdfba8ffa79ff665d8080c15c10b298d260ba69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sun, 12 Mar 2023 10:57:32 +0400 Subject: [PATCH 6/6] fix --- .../Implements/OrderStorage.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs index 88289ba..49bc5d3 100644 --- a/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs +++ b/FoodOrders/FoodOrdersFileImplement/Implements/OrderStorage.cs @@ -3,6 +3,8 @@ using FoodOrdersContracts.SearchModels; using FoodOrdersContracts.StoragesContracts; using FoodOrdersContracts.ViewModels; using FoodOrdersFileImplement.Models; +using System.Reflection; +using System.Security.Cryptography.X509Certificates; namespace FoodOrdersFileImplement.Implements { @@ -38,14 +40,7 @@ namespace FoodOrdersFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var dish in _source.Dishes) - { - if (dish.Id == order.DishId) - { - viewModel.DishName = dish.DishName; - break; - } - } + viewModel.DishName = _source.Dishes.FirstOrDefault(x => (x.Id == order.DishId))?.DishName; return viewModel; }