From 67096788d944d73d90bd85dd258fb0fc3abfedb1 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 14:27:20 +0400 Subject: [PATCH 01/15] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=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())); + } +} -- 2.25.1 From 95b1ecfb8b2235133bfcf1b851b64491cdc4a456 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 14:27:20 +0400 Subject: [PATCH 02/15] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=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())); + } +} -- 2.25.1 From faf008a65c672317bcf386b3f6edf76c2bd1c812 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 15:20:54 +0400 Subject: [PATCH 03/15] =?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 { -- 2.25.1 From 91f33d2dca94080a9efd334d5dc5d33522d731da Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 15:37:07 +0400 Subject: [PATCH 04/15] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=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) { -- 2.25.1 From 6e79e945da15c671c3cc1f41764637277eb01fa0 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 16:23:02 +0400 Subject: [PATCH 05/15] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B0=D0=B3=D0=B0=20?= =?UTF-8?q?=D1=81=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20SushiN?= =?UTF-8?q?ame=20=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), -- 2.25.1 From 3a95478fd24bd88c607229fb2d86a69d08871a75 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 28 Feb 2024 08:53:49 +0400 Subject: [PATCH 06/15] =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=82=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B7=D0=BD=D0=B0=D0=BA=20=D0=B2=D0=BE=D0=BF?= =?UTF-8?q?=D1=80=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; -- 2.25.1 From 06ad6a92f14414236e9ff61717fbb6cdfaefc3b4 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 14:27:20 +0400 Subject: [PATCH 07/15] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=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())); + } +} -- 2.25.1 From 61be2ee7b71bf3bf4704d931aa66faee62fed409 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 15:20:54 +0400 Subject: [PATCH 08/15] =?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 { -- 2.25.1 From b441884b408676b2d5ee8c1b7fba59b2333869c9 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 15:37:07 +0400 Subject: [PATCH 09/15] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=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) { -- 2.25.1 From f5603aee2e837db0da2844cff42e9b5dc4fcec35 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 14:27:20 +0400 Subject: [PATCH 10/15] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=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 --- 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 7c23c6c..1cfada8 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, "Sushi", x => Sushi.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Sushis = LoadData(SushiFileName, "Product", x => Sushi.Create(x)!)!; + Orders = new List(); } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { -- 2.25.1 From 72aa20a1b3af9b854aaac349f93f394f93625e31 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 27 Feb 2024 16:23:02 +0400 Subject: [PATCH 11/15] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B0=D0=B3=D0=B0=20?= =?UTF-8?q?=D1=81=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20SushiN?= =?UTF-8?q?ame=20=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), -- 2.25.1 From 3c8403a6489190ebab05d4ce75c46580b02e2902 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 28 Feb 2024 08:53:49 +0400 Subject: [PATCH 12/15] =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=82=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B7=D0=BD=D0=B0=D0=BA=20=D0=B2=D0=BE=D0=BF?= =?UTF-8?q?=D1=80=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; -- 2.25.1 From 73577b5e0ec2f8781593a38a4d792f5826e19957 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 28 Feb 2024 09:43:05 +0400 Subject: [PATCH 13/15] =?UTF-8?q?=D0=BA=D0=B0=D0=BA=D0=B8=D0=B5=20=D1=82?= =?UTF-8?q?=D0=BE=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SushiBar/SushiBar.sln | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index 586fbb7..ae91de2 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -13,7 +13,7 @@ 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{4F721F76-EF7A-4FF3-80B0-77A459D091D7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -41,10 +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 + {4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- 2.25.1 From 6d000dde33f4ab9f413f7dcf66965e3da7d8b17c Mon Sep 17 00:00:00 2001 From: bekodeg Date: Sun, 5 May 2024 19:18:49 +0400 Subject: [PATCH 14/15] =?UTF-8?q?=D0=BF=D0=BE=D1=82=D0=BE=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=B8=20=D1=82=D0=BE=D0=B3=D0=BE=20=D0=B6=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SushiBarDatabaseImplement.csproj | 29 +++++++++++++++++++ .../SushiBarFileImplement.csproj | 14 +++++++++ 2 files changed, 43 insertions(+) create mode 100644 SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj create mode 100644 SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj new file mode 100644 index 0000000..2af36de --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabaseImplement.csproj @@ -0,0 +1,29 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj new file mode 100644 index 0000000..00e2c96 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + -- 2.25.1 From d6125832ab16e1664ea648c9cc6e52cf9ad430a7 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Sun, 5 May 2024 20:20:03 +0400 Subject: [PATCH 15/15] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D0=BE=D1=80=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SushiBar/Forms/FormComponent.Designer.cs | 65 ++-- SushiBar/SushiBar/Forms/FormComponent.cs | 80 ++++- SushiBar/SushiBar/Forms/FormComponent.resx | 298 +++++++++--------- SushiBar/SushiBar/Forms/FormComponent.ru.resx | 10 +- SushiBar/SushiBar/Forms/FormComponentLogic.cs | 81 ----- .../SushiBar/Forms/FormComponents.Designer.cs | 94 +++--- SushiBar/SushiBar/Forms/FormComponents.cs | 105 ++++-- .../SushiBar/Forms/FormComponentsLogic.cs | 98 ------ .../Forms/FormCreateOrder.Designer.cs | 102 +++--- SushiBar/SushiBar/Forms/FormCreateOrder.cs | 120 +++++-- .../SushiBar/Forms/FormCreateOrderLogic.cs | 115 ------- SushiBar/SushiBar/Forms/FormMain.Designer.cs | 151 ++++----- SushiBar/SushiBar/Forms/FormMain.cs | 141 +++++++-- SushiBar/SushiBar/Forms/FormMainLogic.cs | 140 -------- SushiBar/SushiBar/Forms/FormSushi.Designer.cs | 162 +++++----- SushiBar/SushiBar/Forms/FormSushi.cs | 212 +++++++++++-- SushiBar/SushiBar/Forms/FormSushi.resx | 9 - .../Forms/FormSushiComponent.Designer.cs | 45 +-- SushiBar/SushiBar/Forms/FormSushiComponent.cs | 83 ++++- .../SushiBar/Forms/FormSushiComponentLogic.cs | 78 ----- SushiBar/SushiBar/Forms/FormSushiLogic.cs | 199 ------------ .../SushiBar/Forms/FormSushis.Designer.cs | 100 +++--- SushiBar/SushiBar/Forms/FormSushis.cs | 106 +++++-- SushiBar/SushiBar/Forms/FormSushisLogic.cs | 99 ------ 24 files changed, 1248 insertions(+), 1445 deletions(-) delete mode 100644 SushiBar/SushiBar/Forms/FormComponentLogic.cs delete mode 100644 SushiBar/SushiBar/Forms/FormComponentsLogic.cs delete mode 100644 SushiBar/SushiBar/Forms/FormCreateOrderLogic.cs delete mode 100644 SushiBar/SushiBar/Forms/FormMainLogic.cs delete mode 100644 SushiBar/SushiBar/Forms/FormSushiComponentLogic.cs delete mode 100644 SushiBar/SushiBar/Forms/FormSushiLogic.cs delete mode 100644 SushiBar/SushiBar/Forms/FormSushisLogic.cs diff --git a/SushiBar/SushiBar/Forms/FormComponent.Designer.cs b/SushiBar/SushiBar/Forms/FormComponent.Designer.cs index ad848b9..762ad23 100644 --- a/SushiBar/SushiBar/Forms/FormComponent.Designer.cs +++ b/SushiBar/SushiBar/Forms/FormComponent.Designer.cs @@ -28,55 +28,75 @@ /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormComponent)); label1 = new Label(); textBoxName = new TextBox(); textBoxCost = new TextBox(); label2 = new Label(); - buttonSave = new Button(); - buttonCancel = new Button(); + ButtonSave = new Button(); + ButtonCancel = new Button(); SuspendLayout(); // // label1 // - resources.ApplyResources(label1, "label1"); + label1.AutoSize = true; + label1.Location = new Point(12, 9); label1.Name = "label1"; + label1.Size = new Size(77, 20); + label1.TabIndex = 0; + label1.Text = "Название"; // // textBoxName // - resources.ApplyResources(textBoxName, "textBoxName"); + textBoxName.Location = new Point(95, 6); textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(246, 27); + textBoxName.TabIndex = 1; // // textBoxCost // - resources.ApplyResources(textBoxCost, "textBoxCost"); + textBoxCost.Location = new Point(95, 39); textBoxCost.Name = "textBoxCost"; + textBoxCost.Size = new Size(246, 27); + textBoxCost.TabIndex = 3; // // label2 // - resources.ApplyResources(label2, "label2"); + label2.AutoSize = true; + label2.ImeMode = ImeMode.NoControl; + label2.Location = new Point(12, 42); label2.Name = "label2"; + label2.Size = new Size(45, 20); + label2.TabIndex = 2; + label2.Text = "Цена"; // - // buttonSave + // ButtonSave // - resources.ApplyResources(buttonSave, "buttonSave"); - buttonSave.Name = "buttonSave"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; + ButtonSave.Location = new Point(95, 85); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(121, 29); + ButtonSave.TabIndex = 4; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; // - // buttonCancel + // ButtonCancel // - resources.ApplyResources(buttonCancel, "buttonCancel"); - buttonCancel.Name = "buttonCancel"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += buttonCancel_Click; + ButtonCancel.ImeMode = ImeMode.NoControl; + ButtonCancel.Location = new Point(222, 85); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(119, 29); + ButtonCancel.TabIndex = 5; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; // // FormComponent // - resources.ApplyResources(this, "$this"); + AllowDrop = true; AutoScaleMode = AutoScaleMode.None; - Controls.Add(buttonCancel); - Controls.Add(buttonSave); + ClientSize = new Size(353, 126); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); Controls.Add(textBoxCost); Controls.Add(label2); Controls.Add(textBoxName); @@ -84,6 +104,7 @@ MaximizeBox = false; Name = "FormComponent"; ShowInTaskbar = false; + Text = "Компонент"; TopMost = true; Load += FormComponent_Load; ResumeLayout(false); @@ -96,7 +117,7 @@ private TextBox textBoxName; private TextBox textBoxCost; private Label label2; - private Button buttonSave; - private Button buttonCancel; + private Button ButtonSave; + private Button ButtonCancel; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormComponent.cs b/SushiBar/SushiBar/Forms/FormComponent.cs index 1c22dbe..7225978 100644 --- a/SushiBar/SushiBar/Forms/FormComponent.cs +++ b/SushiBar/SushiBar/Forms/FormComponent.cs @@ -1,17 +1,81 @@ -using System; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using Microsoft.Extensions.Logging; + namespace SushiBar.Forms { - public partial class FormComponent + public partial class FormComponent : Form { - private void buttonSave_Click(object sender, EventArgs e) + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public FormComponent(ILogger logger, IComponentLogic logic) { - ButtonSave_Click(sender, e); + InitializeComponent(); + _logger = logger; + _logic = logic; } - - private void buttonCancel_Click(object sender, EventArgs e) + private void FormComponent_Load(object sender, EventArgs e) { - ButtonCancel_Click(sender, e); - + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение компонента"); + var view = _logic.ReadElement(new ComponentSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.ComponentName; + textBoxCost.Text = view.Cost.ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение компонента"); + try + { + var model = new ComponentBindingModel + { + Id = _id ?? 0, + ComponentName = textBoxName.Text, + Cost = Convert.ToDouble(textBoxCost.Text) + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); } } } diff --git a/SushiBar/SushiBar/Forms/FormComponent.resx b/SushiBar/SushiBar/Forms/FormComponent.resx index 4cf9735..7321b10 100644 --- a/SushiBar/SushiBar/Forms/FormComponent.resx +++ b/SushiBar/SushiBar/Forms/FormComponent.resx @@ -117,178 +117,172 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - buttonSave - - - System.Windows.Forms.TextBox, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - System.Windows.Forms.Label, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - $this - - - System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - - 0 - - - - 222, 85 - - - 95, 39 - - - buttonCancel - - - 8, 20 - - - label1 - - - System.Windows.Forms.Label, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Windows.Forms.TextBox, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - FormComponent - - - System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 2 + + 4 - - System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ButtonSave - - 800, 450 - - - 5 + + $this 2 - - 222, 85 + + + 12, 9 + + + 5 $this - - 0 + + 2 - - 68, 6 - - - FormComponent - - - 4 - - - 1 - - - 5 - - + $this - - 12, 9 - - - 3 - - - $this - - - 5 - - - textBox1 - - - 1 - - - 1 - - - 95, 85 - - - $this - - - textBoxCost - - - 3 - - - 1 - - - 4 - - - $this - - - button1 - - - button2 - - - 0 - - - 95, 85 - - - $this - - - System.Windows.Forms.Form, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 12, 42 - System.Windows.Forms.TextBox, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 1 + + + $this + + + 222, 85 + + + textBoxCost + + + System.Windows.Forms.Label, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + $this + + + 5 + + + 1 + + + System.Windows.Forms.TextBox, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 800, 450 + + + 222, 85 + + + 95, 85 + + + 5 + + + 1 + + + 0 + + + FormComponent + + + 0 + + + 12, 42 + + + 4 + + + 95, 39 + + + Button1 + + + 0 + + + $this + + + $this + + + $this + + + label1 + textBoxName + + 1 + + + System.Windows.Forms.TextBox, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 3 + + + 8, 20 + + + 4 + + + textBox1 + + + System.Windows.Forms.Form, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Button2 + label2 - - 4 + + System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 68, 6 + + + 3 + + + System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + FormComponent + + + System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 95, 85 95, 6 @@ -296,10 +290,10 @@ 0 - - ru - - - True - + + ButtonCancel + + + System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormComponent.ru.resx b/SushiBar/SushiBar/Forms/FormComponent.ru.resx index 14672e4..201be59 100644 --- a/SushiBar/SushiBar/Forms/FormComponent.ru.resx +++ b/SushiBar/SushiBar/Forms/FormComponent.ru.resx @@ -147,19 +147,19 @@ Цена - + 121, 29 - + Сохранить - + NoControl - + 119, 29 - + Отмена diff --git a/SushiBar/SushiBar/Forms/FormComponentLogic.cs b/SushiBar/SushiBar/Forms/FormComponentLogic.cs deleted file mode 100644 index 7225978..0000000 --- a/SushiBar/SushiBar/Forms/FormComponentLogic.cs +++ /dev/null @@ -1,81 +0,0 @@ -using SushiBarContracts.BindingModels; -using SushiBarContracts.BusinessLogicsContracts; -using SushiBarContracts.SearchModels; -using Microsoft.Extensions.Logging; - -namespace SushiBar.Forms -{ - public partial class FormComponent : Form - { - private readonly ILogger _logger; - private readonly IComponentLogic _logic; - private int? _id; - public int Id { set { _id = value; } } - public FormComponent(ILogger logger, IComponentLogic logic) - { - InitializeComponent(); - _logger = logger; - _logic = logic; - } - private void FormComponent_Load(object sender, EventArgs e) - { - if (_id.HasValue) - { - try - { - _logger.LogInformation("Получение компонента"); - var view = _logic.ReadElement(new ComponentSearchModel - { - Id = _id.Value - }); - if (view != null) - { - textBoxName.Text = view.ComponentName; - textBoxCost.Text = view.Cost.ToString(); - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка получения компонента"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void ButtonSave_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxName.Text)) - { - MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - _logger.LogInformation("Сохранение компонента"); - try - { - var model = new ComponentBindingModel - { - Id = _id ?? 0, - ComponentName = textBoxName.Text, - Cost = Convert.ToDouble(textBoxCost.Text) - }; - var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); - DialogResult = DialogResult.OK; - Close(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка сохранения компонента"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void ButtonCancel_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Cancel; - Close(); - } - } -} diff --git a/SushiBar/SushiBar/Forms/FormComponents.Designer.cs b/SushiBar/SushiBar/Forms/FormComponents.Designer.cs index b68809c..5788164 100644 --- a/SushiBar/SushiBar/Forms/FormComponents.Designer.cs +++ b/SushiBar/SushiBar/Forms/FormComponents.Designer.cs @@ -29,10 +29,10 @@ private void InitializeComponent() { dataGridView = new DataGridView(); - buttonAdd = new Button(); - buttonUpd = new Button(); - buttonDel = new Button(); - buttonRef = new Button(); + ButtonAdd = new Button(); + ButtonUpd = new Button(); + ButtonDel = new Button(); + ButtonRef = new Button(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // @@ -48,58 +48,64 @@ dataGridView.Size = new Size(694, 408); dataGridView.TabIndex = 0; // - // buttonAdd + // ButtonAdd // - buttonAdd.Location = new Point(700, 12); - buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(180, 29); - buttonAdd.TabIndex = 1; - buttonAdd.Text = "Добавить"; - buttonAdd.UseVisualStyleBackColor = true; - buttonAdd.Click += buttonAdd_Click; + ButtonAdd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonAdd.Location = new Point(700, 12); + ButtonAdd.Name = "ButtonAdd"; + ButtonAdd.Size = new Size(180, 29); + ButtonAdd.TabIndex = 1; + ButtonAdd.Text = "Добавить"; + ButtonAdd.UseVisualStyleBackColor = true; + ButtonAdd.Click += ButtonAdd_Click; // - // buttonUpd + // ButtonUpd // - buttonUpd.Location = new Point(700, 47); - buttonUpd.Name = "buttonUpd"; - buttonUpd.Size = new Size(180, 29); - buttonUpd.TabIndex = 2; - buttonUpd.Text = "Изменить"; - buttonUpd.UseVisualStyleBackColor = true; - buttonUpd.Click += buttonUpd_Click; + ButtonUpd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonUpd.Location = new Point(700, 47); + ButtonUpd.Name = "ButtonUpd"; + ButtonUpd.Size = new Size(180, 29); + ButtonUpd.TabIndex = 2; + ButtonUpd.Text = "Изменить"; + ButtonUpd.UseVisualStyleBackColor = true; + ButtonUpd.Click += ButtonUpd_Click; // - // buttonDel + // ButtonDel // - buttonDel.Location = new Point(700, 82); - buttonDel.Name = "buttonDel"; - buttonDel.Size = new Size(180, 29); - buttonDel.TabIndex = 3; - buttonDel.Text = "Удалить"; - buttonDel.UseVisualStyleBackColor = true; - buttonDel.Click += buttonDel_Click; + ButtonDel.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonDel.Location = new Point(700, 82); + ButtonDel.Name = "ButtonDel"; + ButtonDel.Size = new Size(180, 29); + ButtonDel.TabIndex = 3; + ButtonDel.Text = "Удалить"; + ButtonDel.UseVisualStyleBackColor = true; + ButtonDel.Click += ButtonDel_Click; // - // buttonRef + // ButtonRef // - buttonRef.Location = new Point(700, 117); - buttonRef.Name = "buttonRef"; - buttonRef.Size = new Size(180, 29); - buttonRef.TabIndex = 4; - buttonRef.Text = "Обновить"; - buttonRef.UseVisualStyleBackColor = true; - buttonRef.Click += buttonRef_Click; + ButtonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonRef.Location = new Point(700, 117); + ButtonRef.Name = "ButtonRef"; + ButtonRef.Size = new Size(180, 29); + ButtonRef.TabIndex = 4; + ButtonRef.Text = "Обновить"; + ButtonRef.UseVisualStyleBackColor = true; + ButtonRef.Click += ButtonRef_Click; // // FormComponents // + AllowDrop = true; AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(892, 408); - Controls.Add(buttonRef); - Controls.Add(buttonDel); - Controls.Add(buttonUpd); - Controls.Add(buttonAdd); + Controls.Add(ButtonRef); + Controls.Add(ButtonDel); + Controls.Add(ButtonUpd); + Controls.Add(ButtonAdd); Controls.Add(dataGridView); Name = "FormComponents"; Text = "Компоненты"; + WindowState = FormWindowState.Maximized; Load += FormComponents_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); @@ -108,9 +114,9 @@ #endregion private DataGridView dataGridView; - private Button buttonAdd; - private Button buttonUpd; - private Button buttonDel; - private Button buttonRef; + private Button ButtonAdd; + private Button ButtonUpd; + private Button ButtonDel; + private Button ButtonRef; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormComponents.cs b/SushiBar/SushiBar/Forms/FormComponents.cs index a2b5925..7943174 100644 --- a/SushiBar/SushiBar/Forms/FormComponents.cs +++ b/SushiBar/SushiBar/Forms/FormComponents.cs @@ -1,35 +1,98 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; namespace SushiBar.Forms { - public partial class FormComponents + public partial class FormComponents : Form { - private void buttonAdd_Click(object sender, EventArgs e) + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + public FormComponents(ILogger logger, IComponentLogic logic) { - ButtonAdd_Click(sender, e); + InitializeComponent(); + _logger = logger; + _logic = logic; } - - private void buttonUpd_Click(object sender, EventArgs e) + private void FormComponents_Load(object sender, EventArgs e) { - ButtonUpd_Click(sender, e); + LoadData(); } - - private void buttonDel_Click(object sender, EventArgs e) + private void LoadData() { - ButtonDel_Click(sender, e); + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка компонентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } - - private void buttonRef_Click(object sender, EventArgs e) + private void ButtonAdd_Click(object sender, EventArgs e) { - ButtonRef_Click(sender, e); + var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление компонента"); + try + { + if (!_logic.Delete(new ComponentBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); } } } diff --git a/SushiBar/SushiBar/Forms/FormComponentsLogic.cs b/SushiBar/SushiBar/Forms/FormComponentsLogic.cs deleted file mode 100644 index 7943174..0000000 --- a/SushiBar/SushiBar/Forms/FormComponentsLogic.cs +++ /dev/null @@ -1,98 +0,0 @@ -using SushiBarContracts.BindingModels; -using SushiBarContracts.BusinessLogicsContracts; -using Microsoft.Extensions.Logging; - -namespace SushiBar.Forms -{ - public partial class FormComponents : Form - { - private readonly ILogger _logger; - private readonly IComponentLogic _logic; - public FormComponents(ILogger logger, IComponentLogic logic) - { - InitializeComponent(); - _logger = logger; - _logic = logic; - } - private void FormComponents_Load(object sender, EventArgs e) - { - LoadData(); - } - private void LoadData() - { - try - { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка компонентов"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки компонентов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void ButtonAdd_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); - if (service is FormComponent form) - { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } - } - } - private void ButtonUpd_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - var service = Program.ServiceProvider?.GetService(typeof(FormComponent)); - if (service is FormComponent form) - { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } - } - } - } - private void ButtonDel_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Удаление компонента"); - try - { - if (!_logic.Delete(new ComponentBindingModel - { - Id = id - })) - { - throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка удаления компонента"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - } - private void ButtonRef_Click(object sender, EventArgs e) - { - LoadData(); - } - } -} diff --git a/SushiBar/SushiBar/Forms/FormCreateOrder.Designer.cs b/SushiBar/SushiBar/Forms/FormCreateOrder.Designer.cs index 22d1beb..fe3e32e 100644 --- a/SushiBar/SushiBar/Forms/FormCreateOrder.Designer.cs +++ b/SushiBar/SushiBar/Forms/FormCreateOrder.Designer.cs @@ -28,35 +28,35 @@ /// private void InitializeComponent() { - buttonSave = new Button(); - buttonCancel = new Button(); + ButtonSave = new Button(); + ButtonCancel = new Button(); label1 = new Label(); label2 = new Label(); label3 = new Label(); - textBoxCount = new TextBox(); - textBoxSum = new TextBox(); - comboBoxSushi = new ComboBox(); + TextBoxCount = new TextBox(); + TextBoxSum = new TextBox(); + ComboBoxSushi = new ComboBox(); SuspendLayout(); // - // buttonSave + // ButtonSave // - buttonSave.Location = new Point(185, 112); - buttonSave.Name = "buttonSave"; - buttonSave.Size = new Size(100, 30); - buttonSave.TabIndex = 0; - buttonSave.Text = "Сохранить"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; + ButtonSave.Location = new Point(185, 112); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(100, 30); + ButtonSave.TabIndex = 0; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; // - // buttonCancel + // ButtonCancel // - buttonCancel.Location = new Point(291, 112); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(100, 30); - buttonCancel.TabIndex = 1; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += buttonCancel_Click; + ButtonCancel.Location = new Point(291, 112); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(100, 30); + ButtonCancel.TabIndex = 1; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; // // label1 // @@ -85,48 +85,50 @@ label3.TabIndex = 4; label3.Text = "Сумма:"; // - // textBoxCount + // TextBoxCount // - textBoxCount.Location = new Point(119, 46); - textBoxCount.Name = "textBoxCount"; - textBoxCount.Size = new Size(272, 27); - textBoxCount.TabIndex = 5; - textBoxCount.TextChanged += textBoxCount_TextChanged; + TextBoxCount.Location = new Point(119, 46); + TextBoxCount.Name = "TextBoxCount"; + TextBoxCount.Size = new Size(272, 27); + TextBoxCount.TabIndex = 5; + TextBoxCount.TextChanged += TextBoxCount_TextChanged; // - // textBoxSum + // TextBoxSum // - textBoxSum.Location = new Point(119, 79); - textBoxSum.Name = "textBoxSum"; - textBoxSum.ReadOnly = true; - textBoxSum.Size = new Size(272, 27); - textBoxSum.TabIndex = 6; + TextBoxSum.Location = new Point(119, 79); + TextBoxSum.Name = "TextBoxSum"; + TextBoxSum.ReadOnly = true; + TextBoxSum.Size = new Size(272, 27); + TextBoxSum.TabIndex = 6; // - // comboBoxSushi + // ComboBoxSushi // - comboBoxSushi.FormattingEnabled = true; - comboBoxSushi.Location = new Point(119, 12); - comboBoxSushi.Name = "comboBoxSushi"; - comboBoxSushi.Size = new Size(272, 28); - comboBoxSushi.TabIndex = 7; - comboBoxSushi.SelectedIndexChanged += comboBoxSushi_SelectedIndexChanged; + ComboBoxSushi.FormattingEnabled = true; + ComboBoxSushi.Location = new Point(119, 12); + ComboBoxSushi.Name = "ComboBoxSushi"; + ComboBoxSushi.Size = new Size(272, 28); + ComboBoxSushi.TabIndex = 7; + ComboBoxSushi.SelectedIndexChanged += ComboBoxSushi_SelectedIndexChanged; // // FormCreateOrder // + AllowDrop = true; AutoScaleMode = AutoScaleMode.None; ClientSize = new Size(403, 152); - Controls.Add(comboBoxSushi); - Controls.Add(textBoxSum); - Controls.Add(textBoxCount); + Controls.Add(ComboBoxSushi); + Controls.Add(TextBoxSum); + Controls.Add(TextBoxCount); Controls.Add(label3); Controls.Add(label2); Controls.Add(label1); - Controls.Add(buttonCancel); - Controls.Add(buttonSave); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); MaximizeBox = false; MinimizeBox = false; Name = "FormCreateOrder"; ShowInTaskbar = false; Text = "Заказ"; + TopMost = true; Load += FormCreateOrder_Load; ResumeLayout(false); PerformLayout(); @@ -134,13 +136,13 @@ #endregion - private Button buttonSave; - private Button buttonCancel; + private Button ButtonSave; + private Button ButtonCancel; private Label label1; private Label label2; private Label label3; - private TextBox textBoxCount; - private TextBox textBoxSum; - private ComboBox comboBoxSushi; + private TextBox TextBoxCount; + private TextBox TextBoxSum; + private ComboBox ComboBoxSushi; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormCreateOrder.cs b/SushiBar/SushiBar/Forms/FormCreateOrder.cs index 216ce4f..b447051 100644 --- a/SushiBar/SushiBar/Forms/FormCreateOrder.cs +++ b/SushiBar/SushiBar/Forms/FormCreateOrder.cs @@ -1,35 +1,113 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarContracts.ViewModels; namespace SushiBar.Forms { - public partial class FormCreateOrder + public partial class FormCreateOrder : Form { - private void buttonSave_Click(object sender, EventArgs e) + private readonly ILogger _logger; + private readonly ISushiLogic _logicS; + private readonly IOrderLogic _logicO; + public FormCreateOrder(ILogger logger, ISushiLogic logicS, IOrderLogic logicO) { - ButtonSave_Click(sender, e); + InitializeComponent(); + _logger = logger; + _logicS = logicS; + _logicO = logicO; } - - private void buttonCancel_Click(object sender, EventArgs e) + private void FormCreateOrder_Load(object sender, EventArgs e) { - ButtonCancel_Click(sender, e); + try + { + _logger.LogInformation("Загрузка суши для заказа"); + List? _list = _logicS.ReadList(null); + if (_list != null) + { + ComboBoxSushi.DisplayMember = "SushiName"; + ComboBoxSushi.ValueMember = "Id"; + ComboBoxSushi.DataSource = _list; + ComboBoxSushi.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки суши"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } - - private void textBoxCount_TextChanged(object sender, EventArgs e) + private void CalcSum() { - TextBoxCount_TextChanged(sender, e); + if (ComboBoxSushi.SelectedValue != null && !string.IsNullOrEmpty(TextBoxCount.Text)) + { + try + { + int id = Convert.ToInt32(ComboBoxSushi.SelectedValue); + var sushi = _logicS.ReadElement(new SushiSearchModel + { + Id = id + }); + int count = Convert.ToInt32(TextBoxCount.Text); + TextBoxSum.Text = Math.Round(count * (sushi?.Price ?? 0), 2).ToString(); + _logger.LogInformation("Расчет суммы заказа"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка расчета суммы заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } } - - private void comboBoxSushi_SelectedIndexChanged(object sender, EventArgs e) + private void TextBoxCount_TextChanged(object sender, EventArgs e) { - ComboBoxSushi_SelectedIndexChanged(sender, e); + CalcSum(); + } + private void ComboBoxSushi_SelectedIndexChanged(object sender, EventArgs e) + { + CalcSum(); + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(TextBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (ComboBoxSushi.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + SushiId = Convert.ToInt32(ComboBoxSushi.SelectedValue), + Count = Convert.ToInt32(TextBoxCount.Text), + Sum = Convert.ToDouble(TextBoxSum.Text) + }); + if (!operationResult) + { + throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); } } } diff --git a/SushiBar/SushiBar/Forms/FormCreateOrderLogic.cs b/SushiBar/SushiBar/Forms/FormCreateOrderLogic.cs deleted file mode 100644 index 4801e2e..0000000 --- a/SushiBar/SushiBar/Forms/FormCreateOrderLogic.cs +++ /dev/null @@ -1,115 +0,0 @@ -using Microsoft.Extensions.Logging; -using Microsoft.VisualBasic.Logging; -using SushiBarContracts.BindingModels; -using SushiBarContracts.BusinessLogicsContracts; -using SushiBarContracts.SearchModels; -using SushiBarContracts.ViewModels; -using System.Windows.Forms; - -namespace SushiBar.Forms -{ - public partial class FormCreateOrder : Form - { - private readonly ILogger _logger; - private readonly ISushiLogic _logicS; - private readonly IOrderLogic _logicO; - public FormCreateOrder(ILogger logger, ISushiLogic logicS, IOrderLogic logicO) - { - InitializeComponent(); - _logger = logger; - _logicS = logicS; - _logicO = logicO; - } - private void FormCreateOrder_Load(object sender, EventArgs e) - { - try - { - _logger.LogInformation("Загрузка суши для заказа"); - List? _list = _logicS.ReadList(null); - if (_list != null) - { - comboBoxSushi.DisplayMember = "SushiName"; - comboBoxSushi.ValueMember = "Id"; - comboBoxSushi.DataSource = _list; - comboBoxSushi.SelectedItem = null; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки суши"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void CalcSum() - { - if (comboBoxSushi.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text)) - { - try - { - int id = Convert.ToInt32(comboBoxSushi.SelectedValue); - var sushi = _logicS.ReadElement(new SushiSearchModel - { - Id = id - }); - int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (sushi?.Price ?? 0), 2).ToString(); - _logger.LogInformation("Расчет суммы заказа"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка расчета суммы заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } - } - private void TextBoxCount_TextChanged(object sender, EventArgs e) - { - CalcSum(); - } - private void ComboBoxSushi_SelectedIndexChanged(object sender, EventArgs e) - { - CalcSum(); - } - private void ButtonSave_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxCount.Text)) - { - MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (comboBoxSushi.SelectedValue == null) - { - MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - _logger.LogInformation("Создание заказа"); - try - { - var operationResult = _logicO.CreateOrder(new OrderBindingModel - { - SushiId = Convert.ToInt32(comboBoxSushi.SelectedValue), - Count = Convert.ToInt32(textBoxCount.Text), - Sum = Convert.ToDouble(textBoxSum.Text) - }); - if (!operationResult) - { - throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах."); - } - MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); - DialogResult = DialogResult.OK; - Close(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка создания заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void ButtonCancel_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Cancel; - Close(); - } - } -} diff --git a/SushiBar/SushiBar/Forms/FormMain.Designer.cs b/SushiBar/SushiBar/Forms/FormMain.Designer.cs index e2eb16f..4c70edd 100644 --- a/SushiBar/SushiBar/Forms/FormMain.Designer.cs +++ b/SushiBar/SushiBar/Forms/FormMain.Designer.cs @@ -31,27 +31,27 @@ dataGridView = new DataGridView(); menuStrip1 = new MenuStrip(); ToolStripMenuItem = new ToolStripMenuItem(); - sushiToolStripMenuItem = new ToolStripMenuItem(); - componentsToolStripMenuItem = new ToolStripMenuItem(); - buttonCreateOrder = new Button(); - buttonTakeOrderInWork = new Button(); - buttonOrderReady = new Button(); - buttonIssuedOrder = new Button(); - buttonRef = new Button(); + SushiToolStripMenuItem = new ToolStripMenuItem(); + ComponentsToolStripMenuItem = new ToolStripMenuItem(); + ButtonCreateOrder = new Button(); + ButtonTakeOrderInWork = new Button(); + ButtonOrderReady = new Button(); + ButtonIssuedOrder = new Button(); + ButtonRef = new Button(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); menuStrip1.SuspendLayout(); SuspendLayout(); // // dataGridView // + dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Dock = DockStyle.Left; dataGridView.Location = new Point(0, 28); dataGridView.Name = "dataGridView"; dataGridView.RowHeadersWidth = 51; dataGridView.RowTemplate.Height = 29; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView.Size = new Size(980, 422); + dataGridView.Size = new Size(992, 422); dataGridView.TabIndex = 0; // // menuStrip1 @@ -66,90 +66,97 @@ // // ToolStripMenuItem // - ToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { sushiToolStripMenuItem, componentsToolStripMenuItem }); + ToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { SushiToolStripMenuItem, ComponentsToolStripMenuItem }); ToolStripMenuItem.Name = "ToolStripMenuItem"; ToolStripMenuItem.Size = new Size(117, 24); ToolStripMenuItem.Text = "Справочники"; // - // sushiToolStripMenuItem + // SushiToolStripMenuItem // - sushiToolStripMenuItem.Name = "sushiToolStripMenuItem"; - sushiToolStripMenuItem.Size = new Size(182, 26); - sushiToolStripMenuItem.Text = "Суши"; - sushiToolStripMenuItem.Click += sushiToolStripMenuItem_Click; + SushiToolStripMenuItem.Name = "SushiToolStripMenuItem"; + SushiToolStripMenuItem.Size = new Size(182, 26); + SushiToolStripMenuItem.Text = "Суши"; + SushiToolStripMenuItem.Click += SushiToolStripMenuItem_Click; // - // componentsToolStripMenuItem + // ComponentsToolStripMenuItem // - componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; - componentsToolStripMenuItem.Size = new Size(182, 26); - componentsToolStripMenuItem.Text = "Компоненты"; - componentsToolStripMenuItem.Click += componentsToolStripMenuItem_Click; + ComponentsToolStripMenuItem.Name = "ComponentsToolStripMenuItem"; + ComponentsToolStripMenuItem.Size = new Size(182, 26); + ComponentsToolStripMenuItem.Text = "Компоненты"; + ComponentsToolStripMenuItem.Click += ComponentsToolStripMenuItem_Click; // - // buttonCreateOrder + // ButtonCreateOrder // - buttonCreateOrder.Location = new Point(998, 50); - buttonCreateOrder.Name = "buttonCreateOrder"; - buttonCreateOrder.Size = new Size(180, 30); - buttonCreateOrder.TabIndex = 2; - buttonCreateOrder.Text = "Создать заказ"; - buttonCreateOrder.UseVisualStyleBackColor = true; - buttonCreateOrder.Click += buttonCreateOrder_Click; + ButtonCreateOrder.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonCreateOrder.Location = new Point(998, 31); + ButtonCreateOrder.Name = "ButtonCreateOrder"; + ButtonCreateOrder.Size = new Size(180, 30); + ButtonCreateOrder.TabIndex = 2; + ButtonCreateOrder.Text = "Создать заказ"; + ButtonCreateOrder.UseVisualStyleBackColor = true; + ButtonCreateOrder.Click += ButtonCreateOrder_Click; // - // buttonTakeOrderInWork + // ButtonTakeOrderInWork // - buttonTakeOrderInWork.Location = new Point(998, 86); - buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; - buttonTakeOrderInWork.Size = new Size(180, 30); - buttonTakeOrderInWork.TabIndex = 3; - buttonTakeOrderInWork.Text = "Отдать на выполнение"; - buttonTakeOrderInWork.UseVisualStyleBackColor = true; - buttonTakeOrderInWork.Click += buttonTakeOrderInWork_Click; + ButtonTakeOrderInWork.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonTakeOrderInWork.Location = new Point(998, 67); + ButtonTakeOrderInWork.Name = "ButtonTakeOrderInWork"; + ButtonTakeOrderInWork.Size = new Size(180, 30); + ButtonTakeOrderInWork.TabIndex = 3; + ButtonTakeOrderInWork.Text = "Отдать на выполнение"; + ButtonTakeOrderInWork.UseVisualStyleBackColor = true; + ButtonTakeOrderInWork.Click += ButtonTakeOrderInWork_Click; // - // buttonOrderReady + // ButtonOrderReady // - buttonOrderReady.Location = new Point(998, 122); - buttonOrderReady.Name = "buttonOrderReady"; - buttonOrderReady.Size = new Size(180, 30); - buttonOrderReady.TabIndex = 4; - buttonOrderReady.Text = "Заказ готов"; - buttonOrderReady.UseVisualStyleBackColor = true; - buttonOrderReady.Click += buttonOrderReady_Click; + ButtonOrderReady.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonOrderReady.Location = new Point(998, 103); + ButtonOrderReady.Name = "ButtonOrderReady"; + ButtonOrderReady.Size = new Size(180, 30); + ButtonOrderReady.TabIndex = 4; + ButtonOrderReady.Text = "Заказ готов"; + ButtonOrderReady.UseVisualStyleBackColor = true; + ButtonOrderReady.Click += ButtonOrderReady_Click; // - // buttonIssuedOrder + // ButtonIssuedOrder // - buttonIssuedOrder.Location = new Point(998, 158); - buttonIssuedOrder.Name = "buttonIssuedOrder"; - buttonIssuedOrder.Size = new Size(180, 30); - buttonIssuedOrder.TabIndex = 5; - buttonIssuedOrder.Text = "Заказ Выдан"; - buttonIssuedOrder.UseVisualStyleBackColor = true; - buttonIssuedOrder.Click += buttonIssuedOrder_Click; + ButtonIssuedOrder.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonIssuedOrder.Location = new Point(998, 139); + ButtonIssuedOrder.Name = "ButtonIssuedOrder"; + ButtonIssuedOrder.Size = new Size(180, 30); + ButtonIssuedOrder.TabIndex = 5; + ButtonIssuedOrder.Text = "Заказ Выдан"; + ButtonIssuedOrder.UseVisualStyleBackColor = true; + ButtonIssuedOrder.Click += ButtonIssuedOrder_Click; // - // buttonRef + // ButtonRef // - buttonRef.Location = new Point(998, 194); - buttonRef.Name = "buttonRef"; - buttonRef.Size = new Size(180, 30); - buttonRef.TabIndex = 6; - buttonRef.Text = "Обновить список"; - buttonRef.UseVisualStyleBackColor = true; - buttonRef.Click += buttonRef_Click; + ButtonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonRef.Location = new Point(998, 175); + ButtonRef.Name = "ButtonRef"; + ButtonRef.Size = new Size(180, 30); + ButtonRef.TabIndex = 6; + ButtonRef.Text = "Обновить список"; + ButtonRef.UseVisualStyleBackColor = true; + ButtonRef.Click += ButtonRef_Click; // // FormMain // + AllowDrop = true; AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1190, 450); - Controls.Add(buttonRef); - Controls.Add(buttonIssuedOrder); - Controls.Add(buttonOrderReady); - Controls.Add(buttonTakeOrderInWork); - Controls.Add(buttonCreateOrder); + Controls.Add(ButtonRef); + Controls.Add(ButtonIssuedOrder); + Controls.Add(ButtonOrderReady); + Controls.Add(ButtonTakeOrderInWork); + Controls.Add(ButtonCreateOrder); Controls.Add(dataGridView); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; Name = "FormMain"; Text = "Сушибар"; + WindowState = FormWindowState.Maximized; Load += FormMain_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); menuStrip1.ResumeLayout(false); @@ -162,13 +169,13 @@ private DataGridView dataGridView; private MenuStrip menuStrip1; - private Button buttonCreateOrder; - private Button buttonTakeOrderInWork; - private Button buttonOrderReady; - private Button buttonIssuedOrder; - private Button buttonRef; + private Button ButtonCreateOrder; + private Button ButtonTakeOrderInWork; + private Button ButtonOrderReady; + private Button ButtonIssuedOrder; + private Button ButtonRef; private ToolStripMenuItem ToolStripMenuItem; - private ToolStripMenuItem sushiToolStripMenuItem; - private ToolStripMenuItem componentsToolStripMenuItem; + private ToolStripMenuItem SushiToolStripMenuItem; + private ToolStripMenuItem ComponentsToolStripMenuItem; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormMain.cs b/SushiBar/SushiBar/Forms/FormMain.cs index f9a5708..c7e1a22 100644 --- a/SushiBar/SushiBar/Forms/FormMain.cs +++ b/SushiBar/SushiBar/Forms/FormMain.cs @@ -1,40 +1,137 @@ -namespace SushiBar.Forms +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; + +namespace SushiBar.Forms { - public partial class FormMain + public partial class FormMain : Form { - private void buttonCreateOrder_Click(object sender, EventArgs e) + private readonly ILogger _logger; + private readonly IOrderLogic _orderLogic; + public FormMain(ILogger logger, IOrderLogic orderLogic) { - ButtonCreateOrder_Click(sender, e); + InitializeComponent(); + _logger = logger; + _orderLogic = orderLogic; } - - private void buttonTakeOrderInWork_Click(object sender, EventArgs e) + private void FormMain_Load(object sender, EventArgs e) { - ButtonTakeOrderInWork_Click(sender, e); + LoadData(); } - - private void buttonOrderReady_Click(object sender, EventArgs e) + private void LoadData() { - ButtonOrderReady_Click(sender, e); + try + { + var list = _orderLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["SushiId"].Visible = false; + dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка заказов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки суши"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } - - private void buttonIssuedOrder_Click(object sender, EventArgs e) + private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e) { - ButtonIssuedOrder_Click(sender, e); + var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); + if (service is FormComponents form) + { + form.ShowDialog(); + } } - - private void buttonRef_Click(object sender, EventArgs e) + private void SushiToolStripMenuItem_Click(object sender, EventArgs e) { - ButtonRef_Click(sender, e); + var service = Program.ServiceProvider?.GetService(typeof(FormSushis)); + if (service is FormSushis form) + { + form.ShowDialog(); + } } - - private void sushiToolStripMenuItem_Click(object sender, EventArgs e) + private void ButtonCreateOrder_Click(object sender, EventArgs e) { - SushiToolStripMenuItem_Click(sender, e); + var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); + if (service is FormCreateOrder form) + { + form.ShowDialog(); + LoadData(); + } } - - private void componentsToolStripMenuItem_Click(object sender, EventArgs e) + private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) { - ComponentsToolStripMenuItem_Click(sender, e); + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id); + try + { + var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка передачи заказа в работу"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void ButtonOrderReady_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); + try + { + var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void ButtonIssuedOrder_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id); + try + { + var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + _logger.LogInformation("Заказ №{id} выдан", id); + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о выдачи заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); } } } diff --git a/SushiBar/SushiBar/Forms/FormMainLogic.cs b/SushiBar/SushiBar/Forms/FormMainLogic.cs deleted file mode 100644 index 7d96481..0000000 --- a/SushiBar/SushiBar/Forms/FormMainLogic.cs +++ /dev/null @@ -1,140 +0,0 @@ -using Microsoft.Extensions.Logging; -using SushiBarBusinessLogic.BusinessLogics; -using SushiBarContracts.BindingModels; -using SushiBarContracts.BusinessLogicsContracts; -using SushiBarDataModels.Enums; -using System.Windows.Forms; - -namespace SushiBar.Forms -{ - public partial class FormMain : Form - { - private readonly ILogger _logger; - private readonly IOrderLogic _orderLogic; - public FormMain(ILogger logger, IOrderLogic orderLogic) - { - InitializeComponent(); - _logger = logger; - _orderLogic = orderLogic; - } - private void FormMain_Load(object sender, EventArgs e) - { - LoadData(); - } - private void LoadData() - { - try - { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["SushiId"].Visible = false; - dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка заказов"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки суши"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); - if (service is FormComponents form) - { - form.ShowDialog(); - } - } - private void SushiToolStripMenuItem_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormSushis)); - if (service is FormSushis form) - { - form.ShowDialog(); - } - } - private void ButtonCreateOrder_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); - if (service is FormCreateOrder form) - { - form.ShowDialog(); - LoadData(); - } - } - private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id); - try - { - var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = id}); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка передачи заказа в работу"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void ButtonOrderReady_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); - try - { - var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка отметки о готовности заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void ButtonIssuedOrder_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id); - try - { - var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - _logger.LogInformation("Заказ №{id} выдан", id); - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка отметки о выдачи заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void ButtonRef_Click(object sender, EventArgs e) - { - LoadData(); - } - } -} diff --git a/SushiBar/SushiBar/Forms/FormSushi.Designer.cs b/SushiBar/SushiBar/Forms/FormSushi.Designer.cs index 83ce316..2a52066 100644 --- a/SushiBar/SushiBar/Forms/FormSushi.Designer.cs +++ b/SushiBar/SushiBar/Forms/FormSushi.Designer.cs @@ -28,17 +28,17 @@ /// private void InitializeComponent() { - buttonAdd = new Button(); + ButtonAdd = new Button(); dataGridView = new DataGridView(); ID = new DataGridViewTextBoxColumn(); ComponentName = new DataGridViewTextBoxColumn(); ComponentCount = new DataGridViewTextBoxColumn(); groupBox1 = new GroupBox(); - buttonRef = new Button(); - buttonDel = new Button(); - buttonUpd = new Button(); - buttonSave = new Button(); - buttonCancel = new Button(); + ButtonRef = new Button(); + ButtonDel = new Button(); + ButtonUpd = new Button(); + ButtonSave = new Button(); + ButtonCancel = new Button(); label1 = new Label(); label2 = new Label(); textBoxPrice = new TextBox(); @@ -47,16 +47,17 @@ groupBox1.SuspendLayout(); SuspendLayout(); // - // buttonAdd + // ButtonAdd // - buttonAdd.Location = new Point(640, 29); - buttonAdd.Margin = new Padding(3, 4, 3, 4); - buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(126, 39); - buttonAdd.TabIndex = 0; - buttonAdd.Text = "Добавить"; - buttonAdd.UseVisualStyleBackColor = true; - buttonAdd.Click += buttonAdd_Click; + ButtonAdd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonAdd.Location = new Point(614, 28); + ButtonAdd.Margin = new Padding(3, 4, 3, 4); + ButtonAdd.Name = "ButtonAdd"; + ButtonAdd.Size = new Size(151, 39); + ButtonAdd.TabIndex = 0; + ButtonAdd.Text = "Добавить"; + ButtonAdd.UseVisualStyleBackColor = true; + ButtonAdd.Click += ButtonAdd_Click; // // dataGridView // @@ -72,7 +73,7 @@ dataGridView.RowHeadersWidth = 51; dataGridView.RowTemplate.Height = 25; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView.Size = new Size(630, 263); + dataGridView.Size = new Size(605, 243); dataGridView.TabIndex = 1; // // ID @@ -102,76 +103,80 @@ // // groupBox1 // - groupBox1.Controls.Add(buttonRef); - groupBox1.Controls.Add(buttonDel); - groupBox1.Controls.Add(buttonUpd); + groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + groupBox1.Controls.Add(ButtonRef); + groupBox1.Controls.Add(ButtonDel); + groupBox1.Controls.Add(ButtonUpd); groupBox1.Controls.Add(dataGridView); - groupBox1.Controls.Add(buttonAdd); + groupBox1.Controls.Add(ButtonAdd); groupBox1.Location = new Point(14, 101); groupBox1.Margin = new Padding(3, 4, 3, 4); groupBox1.Name = "groupBox1"; groupBox1.Padding = new Padding(3, 4, 3, 4); - groupBox1.Size = new Size(773, 295); + groupBox1.Size = new Size(774, 295); groupBox1.TabIndex = 2; groupBox1.TabStop = false; groupBox1.Text = "Компоненты"; // - // buttonRef + // ButtonRef // - buttonRef.Location = new Point(640, 169); - buttonRef.Margin = new Padding(3, 4, 3, 4); - buttonRef.Name = "buttonRef"; - buttonRef.Size = new Size(126, 39); - buttonRef.TabIndex = 4; - buttonRef.Text = "Обновить"; - buttonRef.UseVisualStyleBackColor = true; - buttonRef.Click += buttonRef_Click; + ButtonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonRef.Location = new Point(614, 168); + ButtonRef.Margin = new Padding(3, 4, 3, 4); + ButtonRef.Name = "ButtonRef"; + ButtonRef.Size = new Size(151, 39); + ButtonRef.TabIndex = 4; + ButtonRef.Text = "Обновить"; + ButtonRef.UseVisualStyleBackColor = true; + ButtonRef.Click += ButtonRef_Click; // - // buttonDel + // ButtonDel // - buttonDel.Location = new Point(640, 123); - buttonDel.Margin = new Padding(3, 4, 3, 4); - buttonDel.Name = "buttonDel"; - buttonDel.Size = new Size(126, 39); - buttonDel.TabIndex = 3; - buttonDel.Text = "Удалить"; - buttonDel.UseVisualStyleBackColor = true; - buttonDel.Click += buttonDel_Click; + ButtonDel.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonDel.Location = new Point(614, 122); + ButtonDel.Margin = new Padding(3, 4, 3, 4); + ButtonDel.Name = "ButtonDel"; + ButtonDel.Size = new Size(151, 39); + ButtonDel.TabIndex = 3; + ButtonDel.Text = "Удалить"; + ButtonDel.UseVisualStyleBackColor = true; + ButtonDel.Click += ButtonDel_Click; // - // buttonUpd + // ButtonUpd // - buttonUpd.Location = new Point(640, 76); - buttonUpd.Margin = new Padding(3, 4, 3, 4); - buttonUpd.Name = "buttonUpd"; - buttonUpd.Size = new Size(126, 39); - buttonUpd.TabIndex = 2; - buttonUpd.Text = "Изменить"; - buttonUpd.UseVisualStyleBackColor = true; - buttonUpd.Click += buttonUpd_Click; + ButtonUpd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonUpd.Location = new Point(614, 75); + ButtonUpd.Margin = new Padding(3, 4, 3, 4); + ButtonUpd.Name = "ButtonUpd"; + ButtonUpd.Size = new Size(151, 39); + ButtonUpd.TabIndex = 2; + ButtonUpd.Text = "Изменить"; + ButtonUpd.UseVisualStyleBackColor = true; + ButtonUpd.Click += ButtonUpd_Click; // - // buttonSave + // ButtonSave // - buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonSave.Location = new Point(573, 404); - buttonSave.Margin = new Padding(3, 4, 3, 4); - buttonSave.Name = "buttonSave"; - buttonSave.Size = new Size(100, 31); - buttonSave.TabIndex = 3; - buttonSave.Text = "Сохранить"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; + ButtonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonSave.Location = new Point(573, 404); + ButtonSave.Margin = new Padding(3, 4, 3, 4); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(100, 31); + ButtonSave.TabIndex = 3; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; // - // buttonCancel + // ButtonCancel // - buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonCancel.Location = new Point(679, 404); - buttonCancel.Margin = new Padding(3, 4, 3, 4); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(100, 31); - buttonCancel.TabIndex = 4; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += buttonCancel_Click; + ButtonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + ButtonCancel.Location = new Point(679, 404); + ButtonCancel.Margin = new Padding(3, 4, 3, 4); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(100, 31); + ButtonCancel.TabIndex = 4; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; // // label1 // @@ -209,6 +214,7 @@ // // FormSushi // + AllowDrop = true; AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 451); @@ -216,15 +222,11 @@ Controls.Add(textBoxPrice); Controls.Add(label2); Controls.Add(label1); - Controls.Add(buttonCancel); - Controls.Add(buttonSave); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); Controls.Add(groupBox1); - MaximizeBox = false; - MinimizeBox = false; Name = "FormSushi"; - ShowInTaskbar = false; Text = "Суши"; - TopMost = true; Load += FormSushi_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); groupBox1.ResumeLayout(false); @@ -234,14 +236,14 @@ #endregion - private Button buttonAdd; + private Button ButtonAdd; private DataGridView dataGridView; private GroupBox groupBox1; - private Button buttonRef; - private Button buttonDel; - private Button buttonUpd; - private Button buttonSave; - private Button buttonCancel; + private Button ButtonRef; + private Button ButtonDel; + private Button ButtonUpd; + private Button ButtonSave; + private Button ButtonCancel; private Label label1; private Label label2; private TextBox textBoxPrice; diff --git a/SushiBar/SushiBar/Forms/FormSushi.cs b/SushiBar/SushiBar/Forms/FormSushi.cs index 8e40fa5..9df5936 100644 --- a/SushiBar/SushiBar/Forms/FormSushi.cs +++ b/SushiBar/SushiBar/Forms/FormSushi.cs @@ -1,35 +1,199 @@ -namespace SushiBar.Forms +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarDataModels.Models; + +namespace SushiBar.Forms { - public partial class FormSushi + public partial class FormSushi : Form { - private void buttonAdd_Click(object sender, EventArgs e) + private readonly ILogger _logger; + private readonly ISushiLogic _logic; + private int? _id; + private Dictionary _sushiComponents; + public int Id { set { _id = value; } } + public FormSushi(ILogger logger, ISushiLogic logic) { - ButtonAdd_Click(sender, e); + InitializeComponent(); + _logger = logger; + _logic = logic; + _sushiComponents = new Dictionary(); + } + private void FormSushi_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка изделия"); + try + { + var view = _logic.ReadElement(new SushiSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.SushiName; + textBoxPrice.Text = view.Price.ToString(); + _sushiComponents = view.SushiComponents ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void LoadData() + { + _logger.LogInformation("Загрузка компонент изделия"); + try + { + if (_sushiComponents != null) + { + dataGridView.Rows.Clear(); + foreach (var sc in _sushiComponents) + { + dataGridView.Rows.Add(new object[] { sc.Key, sc.Value.Item1.ComponentName, sc.Value.Item2 }); + } + textBoxPrice.Text = CalcPrice().ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонент изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSushiComponent)); + if (service is FormSushiComponent form) + { + + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Добавление нового компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + if (_sushiComponents.ContainsKey(form.Id)) + { + _sushiComponents[form.Id] = (form.ComponentModel, form.Count); + } + else + { + _sushiComponents.Add(form.Id, (form.ComponentModel, form.Count)); + } + LoadData(); + } + } + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSushiComponent)); + if (service is FormSushiComponent form) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _sushiComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Изменение компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + _sushiComponents[form.Id] = (form.ComponentModel, form.Count); + LoadData(); + } + } + } + } + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + try + { + _logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value); + _sushiComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + LoadData(); + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPrice.Text)) + { + MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (_sushiComponents == null || _sushiComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение изделия"); + try + { + var model = new SushiBindingModel + { + Id = _id ?? 0, + SushiName = textBoxName.Text, + Price = Convert.ToDouble(textBoxPrice.Text), + SushiComponents = _sushiComponents + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } - private void buttonUpd_Click(object sender, EventArgs e) + private void ButtonCancel_Click(object sender, EventArgs e) { - ButtonUpd_Click(sender, e); + DialogResult = DialogResult.Cancel; + Close(); } - - private void buttonDel_Click(object sender, EventArgs e) + private double CalcPrice() { - ButtonDel_Click(sender, e); - } - - private void buttonRef_Click(object sender, EventArgs e) - { - ButtonRef_Click(sender, e); - } - - private void buttonSave_Click(object sender, EventArgs e) - { - ButtonSave_Click(sender, e); - } - - private void buttonCancel_Click(object sender, EventArgs e) - { - ButtonCancel_Click(sender, e); + double price = 0; + foreach (var elem in _sushiComponents) + { + price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); } } } diff --git a/SushiBar/SushiBar/Forms/FormSushi.resx b/SushiBar/SushiBar/Forms/FormSushi.resx index 610eb88..b17d555 100644 --- a/SushiBar/SushiBar/Forms/FormSushi.resx +++ b/SushiBar/SushiBar/Forms/FormSushi.resx @@ -126,13 +126,4 @@ True - - True - - - True - - - True - \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormSushiComponent.Designer.cs b/SushiBar/SushiBar/Forms/FormSushiComponent.Designer.cs index 0563bb3..fb4e212 100644 --- a/SushiBar/SushiBar/Forms/FormSushiComponent.Designer.cs +++ b/SushiBar/SushiBar/Forms/FormSushiComponent.Designer.cs @@ -32,8 +32,8 @@ label2 = new Label(); comboBoxComponent = new ComboBox(); textBoxCount = new TextBox(); - buttonSave = new Button(); - buttonCancel = new Button(); + ButtonSave = new Button(); + ButtonCancel = new Button(); SuspendLayout(); // // label1 @@ -69,33 +69,34 @@ textBoxCount.Size = new Size(227, 27); textBoxCount.TabIndex = 3; // - // buttonSave + // ButtonSave // - buttonSave.Location = new Point(158, 95); - buttonSave.Name = "buttonSave"; - buttonSave.Size = new Size(94, 29); - buttonSave.TabIndex = 4; - buttonSave.Text = "Сохранить"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; + ButtonSave.Location = new Point(158, 95); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(94, 29); + ButtonSave.TabIndex = 4; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; // - // buttonCancel + // ButtonCancel // - buttonCancel.Location = new Point(258, 95); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(94, 29); - buttonCancel.TabIndex = 5; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += buttonCancel_Click; + ButtonCancel.Location = new Point(258, 95); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(94, 29); + ButtonCancel.TabIndex = 5; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; // // FormSushiComponent // + AllowDrop = true; AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(364, 132); - Controls.Add(buttonCancel); - Controls.Add(buttonSave); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); Controls.Add(textBoxCount); Controls.Add(comboBoxComponent); Controls.Add(label2); @@ -116,7 +117,7 @@ private Label label2; private ComboBox comboBoxComponent; private TextBox textBoxCount; - private Button buttonSave; - private Button buttonCancel; + private Button ButtonSave; + private Button ButtonCancel; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormSushiComponent.cs b/SushiBar/SushiBar/Forms/FormSushiComponent.cs index 994a0b3..bcbd12e 100644 --- a/SushiBar/SushiBar/Forms/FormSushiComponent.cs +++ b/SushiBar/SushiBar/Forms/FormSushiComponent.cs @@ -1,25 +1,78 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.ViewModels; +using SushiBarDataModels.Models; namespace SushiBar.Forms { - public partial class FormSushiComponent + partial class FormSushiComponent : Form { - private void buttonSave_Click(object sender, EventArgs e) + private readonly List? _list; + public int Id { - ButtonSave_Click(sender, e); + get + { + return Convert.ToInt32(comboBoxComponent.SelectedValue); + } + set + { + comboBoxComponent.SelectedValue = value; + } } - - private void buttonCancel_Click(object sender, EventArgs e) + public IComponentModel? ComponentModel { - ButtonCancel_Click(sender, e); + get + { + if (_list == null) + { + return null; + } + foreach (var elem in _list) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + public int Count + { + get { return Convert.ToInt32(textBoxCount.Text); } + set + { textBoxCount.Text = value.ToString(); } + } + public FormSushiComponent(IComponentLogic logic) + { + InitializeComponent(); + _list = logic.ReadList(null); + if (_list != null) + { + comboBoxComponent.DisplayMember = "ComponentName"; + comboBoxComponent.ValueMember = "Id"; + comboBoxComponent.DataSource = _list; + comboBoxComponent.SelectedItem = null; + } + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxComponent.SelectedValue == null) + { + MessageBox.Show("Выберите компонент", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); } } } diff --git a/SushiBar/SushiBar/Forms/FormSushiComponentLogic.cs b/SushiBar/SushiBar/Forms/FormSushiComponentLogic.cs deleted file mode 100644 index bcbd12e..0000000 --- a/SushiBar/SushiBar/Forms/FormSushiComponentLogic.cs +++ /dev/null @@ -1,78 +0,0 @@ -using SushiBarContracts.BusinessLogicsContracts; -using SushiBarContracts.ViewModels; -using SushiBarDataModels.Models; - -namespace SushiBar.Forms -{ - partial class FormSushiComponent : Form - { - private readonly List? _list; - public int Id - { - get - { - return Convert.ToInt32(comboBoxComponent.SelectedValue); - } - set - { - comboBoxComponent.SelectedValue = value; - } - } - public IComponentModel? ComponentModel - { - get - { - if (_list == null) - { - return null; - } - foreach (var elem in _list) - { - if (elem.Id == Id) - { - return elem; - } - } - return null; - } - } - public int Count - { - get { return Convert.ToInt32(textBoxCount.Text); } - set - { textBoxCount.Text = value.ToString(); } - } - public FormSushiComponent(IComponentLogic logic) - { - InitializeComponent(); - _list = logic.ReadList(null); - if (_list != null) - { - comboBoxComponent.DisplayMember = "ComponentName"; - comboBoxComponent.ValueMember = "Id"; - comboBoxComponent.DataSource = _list; - comboBoxComponent.SelectedItem = null; - } - } - private void ButtonSave_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxCount.Text)) - { - MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (comboBoxComponent.SelectedValue == null) - { - MessageBox.Show("Выберите компонент", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - DialogResult = DialogResult.OK; - Close(); - } - private void ButtonCancel_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Cancel; - Close(); - } - } -} diff --git a/SushiBar/SushiBar/Forms/FormSushiLogic.cs b/SushiBar/SushiBar/Forms/FormSushiLogic.cs deleted file mode 100644 index 9df5936..0000000 --- a/SushiBar/SushiBar/Forms/FormSushiLogic.cs +++ /dev/null @@ -1,199 +0,0 @@ -using Microsoft.Extensions.Logging; -using SushiBarContracts.BindingModels; -using SushiBarContracts.BusinessLogicsContracts; -using SushiBarContracts.SearchModels; -using SushiBarDataModels.Models; - -namespace SushiBar.Forms -{ - public partial class FormSushi : Form - { - private readonly ILogger _logger; - private readonly ISushiLogic _logic; - private int? _id; - private Dictionary _sushiComponents; - public int Id { set { _id = value; } } - public FormSushi(ILogger logger, ISushiLogic logic) - { - InitializeComponent(); - _logger = logger; - _logic = logic; - _sushiComponents = new Dictionary(); - } - private void FormSushi_Load(object sender, EventArgs e) - { - if (_id.HasValue) - { - _logger.LogInformation("Загрузка изделия"); - try - { - var view = _logic.ReadElement(new SushiSearchModel - { - Id = _id.Value - }); - if (view != null) - { - textBoxName.Text = view.SushiName; - textBoxPrice.Text = view.Price.ToString(); - _sushiComponents = view.SushiComponents ?? new Dictionary(); - LoadData(); - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки изделия"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void LoadData() - { - _logger.LogInformation("Загрузка компонент изделия"); - try - { - if (_sushiComponents != null) - { - dataGridView.Rows.Clear(); - foreach (var sc in _sushiComponents) - { - dataGridView.Rows.Add(new object[] { sc.Key, sc.Value.Item1.ComponentName, sc.Value.Item2 }); - } - textBoxPrice.Text = CalcPrice().ToString(); - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки компонент изделия"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void ButtonAdd_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormSushiComponent)); - if (service is FormSushiComponent form) - { - - if (form.ShowDialog() == DialogResult.OK) - { - if (form.ComponentModel == null) - { - return; - } - _logger.LogInformation("Добавление нового компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); - if (_sushiComponents.ContainsKey(form.Id)) - { - _sushiComponents[form.Id] = (form.ComponentModel, form.Count); - } - else - { - _sushiComponents.Add(form.Id, (form.ComponentModel, form.Count)); - } - LoadData(); - } - } - } - private void ButtonUpd_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - var service = Program.ServiceProvider?.GetService(typeof(FormSushiComponent)); - if (service is FormSushiComponent form) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); - form.Id = id; - form.Count = _sushiComponents[id].Item2; - if (form.ShowDialog() == DialogResult.OK) - { - if (form.ComponentModel == null) - { - return; - } - _logger.LogInformation("Изменение компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); - _sushiComponents[form.Id] = (form.ComponentModel, form.Count); - LoadData(); - } - } - } - } - private void ButtonDel_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) - { - try - { - _logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value); - _sushiComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); - } - catch (Exception ex) - { - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - LoadData(); - } - } - } - private void ButtonRef_Click(object sender, EventArgs e) - { - LoadData(); - } - private void ButtonSave_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxName.Text)) - { - MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (string.IsNullOrEmpty(textBoxPrice.Text)) - { - MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (_sushiComponents == null || _sushiComponents.Count == 0) - { - MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - _logger.LogInformation("Сохранение изделия"); - try - { - var model = new SushiBindingModel - { - Id = _id ?? 0, - SushiName = textBoxName.Text, - Price = Convert.ToDouble(textBoxPrice.Text), - SushiComponents = _sushiComponents - }; - var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); - DialogResult = DialogResult.OK; - Close(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка сохранения изделия"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - private void ButtonCancel_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Cancel; - Close(); - } - private double CalcPrice() - { - double price = 0; - foreach (var elem in _sushiComponents) - { - price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); - } - return Math.Round(price * 1.1, 2); - } - } -} diff --git a/SushiBar/SushiBar/Forms/FormSushis.Designer.cs b/SushiBar/SushiBar/Forms/FormSushis.Designer.cs index b340685..5b297a2 100644 --- a/SushiBar/SushiBar/Forms/FormSushis.Designer.cs +++ b/SushiBar/SushiBar/Forms/FormSushis.Designer.cs @@ -29,78 +29,84 @@ private void InitializeComponent() { dataGridView = new DataGridView(); - buttonRef = new Button(); - buttonDel = new Button(); - buttonUpd = new Button(); - buttonAdd = new Button(); + ButtonRef = new Button(); + ButtonDel = new Button(); + ButtonUpd = new Button(); + ButtonAdd = new Button(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // dataGridView // - dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Left; dataGridView.Location = new Point(0, 0); dataGridView.Name = "dataGridView"; dataGridView.RowHeadersWidth = 70; dataGridView.RowTemplate.Height = 29; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView.Size = new Size(762, 450); + dataGridView.Size = new Size(777, 450); dataGridView.TabIndex = 1; // - // buttonRef + // ButtonRef // - buttonRef.Location = new Point(768, 117); - buttonRef.Name = "buttonRef"; - buttonRef.Size = new Size(114, 29); - buttonRef.TabIndex = 8; - buttonRef.Text = "Обновить"; - buttonRef.UseVisualStyleBackColor = true; - buttonRef.Click += buttonRef_Click; + ButtonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonRef.Location = new Point(783, 117); + ButtonRef.Name = "ButtonRef"; + ButtonRef.Size = new Size(150, 29); + ButtonRef.TabIndex = 8; + ButtonRef.Text = "Обновить"; + ButtonRef.UseVisualStyleBackColor = true; + ButtonRef.Click += ButtonRef_Click; // - // buttonDel + // ButtonDel // - buttonDel.Location = new Point(768, 82); - buttonDel.Name = "buttonDel"; - buttonDel.Size = new Size(114, 29); - buttonDel.TabIndex = 7; - buttonDel.Text = "Удалить"; - buttonDel.UseVisualStyleBackColor = true; - buttonDel.Click += buttonDel_Click; + ButtonDel.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonDel.Location = new Point(783, 82); + ButtonDel.Name = "ButtonDel"; + ButtonDel.Size = new Size(150, 29); + ButtonDel.TabIndex = 7; + ButtonDel.Text = "Удалить"; + ButtonDel.UseVisualStyleBackColor = true; + ButtonDel.Click += ButtonDel_Click; // - // buttonUpd + // ButtonUpd // - buttonUpd.Location = new Point(768, 47); - buttonUpd.Name = "buttonUpd"; - buttonUpd.Size = new Size(114, 29); - buttonUpd.TabIndex = 6; - buttonUpd.Text = "Изменить"; - buttonUpd.UseVisualStyleBackColor = true; - buttonUpd.Click += buttonUpd_Click; + ButtonUpd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonUpd.Location = new Point(783, 47); + ButtonUpd.Name = "ButtonUpd"; + ButtonUpd.Size = new Size(150, 29); + ButtonUpd.TabIndex = 6; + ButtonUpd.Text = "Изменить"; + ButtonUpd.UseVisualStyleBackColor = true; + ButtonUpd.Click += ButtonUpd_Click; // - // buttonAdd + // ButtonAdd // - buttonAdd.Location = new Point(768, 12); - buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(114, 29); - buttonAdd.TabIndex = 5; - buttonAdd.Text = "Добавить"; - buttonAdd.UseVisualStyleBackColor = true; - buttonAdd.Click += buttonAdd_Click; + ButtonAdd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonAdd.Location = new Point(783, 12); + ButtonAdd.Name = "ButtonAdd"; + ButtonAdd.Size = new Size(150, 29); + ButtonAdd.TabIndex = 5; + ButtonAdd.Text = "Добавить"; + ButtonAdd.UseVisualStyleBackColor = true; + ButtonAdd.Click += ButtonAdd_Click; // // FormSushis // + AllowDrop = true; AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(893, 450); - Controls.Add(buttonRef); - Controls.Add(buttonDel); - Controls.Add(buttonUpd); - Controls.Add(buttonAdd); + ClientSize = new Size(945, 450); + Controls.Add(ButtonRef); + Controls.Add(ButtonDel); + Controls.Add(ButtonUpd); + Controls.Add(ButtonAdd); Controls.Add(dataGridView); Name = "FormSushis"; Text = "Суши"; + WindowState = FormWindowState.Maximized; Load += FormSushis_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); @@ -109,9 +115,9 @@ #endregion private DataGridView dataGridView; - private Button buttonRef; - private Button buttonDel; - private Button buttonUpd; - private Button buttonAdd; + private Button ButtonRef; + private Button ButtonDel; + private Button ButtonUpd; + private Button ButtonAdd; } } \ No newline at end of file diff --git a/SushiBar/SushiBar/Forms/FormSushis.cs b/SushiBar/SushiBar/Forms/FormSushis.cs index 6a12f95..caefccd 100644 --- a/SushiBar/SushiBar/Forms/FormSushis.cs +++ b/SushiBar/SushiBar/Forms/FormSushis.cs @@ -1,35 +1,99 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; namespace SushiBar.Forms { - public partial class FormSushis + public partial class FormSushis : Form { - private void buttonAdd_Click(object sender, EventArgs e) + private readonly ILogger _logger; + private readonly ISushiLogic _logic; + public FormSushis(ILogger logger, ISushiLogic logic) { - ButtonAdd_Click(sender, e); + InitializeComponent(); + _logger = logger; + _logic = logic; } - - private void buttonUpd_Click(object sender, EventArgs e) + private void FormSushis_Load(object sender, EventArgs e) { - ButtonUpd_Click(sender, e); + LoadData(); } - - private void buttonDel_Click(object sender, EventArgs e) + private void LoadData() { - ButtonDel_Click(sender, e); + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["SushiComponents"].Visible = false; + dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка суши"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки суши"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } - - private void buttonRef_Click(object sender, EventArgs e) + private void ButtonAdd_Click(object sender, EventArgs e) { - ButtonRef_Click(sender, e); + var service = Program.ServiceProvider?.GetService(typeof(FormSushi)); + if (service is FormSushi form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSushi)); + if (service is FormSushi form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление компонента"); + try + { + if (!_logic.Delete(new SushiBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); } } } diff --git a/SushiBar/SushiBar/Forms/FormSushisLogic.cs b/SushiBar/SushiBar/Forms/FormSushisLogic.cs deleted file mode 100644 index caefccd..0000000 --- a/SushiBar/SushiBar/Forms/FormSushisLogic.cs +++ /dev/null @@ -1,99 +0,0 @@ -using Microsoft.Extensions.Logging; -using SushiBarContracts.BindingModels; -using SushiBarContracts.BusinessLogicsContracts; - -namespace SushiBar.Forms -{ - public partial class FormSushis : Form - { - private readonly ILogger _logger; - private readonly ISushiLogic _logic; - public FormSushis(ILogger logger, ISushiLogic logic) - { - InitializeComponent(); - _logger = logger; - _logic = logic; - } - private void FormSushis_Load(object sender, EventArgs e) - { - LoadData(); - } - private void LoadData() - { - try - { - var list = _logic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["SushiComponents"].Visible = false; - dataGridView.Columns["SushiName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - } - _logger.LogInformation("Загрузка суши"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки суши"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void ButtonAdd_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormSushi)); - if (service is FormSushi form) - { - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } - } - } - private void ButtonUpd_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - var service = Program.ServiceProvider?.GetService(typeof(FormSushi)); - if (service is FormSushi form) - { - form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - if (form.ShowDialog() == DialogResult.OK) - { - LoadData(); - } - } - } - } - private void ButtonDel_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Удаление компонента"); - try - { - if (!_logic.Delete(new SushiBindingModel - { - Id = id - })) - { - throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка удаления компонента"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - } - private void ButtonRef_Click(object sender, EventArgs e) - { - LoadData(); - } - } -} -- 2.25.1