From 67096788d944d73d90bd85dd258fb0fc3abfedb1 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 14:27:20 +0400 Subject: [PATCH 1/6] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20?= =?UTF-8?q?=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 49 +++++++++++ .../Implements/ComponentStorage.cs | 76 ++++++++++++++++ .../Implements/OrderStorage.cs | 74 ++++++++++++++++ .../Implements/SushiStorage.cs | 81 +++++++++++++++++ .../SushiBarFileImplement/Models/Component.cs | 59 +++++++++++++ .../SushiBarFileImplement/Models/Order.cs | 87 +++++++++++++++++++ .../SushiBarFileImplement/Models/Sushi.cs | 87 +++++++++++++++++++ 7 files changed, 513 insertions(+) create mode 100644 SushiBar/SushiBarFileImplement/DataFileSingleton.cs create mode 100644 SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs create mode 100644 SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs create mode 100644 SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs create mode 100644 SushiBar/SushiBarFileImplement/Models/Component.cs create mode 100644 SushiBar/SushiBarFileImplement/Models/Order.cs create mode 100644 SushiBar/SushiBarFileImplement/Models/Sushi.cs diff --git a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..1cfada8 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs @@ -0,0 +1,49 @@ +using SushiBarFileImplement.Models; +using System.Linq; +using System.Xml.Linq; + +namespace SushiBarFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string SushiFileName = "Sushi.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Sushis { 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 SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Sushis = LoadData(SushiFileName, "Product", x => Sushi.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/SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs b/SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..b8dbe78 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,76 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.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/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..7d902cb --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,74 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => 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 => + x.GetViewModel).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Orders.FirstOrDefault(x => + (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 newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..8423504 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs @@ -0,0 +1,81 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.Implements +{ + public class SushiStorage : ISushiStorage + { + private readonly DataFileSingleton source; + public SushiStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Sushis.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName)) + { + return new(); + } + return source.Sushis.Where(x => + x.SushiName.Contains(model.SushiName)).Select(x => + x.GetViewModel).ToList(); + } + + public SushiViewModel? GetElement(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) + { + return null; + } + return source.Sushis.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SushiName) && + x.SushiName == model.SushiName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public SushiViewModel? Insert(SushiBindingModel model) + { + model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1; + var newSushi = Sushi.Create(model); + if (newSushi == null) + { + return null; + } + source.Sushis.Add(newSushi); + source.SaveSushis(); + return newSushi.GetViewModel; + } + + public SushiViewModel? Update(SushiBindingModel model) + { + var sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (sushi == null) + { + return null; + } + sushi.Update(model); + source.SaveSushis(); + return sushi.GetViewModel; + } + + public SushiViewModel? Delete(SushiBindingModel model) + { + var element = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Sushis.Remove(element); + source.SaveSushis(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarFileImplement/Models/Component.cs b/SushiBar/SushiBarFileImplement/Models/Component.cs new file mode 100644 index 0000000..8c4ce84 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Component.cs @@ -0,0 +1,59 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.Xml.Linq; + +namespace SushiBarFileImplement.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/SushiBar/SushiBarFileImplement/Models/Order.cs b/SushiBar/SushiBarFileImplement/Models/Order.cs new file mode 100644 index 0000000..4195407 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Order.cs @@ -0,0 +1,87 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Enums; +using SushiBarDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SushiBarFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int SushiId { 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; } + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + 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), + SushiId = Convert.ToInt32(element.Element("SushiId")!.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, + SushiId = SushiId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("SushiId", SushiId), + new XElement("Count", Count), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +} diff --git a/SushiBar/SushiBarFileImplement/Models/Sushi.cs b/SushiBar/SushiBarFileImplement/Models/Sushi.cs new file mode 100644 index 0000000..5e98c30 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Sushi.cs @@ -0,0 +1,87 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.Xml.Linq; + +namespace SushiBarFileImplement.Models +{ + public class Sushi : ISushiModel + { + public int Id { get; private set; } + public string SushiName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _sushiComponents = null; + public Dictionary SushiComponents + { + get + { + if (_sushiComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _sushiComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _sushiComponents; + } + } + public static Sushi? Create(SushiBindingModel model) + { + if (model == null) + { + return null; + } + return new Sushi() + { + Id = model.Id, + SushiName = model.SushiName, + Price = model.Price, + Components = model.SushiComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Sushi? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Sushi() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SushiName = element.Element("SushiName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("SushiComponents")!.Elements("SushiComponent").ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(SushiBindingModel model) + { + if (model == null) + { + return; + } + SushiName = model.SushiName; + Price = model.Price; + Components = model.SushiComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _sushiComponents = null; + } + public SushiViewModel GetViewModel => new() + { + Id = Id, + SushiName = SushiName, + Price = Price, + SushiComponents = SushiComponents + }; + public XElement GetXElement => new("Sushi", + new XAttribute("Id", Id), + new XElement("SushiName", SushiName), + new XElement("Price", Price.ToString()), + new XElement("SushiComponents", Components.Select(x => + new XElement("SushiComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} From 95b1ecfb8b2235133bfcf1b851b64491cdc4a456 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 14:27:20 +0400 Subject: [PATCH 2/6] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20?= =?UTF-8?q?=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 49 +++++++++++ .../Implements/ComponentStorage.cs | 76 ++++++++++++++++ .../Implements/OrderStorage.cs | 74 ++++++++++++++++ .../Implements/SushiStorage.cs | 81 +++++++++++++++++ .../SushiBarFileImplement/Models/Component.cs | 59 +++++++++++++ .../SushiBarFileImplement/Models/Order.cs | 87 +++++++++++++++++++ .../SushiBarFileImplement/Models/Sushi.cs | 87 +++++++++++++++++++ 7 files changed, 513 insertions(+) create mode 100644 SushiBar/SushiBarFileImplement/DataFileSingleton.cs create mode 100644 SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs create mode 100644 SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs create mode 100644 SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs create mode 100644 SushiBar/SushiBarFileImplement/Models/Component.cs create mode 100644 SushiBar/SushiBarFileImplement/Models/Order.cs create mode 100644 SushiBar/SushiBarFileImplement/Models/Sushi.cs diff --git a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..1cfada8 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs @@ -0,0 +1,49 @@ +using SushiBarFileImplement.Models; +using System.Linq; +using System.Xml.Linq; + +namespace SushiBarFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string SushiFileName = "Sushi.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Sushis { 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 SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Sushis = LoadData(SushiFileName, "Product", x => Sushi.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/SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs b/SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..b8dbe78 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,76 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.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/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..7d902cb --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,74 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => 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 => + x.GetViewModel).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Orders.FirstOrDefault(x => + (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 newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..8423504 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs @@ -0,0 +1,81 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using SushiBarFileImplement.Models; + +namespace SushiBarFileImplement.Implements +{ + public class SushiStorage : ISushiStorage + { + private readonly DataFileSingleton source; + public SushiStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Sushis.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName)) + { + return new(); + } + return source.Sushis.Where(x => + x.SushiName.Contains(model.SushiName)).Select(x => + x.GetViewModel).ToList(); + } + + public SushiViewModel? GetElement(SushiSearchModel model) + { + if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) + { + return null; + } + return source.Sushis.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SushiName) && + x.SushiName == model.SushiName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public SushiViewModel? Insert(SushiBindingModel model) + { + model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1; + var newSushi = Sushi.Create(model); + if (newSushi == null) + { + return null; + } + source.Sushis.Add(newSushi); + source.SaveSushis(); + return newSushi.GetViewModel; + } + + public SushiViewModel? Update(SushiBindingModel model) + { + var sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (sushi == null) + { + return null; + } + sushi.Update(model); + source.SaveSushis(); + return sushi.GetViewModel; + } + + public SushiViewModel? Delete(SushiBindingModel model) + { + var element = source.Sushis.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Sushis.Remove(element); + source.SaveSushis(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SushiBar/SushiBarFileImplement/Models/Component.cs b/SushiBar/SushiBarFileImplement/Models/Component.cs new file mode 100644 index 0000000..8c4ce84 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Component.cs @@ -0,0 +1,59 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.Xml.Linq; + +namespace SushiBarFileImplement.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/SushiBar/SushiBarFileImplement/Models/Order.cs b/SushiBar/SushiBarFileImplement/Models/Order.cs new file mode 100644 index 0000000..4195407 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Order.cs @@ -0,0 +1,87 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Enums; +using SushiBarDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SushiBarFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int SushiId { 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; } + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SushiId = model.SushiId, + 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), + SushiId = Convert.ToInt32(element.Element("SushiId")!.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, + SushiId = SushiId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("SushiId", SushiId), + new XElement("Count", Count), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString()) + ); + } +} diff --git a/SushiBar/SushiBarFileImplement/Models/Sushi.cs b/SushiBar/SushiBarFileImplement/Models/Sushi.cs new file mode 100644 index 0000000..5e98c30 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Sushi.cs @@ -0,0 +1,87 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; +using System.Xml.Linq; + +namespace SushiBarFileImplement.Models +{ + public class Sushi : ISushiModel + { + public int Id { get; private set; } + public string SushiName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _sushiComponents = null; + public Dictionary SushiComponents + { + get + { + if (_sushiComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _sushiComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _sushiComponents; + } + } + public static Sushi? Create(SushiBindingModel model) + { + if (model == null) + { + return null; + } + return new Sushi() + { + Id = model.Id, + SushiName = model.SushiName, + Price = model.Price, + Components = model.SushiComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Sushi? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Sushi() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + SushiName = element.Element("SushiName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("SushiComponents")!.Elements("SushiComponent").ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(SushiBindingModel model) + { + if (model == null) + { + return; + } + SushiName = model.SushiName; + Price = model.Price; + Components = model.SushiComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _sushiComponents = null; + } + public SushiViewModel GetViewModel => new() + { + Id = Id, + SushiName = SushiName, + Price = Price, + SushiComponents = SushiComponents + }; + public XElement GetXElement => new("Sushi", + new XAttribute("Id", Id), + new XElement("SushiName", SushiName), + new XElement("Price", Price.ToString()), + new XElement("SushiComponents", Components.Select(x => + new XElement("SushiComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} From faf008a65c672317bcf386b3f6edf76c2bd1c812 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 15:20:54 +0400 Subject: [PATCH 3/6] =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBar.sln | 6 ++++++ SushiBar/SushiBar/Program.cs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index 6fd8544..586fbb7 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarDataModels", "Sushi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarListImplement", "SushiBarListImplement\SushiBarListImplement.csproj", "{FE2BCA7B-A6E4-4628-9DCE-2F04F258BE1F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{3BAA21BA-8829-46C4-88B3-52B862716C5D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {FE2BCA7B-A6E4-4628-9DCE-2F04F258BE1F}.Debug|Any CPU.Build.0 = Debug|Any CPU {FE2BCA7B-A6E4-4628-9DCE-2F04F258BE1F}.Release|Any CPU.ActiveCfg = Release|Any CPU {FE2BCA7B-A6E4-4628-9DCE-2F04F258BE1F}.Release|Any CPU.Build.0 = Release|Any CPU + {3BAA21BA-8829-46C4-88B3-52B862716C5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3BAA21BA-8829-46C4-88B3-52B862716C5D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3BAA21BA-8829-46C4-88B3-52B862716C5D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3BAA21BA-8829-46C4-88B3-52B862716C5D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBar/Program.cs b/SushiBar/SushiBar/Program.cs index 0627eda..8148c48 100644 --- a/SushiBar/SushiBar/Program.cs +++ b/SushiBar/SushiBar/Program.cs @@ -5,7 +5,7 @@ using SushiBar.Forms; using SushiBarBusinessLogic.BusinessLogics; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.StoragesContracts; -using SushiBarListImplement.Implements; +using SushiBarFileImplement.Implements; namespace SushiBar { From 91f33d2dca94080a9efd334d5dc5d33522d731da Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 15:37:07 +0400 Subject: [PATCH 4/6] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20DataFileSingleton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBarFileImplement/DataFileSingleton.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs index 1cfada8..7c23c6c 100644 --- a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs +++ b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs @@ -27,8 +27,8 @@ namespace SushiBarFileImplement private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - Sushis = LoadData(SushiFileName, "Product", x => Sushi.Create(x)!)!; - Orders = new List(); + Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { From 6e79e945da15c671c3cc1f41764637277eb01fa0 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 16:23:02 +0400 Subject: [PATCH 5/6] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B0=D0=B3=D0=B0=20=D1=81=20?= =?UTF-8?q?=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=B0=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20SushiName=20?= =?UTF-8?q?=D0=B2=20OrderViewModel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 20 ++++++++++++------- .../SushiBarFileImplement/Models/Order.cs | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs index 7d902cb..2d42369 100644 --- a/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs @@ -15,7 +15,7 @@ namespace SushiBarFileImplement.Implements } public List GetFullList() { - return source.Orders.Select(x => x.GetViewModel).ToList(); + return source.Orders.Select(x => AttachSushiName(x.GetViewModel)).ToList(); } public List GetFilteredList(OrderSearchModel model) { @@ -25,7 +25,7 @@ namespace SushiBarFileImplement.Implements } return source.Orders.Where(x => x.Id == model.Id).Select(x => - x.GetViewModel).ToList(); + AttachSushiName(x.GetViewModel)).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { @@ -33,8 +33,8 @@ namespace SushiBarFileImplement.Implements { return null; } - return source.Orders.FirstOrDefault(x => - (x.Id == model.Id))?.GetViewModel; + return AttachSushiName(source.Orders.FirstOrDefault(x => + (x.Id == model.Id))?.GetViewModel); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -46,7 +46,7 @@ namespace SushiBarFileImplement.Implements } source.Orders.Add(newOrder); source.SaveOrders(); - return newOrder.GetViewModel; + return AttachSushiName(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) { @@ -57,7 +57,7 @@ namespace SushiBarFileImplement.Implements } order.Update(model); source.SaveOrders(); - return order.GetViewModel; + return AttachSushiName(order.GetViewModel); } public OrderViewModel? Delete(OrderBindingModel model) { @@ -66,9 +66,15 @@ namespace SushiBarFileImplement.Implements { source.Orders.Remove(element); source.SaveOrders(); - return element.GetViewModel; + return AttachSushiName(element.GetViewModel); } return null; } + private OrderViewModel AttachSushiName(OrderViewModel model) + { + if (model == null) { return null; } + model.SushiName = source.Sushis.First(x => x.Id == model.SushiId).SushiName; + return model; + } } } diff --git a/SushiBar/SushiBarFileImplement/Models/Order.cs b/SushiBar/SushiBarFileImplement/Models/Order.cs index 4195407..6251638 100644 --- a/SushiBar/SushiBarFileImplement/Models/Order.cs +++ b/SushiBar/SushiBarFileImplement/Models/Order.cs @@ -45,7 +45,7 @@ namespace SushiBarFileImplement.Models } return new Order() { - Id = Convert.ToInt32(element.Attribute("id")!.Value), + Id = Convert.ToInt32(element.Attribute("Id")!.Value), SushiId = Convert.ToInt32(element.Element("SushiId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), From 3a95478fd24bd88c607229fb2d86a69d08871a75 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 28 Feb 2024 08:53:49 +0400 Subject: [PATCH 6/6] =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=B7=D0=BD=D0=B0=D0=BA=20=D0=B2=D0=BE=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs index 2d42369..2f57336 100644 --- a/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs @@ -70,7 +70,7 @@ namespace SushiBarFileImplement.Implements } return null; } - private OrderViewModel AttachSushiName(OrderViewModel model) + private OrderViewModel AttachSushiName(OrderViewModel ?model) { if (model == null) { return null; } model.SushiName = source.Sushis.First(x => x.Id == model.SushiId).SushiName;