From a67b4a9631c5a780ad337619f1efc53280494e25 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:27:19 +0300 Subject: [PATCH 1/6] lab 2 base --- Pizzeria/Pizzeria.sln | 8 +- Pizzeria/Pizzeria/Pizzeria.csproj | 2 +- Pizzeria/Pizzeria/Program.cs | 2 +- .../DataFileSingleton.cs | 51 +++++++++ .../Implements/ComponentStorage.cs | 81 ++++++++++++++ .../Implements/OrderStorage.cs | 72 ++++++++++++ .../Implements/PizzaStorage.cs | 81 ++++++++++++++ .../PizzeriaFileImplement.csproj | 14 +++ .../models/Component.cs | 59 ++++++++++ .../PizzeriaShopFileImplement/models/Order.cs | 105 ++++++++++++++++++ .../PizzeriaShopFileImplement/models/Pizza.cs | 89 +++++++++++++++ 11 files changed, 561 insertions(+), 3 deletions(-) create mode 100644 Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/Implements/ComponentStorage.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/Implements/OrderStorage.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/Implements/PizzaStorage.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/PizzeriaFileImplement.csproj create mode 100644 Pizzeria/PizzeriaShopFileImplement/models/Component.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/models/Order.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/models/Pizza.cs diff --git a/Pizzeria/Pizzeria.sln b/Pizzeria/Pizzeria.sln index 77691e4..1f6ef81 100644 --- a/Pizzeria/Pizzeria.sln +++ b/Pizzeria/Pizzeria.sln @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaContracts", "Pizzer EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{B55806ED-5FDE-4A9F-B500-1DFBE8E50491}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{138A775B-55CA-4120-918F-6093C0B2B241}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{138A775B-55CA-4120-918F-6093C0B2B241}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaFileImplement", "PizzeriaShopFileImplement\PizzeriaFileImplement.csproj", "{44B31B6F-2852-454B-9E7D-E965C9A348ED}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {138A775B-55CA-4120-918F-6093C0B2B241}.Debug|Any CPU.Build.0 = Debug|Any CPU {138A775B-55CA-4120-918F-6093C0B2B241}.Release|Any CPU.ActiveCfg = Release|Any CPU {138A775B-55CA-4120-918F-6093C0B2B241}.Release|Any CPU.Build.0 = Release|Any CPU + {44B31B6F-2852-454B-9E7D-E965C9A348ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {44B31B6F-2852-454B-9E7D-E965C9A348ED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {44B31B6F-2852-454B-9E7D-E965C9A348ED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {44B31B6F-2852-454B-9E7D-E965C9A348ED}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Pizzeria/Pizzeria/Pizzeria.csproj b/Pizzeria/Pizzeria/Pizzeria.csproj index 43e30b3..ea801bb 100644 --- a/Pizzeria/Pizzeria/Pizzeria.csproj +++ b/Pizzeria/Pizzeria/Pizzeria.csproj @@ -18,7 +18,7 @@ - + diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index 6905fbb..ec55503 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -1,7 +1,7 @@ using PizzeriaBusinessLogic; using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; -using PizzeriaListImplement.Implements; +using PizzeriaFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..d09c25a --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs @@ -0,0 +1,51 @@ +using PizzeriaFileImplement.models; +using System.Xml.Linq; + +namespace PizzeriaFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string PizzaFileName = "Pizza.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Pizzas { 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 SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => + Component.Create(x)!)!; + Pizzas = LoadData(PizzaFileName, "Pizza", x => + Pizza.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ComponentStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..843f621 --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,81 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.models; + + +namespace PizzeriaFileImplement.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/Pizzeria/PizzeriaShopFileImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..9c695a3 --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,72 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.models; + + +namespace PizzeriaFileImplement.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) + { + return source.Orders + .Where(x => (model.Id.HasValue && x.Id == model.Id)) + .Select(x => x.GetViewModel) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + return source.Orders + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return 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/Pizzeria/PizzeriaShopFileImplement/Implements/PizzaStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/PizzaStorage.cs new file mode 100644 index 0000000..15fbecc --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/PizzaStorage.cs @@ -0,0 +1,81 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.models; + + +namespace PizzeriaFileImplement.Implements +{ + public class PizzaStorage : IPizzaStorage + { + private readonly DataFileSingleton source; + public PizzaStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Pizzas + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName)) + { + return new(); + } + return source.Pizzas + .Where(x => x.PizzaName.Contains(model.PizzaName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public PizzaViewModel? GetElement(PizzaSearchModel model) + { + if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue) + { + return null; + } + return source.Pizzas + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.PizzaName) && x.PizzaName == model.PizzaName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public PizzaViewModel? Insert(PizzaBindingModel model) + { + model.Id = source.Pizzas.Count > 0 ? source.Pizzas.Max(x => x.Id) + 1 : 1; + var newPizza = Pizza.Create(model); + if (newPizza == null) + { + return null; + } + source.Pizzas.Add(newPizza); + source.SavePizzas(); + return newPizza.GetViewModel; + } + public PizzaViewModel? Update(PizzaBindingModel model) + { + var pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.Id); + if (pizza == null) + { + return null; + } + pizza.Update(model); + source.SavePizzas(); + return pizza.GetViewModel; + } + public PizzaViewModel? Delete(PizzaBindingModel model) + { + var element = source.Pizzas.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Pizzas.Remove(element); + source.SavePizzas(); + return element.GetViewModel; + } + return null; + } + } + +} diff --git a/Pizzeria/PizzeriaShopFileImplement/PizzeriaFileImplement.csproj b/Pizzeria/PizzeriaShopFileImplement/PizzeriaFileImplement.csproj new file mode 100644 index 0000000..b612a23 --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/PizzeriaFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Component.cs b/Pizzeria/PizzeriaShopFileImplement/models/Component.cs new file mode 100644 index 0000000..db20025 --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/models/Component.cs @@ -0,0 +1,59 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.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/Pizzeria/PizzeriaShopFileImplement/models/Order.cs b/Pizzeria/PizzeriaShopFileImplement/models/Order.cs new file mode 100644 index 0000000..f94c993 --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/models/Order.cs @@ -0,0 +1,105 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.models +{ + public class Order : IOrderModel + { + public int PizzaId { get; private set; } + + public string PizzaName + { + get + { + var source = DataFileSingleton.GetInstance(); + return source.Pizzas.FirstOrDefault(x => x.Id == PizzaId).PizzaName; + } + } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int Id { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PizzaId = model.PizzaId, + 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), + PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToInt32(element.Element("Sum")!.Value), + Status = Enum.Parse(element.Element("Status")!.Value), + DateCreate = DateTime.Parse(element.Element("DateCreate")!.Value), + DateImplement = !string.IsNullOrEmpty(element.Element("DateImplement")!.Value.ToString()) ? + DateTime.Parse(element.Element("DateImplement")!.Value) : null, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + PizzaId = model.PizzaId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + PizzaId = PizzaId, + PizzaName = PizzaName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("PizzaId", PizzaId), + new XElement("Count", Count), + new XElement("Sum", Sum), + new XElement("Status", Status), + new XElement("DateCreate", DateCreate), + new XElement("DateImplement", DateImplement)); + } +} diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Pizza.cs b/Pizzeria/PizzeriaShopFileImplement/models/Pizza.cs new file mode 100644 index 0000000..7220c53 --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/models/Pizza.cs @@ -0,0 +1,89 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.models +{ + public class Pizza : IPizzaModel + { + public int Id { get; private set; } + public string PizzaName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _pizzaComponents = null; + public Dictionary PizzaComponents + { + get + { + if (_pizzaComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _pizzaComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _pizzaComponents; + } + } + public static Pizza? Create(PizzaBindingModel model) + { + if (model == null) + { + return null; + } + return new Pizza() + { + Id = model.Id, + PizzaName = model.PizzaName, + Price = model.Price, + Components = model.PizzaComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Pizza? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pizza() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PizzaName = element.Element("PizzaName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PizzaComponents")!.Elements("PizzaComponent") + .ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PizzaBindingModel model) + { + if (model == null) + { + return; + } + PizzaName = model.PizzaName; + Price = model.Price; + Components = model.PizzaComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _pizzaComponents = null; + } + public PizzaViewModel GetViewModel => new() + { + Id = Id, + PizzaName = PizzaName, + Price = Price, + PizzaComponents = PizzaComponents + }; + public XElement GetXElement => new("Pizza", + new XAttribute("Id", Id), + new XElement("PizzaName", PizzaName), + new XElement("Price", Price.ToString()), + new XElement("PizzaComponents", Components.Select(x => + new XElement("PizzaComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} -- 2.25.1 From abc25205ce1dee24639a73fb23e5cedd89e4bbca Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:10:44 +0400 Subject: [PATCH 2/6] step 1 --- .../DataFileSingleton.cs | 9 +- .../Implements/ShopStorage.cs | 79 ++++++++++++++++ .../PizzeriaShopFileImplement/models/Shop.cs | 93 +++++++++++++++++++ 3 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/models/Shop.cs diff --git a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs index d09c25a..3e309f1 100644 --- a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace PizzeriaFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PizzaFileName = "Pizza.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pizzas { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -25,10 +27,9 @@ namespace PizzeriaFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); private DataFileSingleton() { - Components = LoadData(ComponentFileName, "Component", x => - Component.Create(x)!)!; - Pizzas = LoadData(PizzaFileName, "Pizza", x => - Pizza.Create(x)!)!; + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Pizzas = LoadData(PizzaFileName, "Pizza", x => Pizza.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..8605fca --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,79 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.models; + +namespace PizzeriaFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Shops + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops + .Where(x => x.ShopName.Contains(model.ShopName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveData(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveData(); + return shop.GetViewModel; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveData(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs new file mode 100644 index 0000000..0090f9d --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs @@ -0,0 +1,93 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Addres { get; set; } = string.Empty; + public DateTime OpenTime { get; set; } + public Dictionary Pizzas { get; private set; } = new(); + private Dictionary? _shopPizzas = null; + public Dictionary ShopPizzas + { + get + { + if (_shopPizzas == null) + { + var source = DataFileSingleton.GetInstance(); + _shopPizzas = Pizzas.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!, + y.Value)); + } + return _shopPizzas; + } + } + + public static Shop? Create(ShopBindingModel model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Addres = model.Addres, + OpenTime = model.OpenTime, + Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Addres = element.Element("Price")!.Value, + Pizzas = element.Element("ShopPizzas")!.Elements("ShopPizza") + .ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(ShopBindingModel model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Addres = model.Addres; + OpenTime = model.OpenTime; + Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopPizzas = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Addres = Addres, + OpenTime = OpenTime, + ShopPizzas = ShopPizzas + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Addres", Addres), + new XElement("ShopPizzas", Pizzas.Select(x => + new XElement("ShopPizza", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} -- 2.25.1 From dbeb844986167ca636d740fa655cec616277f081 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Tue, 11 Apr 2023 21:52:06 +0400 Subject: [PATCH 3/6] add some logic --- Pizzeria/Pizzeria/FormMain.Designer.cs | 14 +++++++++ Pizzeria/Pizzeria/FormMain.cs | 5 +++ Pizzeria/Pizzeria/FormShop.Designer.cs | 31 ++++++++++++++++--- Pizzeria/Pizzeria/FormShop.cs | 1 + .../BindingModels/ShopBindingModel.cs | 1 + .../ViewModels/ShopViewModel.cs | 3 +- .../PizzeriaDataModels/Models/IShopModel.cs | 1 + .../DataFileSingleton.cs | 1 + .../Implements/ShopStorage.cs | 6 ++-- .../PizzeriaShopFileImplement/models/Shop.cs | 10 +++++- 10 files changed, 64 insertions(+), 9 deletions(-) diff --git a/Pizzeria/Pizzeria/FormMain.Designer.cs b/Pizzeria/Pizzeria/FormMain.Designer.cs index f00039f..bd01748 100644 --- a/Pizzeria/Pizzeria/FormMain.Designer.cs +++ b/Pizzeria/Pizzeria/FormMain.Designer.cs @@ -42,6 +42,7 @@ this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.магазиныToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.button6 = new System.Windows.Forms.Button(); + this.buttonSell = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); @@ -174,11 +175,23 @@ this.button6.UseVisualStyleBackColor = true; this.button6.Click += new System.EventHandler(this.ButtonRestore_Click); // + // buttonSell + // + this.buttonSell.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSell.Location = new System.Drawing.Point(987, 328); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(209, 29); + this.buttonSell.TabIndex = 13; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1233, 439); + this.Controls.Add(this.buttonSell); this.Controls.Add(this.button6); this.Controls.Add(this.menuStrip1); this.Controls.Add(this.button3); @@ -214,5 +227,6 @@ private ToolStripMenuItem изделияToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; private Button button6; + private Button buttonSell; } } \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormMain.cs b/Pizzeria/Pizzeria/FormMain.cs index 4f2c854..f48dbbb 100644 --- a/Pizzeria/Pizzeria/FormMain.cs +++ b/Pizzeria/Pizzeria/FormMain.cs @@ -159,6 +159,11 @@ namespace Pizzeria LoadData(); } + private void ButtonSell_Click(object sender, EventArgs e) + { + LoadData(); + } + private void ButtonRestore_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormShopPizza)); diff --git a/Pizzeria/Pizzeria/FormShop.Designer.cs b/Pizzeria/Pizzeria/FormShop.Designer.cs index 921424f..5c4799e 100644 --- a/Pizzeria/Pizzeria/FormShop.Designer.cs +++ b/Pizzeria/Pizzeria/FormShop.Designer.cs @@ -41,6 +41,8 @@ this.labelShop = new System.Windows.Forms.Label(); this.buttonSave = new System.Windows.Forms.Button(); this.buttonCancel = new System.Windows.Forms.Button(); + this.textBoxCapacity = new System.Windows.Forms.TextBox(); + this.label1 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); // @@ -103,7 +105,7 @@ this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 62; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(664, 296); + this.dataGridView.Size = new System.Drawing.Size(870, 296); this.dataGridView.TabIndex = 21; // // ColumnID @@ -147,7 +149,7 @@ // buttonSave // this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonSave.Location = new System.Drawing.Point(415, 383); + this.buttonSave.Location = new System.Drawing.Point(621, 383); this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonSave.Name = "buttonSave"; this.buttonSave.Size = new System.Drawing.Size(137, 30); @@ -159,7 +161,7 @@ // buttonCancel // this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonCancel.Location = new System.Drawing.Point(558, 383); + this.buttonCancel.Location = new System.Drawing.Point(764, 383); this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(118, 30); @@ -168,11 +170,30 @@ this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); // + // textBoxCapacity + // + this.textBoxCapacity.Location = new System.Drawing.Point(696, 36); + this.textBoxCapacity.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.textBoxCapacity.Name = "textBoxCapacity"; + this.textBoxCapacity.Size = new System.Drawing.Size(186, 27); + this.textBoxCapacity.TabIndex = 29; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(696, 12); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(100, 20); + this.label1.TabIndex = 30; + this.label1.Text = "Вместимость"; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(704, 426); + this.ClientSize = new System.Drawing.Size(910, 426); + this.Controls.Add(this.label1); + this.Controls.Add(this.textBoxCapacity); this.Controls.Add(this.buttonSave); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.dateTimePicker); @@ -206,5 +227,7 @@ private Label labelShop; private Button buttonSave; private Button buttonCancel; + private TextBox textBoxCapacity; + private Label label1; } } \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormShop.cs b/Pizzeria/Pizzeria/FormShop.cs index f0298e4..9d0875f 100644 --- a/Pizzeria/Pizzeria/FormShop.cs +++ b/Pizzeria/Pizzeria/FormShop.cs @@ -90,6 +90,7 @@ namespace Pizzeria Id = _id ?? 0, ShopName = textBoxName.Text, Addres = textBoxAddres.Text, + Capacity = Convert.ToInt32(textBoxCapacity.Text), OpenTime = dateTimePicker.Value.Date, ShopPizzas = _shopPizzas }; diff --git a/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs index 621cafe..1787239 100644 --- a/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs +++ b/Pizzeria/PizzeriaContracts/BindingModels/ShopBindingModel.cs @@ -10,6 +10,7 @@ namespace PizzeriaContracts.BindingModels public string Addres { get; set; } = String.Empty; public DateTime OpenTime { get; set; } + public int Capacity { get; set; } public Dictionary ShopPizzas { diff --git a/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs index ef7b3e1..c86ebfe 100644 --- a/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs +++ b/Pizzeria/PizzeriaContracts/ViewModels/ShopViewModel.cs @@ -13,7 +13,8 @@ namespace PizzeriaContracts.ViewModels public string Addres { get; set; } = string.Empty; [DisplayName("Дата открытия")] public DateTime OpenTime { get; set; } - + [DisplayName("Вместимость")] + public int Capacity { get; set; } public Dictionary ShopPizzas { get; diff --git a/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs b/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs index 7760585..3b19692 100644 --- a/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs +++ b/Pizzeria/PizzeriaDataModels/Models/IShopModel.cs @@ -12,6 +12,7 @@ namespace PizzeriaDataModels string Addres { get; } DateTime OpenTime { get; } + int Capacity { get; } Dictionary ShopPizzas { get; } } } diff --git a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs index 3e309f1..1aa6d12 100644 --- a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs @@ -24,6 +24,7 @@ namespace PizzeriaFileImplement } public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); private DataFileSingleton() { diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs index 8605fca..04f9a85 100644 --- a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -50,7 +50,7 @@ namespace PizzeriaFileImplement.Implements return null; } source.Shops.Add(newShop); - source.SaveData(); + source.SaveShops(); return newShop.GetViewModel; } public ShopViewModel? Update(ShopBindingModel model) @@ -61,7 +61,7 @@ namespace PizzeriaFileImplement.Implements return null; } shop.Update(model); - source.SaveData(); + source.SaveShops(); return shop.GetViewModel; } public ShopViewModel? Delete(ShopBindingModel model) @@ -70,7 +70,7 @@ namespace PizzeriaFileImplement.Implements if (element != null) { source.Shops.Remove(element); - source.SaveData(); + source.SaveShops(); return element.GetViewModel; } return null; diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs index 0090f9d..9af508f 100644 --- a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs +++ b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs @@ -11,6 +11,7 @@ namespace PizzeriaFileImplement.models public string ShopName { get; set; } = string.Empty; public string Addres { get; set; } = string.Empty; public DateTime OpenTime { get; set; } + public int Capacity { get; set; } public Dictionary Pizzas { get; private set; } = new(); private Dictionary? _shopPizzas = null; public Dictionary ShopPizzas @@ -40,6 +41,7 @@ namespace PizzeriaFileImplement.models ShopName = model.ShopName, Addres = model.Addres, OpenTime = model.OpenTime, + Capacity = model.Capacity, Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2) }; } @@ -49,11 +51,14 @@ namespace PizzeriaFileImplement.models { return null; } + return new Shop() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ShopName = element.Element("ShopName")!.Value, - Addres = element.Element("Price")!.Value, + Addres = element.Element("Addres")!.Value, + OpenTime = DateTime.Parse(element.Element("OpenTime")!.Value), + Capacity = Convert.ToInt32(element.Element("Capacity")!.Value), Pizzas = element.Element("ShopPizzas")!.Elements("ShopPizza") .ToDictionary( x => Convert.ToInt32(x.Element("Key")?.Value), @@ -78,12 +83,15 @@ namespace PizzeriaFileImplement.models ShopName = ShopName, Addres = Addres, OpenTime = OpenTime, + Capacity = Capacity, ShopPizzas = ShopPizzas }; public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("ShopName", ShopName), new XElement("Addres", Addres), + new XElement("OpenTime", OpenTime), + new XElement("Capacity", Capacity), new XElement("ShopPizzas", Pizzas.Select(x => new XElement("ShopPizza", new XElement("Key", x.Key), -- 2.25.1 From 1e9c5a2ae12fbc8b01f7ed05753dc08a9ccc3c19 Mon Sep 17 00:00:00 2001 From: parap Date: Thu, 20 Apr 2023 11:47:09 +0400 Subject: [PATCH 4/6] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=202=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=87=D1=82=D0=B8=20=D0=BF=D0=BE=D0=BB=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pizzeria/Pizzeria/FormMain.cs | 6 +- .../Pizzeria/FormPizzaComponent.Designer.cs | 113 +++++++++-------- Pizzeria/Pizzeria/FormSellPizza.Designer.cs | 118 ++++++++++++++++++ Pizzeria/Pizzeria/FormSellPizza.cs | 93 ++++++++++++++ Pizzeria/Pizzeria/FormSellPizza.resx | 60 +++++++++ Pizzeria/Pizzeria/FormShop.cs | 1 + Pizzeria/Pizzeria/Program.cs | 1 + Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs | 21 ++++ .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../StoragesContracts/IShopStorage.cs | 2 + Pizzeria/PizzeriaListImplement/Models/Shop.cs | 5 + .../Implements/ShopStorage.cs | 37 ++++++ .../PizzeriaShopFileImplement/models/Shop.cs | 2 +- 13 files changed, 401 insertions(+), 59 deletions(-) create mode 100644 Pizzeria/Pizzeria/FormSellPizza.Designer.cs create mode 100644 Pizzeria/Pizzeria/FormSellPizza.cs create mode 100644 Pizzeria/Pizzeria/FormSellPizza.resx diff --git a/Pizzeria/Pizzeria/FormMain.cs b/Pizzeria/Pizzeria/FormMain.cs index f48dbbb..686b13a 100644 --- a/Pizzeria/Pizzeria/FormMain.cs +++ b/Pizzeria/Pizzeria/FormMain.cs @@ -161,7 +161,11 @@ namespace Pizzeria private void ButtonSell_Click(object sender, EventArgs e) { - LoadData(); + var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza)); + if (service is FormSellPizza form) + { + form.ShowDialog(); + } } private void ButtonRestore_Click(object sender, EventArgs e) diff --git a/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs b/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs index 9d5ab54..f30f5a8 100644 --- a/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs +++ b/Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs @@ -28,84 +28,83 @@ /// private void InitializeComponent() { - this.buttonCancel = new System.Windows.Forms.Button(); - this.buttonSave = new System.Windows.Forms.Button(); - this.textBoxCount = new System.Windows.Forms.TextBox(); - this.label2 = new System.Windows.Forms.Label(); - this.label1 = new System.Windows.Forms.Label(); - this.comboBoxComponent = new System.Windows.Forms.ComboBox(); - this.SuspendLayout(); + buttonCancel = new Button(); + buttonSave = new Button(); + textBoxCount = new TextBox(); + label2 = new Label(); + label1 = new Label(); + comboBoxComponent = new ComboBox(); + SuspendLayout(); // // buttonCancel // - this.buttonCancel.Location = new System.Drawing.Point(249, 101); - this.buttonCancel.Name = "buttonCancel"; - this.buttonCancel.Size = new System.Drawing.Size(94, 29); - this.buttonCancel.TabIndex = 11; - this.buttonCancel.Text = "Отмена"; - this.buttonCancel.UseVisualStyleBackColor = true; - this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + buttonCancel.Location = new Point(249, 101); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 11; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; // // buttonSave // - this.buttonSave.Location = new System.Drawing.Point(135, 101); - this.buttonSave.Name = "buttonSave"; - this.buttonSave.Size = new System.Drawing.Size(94, 29); - this.buttonSave.TabIndex = 10; - this.buttonSave.Text = "Сохранить"; - this.buttonSave.UseVisualStyleBackColor = true; - this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + buttonSave.Location = new Point(135, 101); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(94, 29); + buttonSave.TabIndex = 10; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; // // textBoxCount // - this.textBoxCount.Location = new System.Drawing.Point(118, 59); - this.textBoxCount.Name = "textBoxCount"; - this.textBoxCount.Size = new System.Drawing.Size(225, 27); - this.textBoxCount.TabIndex = 9; + textBoxCount.Location = new Point(118, 59); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(225, 27); + textBoxCount.TabIndex = 9; // // label2 // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(23, 66); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(93, 20); - this.label2.TabIndex = 7; - this.label2.Text = "Количество:"; + label2.AutoSize = true; + label2.Location = new Point(23, 66); + label2.Name = "label2"; + label2.Size = new Size(93, 20); + label2.TabIndex = 7; + label2.Text = "Количество:"; // // label1 // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(23, 33); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(91, 20); - this.label1.TabIndex = 6; - this.label1.Text = "Компонент:"; + label1.AutoSize = true; + label1.Location = new Point(23, 33); + label1.Name = "label1"; + label1.Size = new Size(91, 20); + label1.TabIndex = 6; + label1.Text = "Компонент:"; // // comboBoxComponent // - this.comboBoxComponent.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxComponent.FormattingEnabled = true; - this.comboBoxComponent.Location = new System.Drawing.Point(120, 25); - this.comboBoxComponent.Name = "comboBoxComponent"; - this.comboBoxComponent.Size = new System.Drawing.Size(224, 28); - this.comboBoxComponent.TabIndex = 12; + comboBoxComponent.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxComponent.FormattingEnabled = true; + comboBoxComponent.Location = new Point(120, 25); + comboBoxComponent.Name = "comboBoxComponent"; + comboBoxComponent.Size = new Size(224, 28); + comboBoxComponent.TabIndex = 12; // // FormPizzaComponent // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(370, 153); - this.Controls.Add(this.comboBoxComponent); - this.Controls.Add(this.buttonCancel); - this.Controls.Add(this.buttonSave); - this.Controls.Add(this.textBoxCount); - this.Controls.Add(this.label2); - this.Controls.Add(this.label1); - this.Name = "FormPizzaComponent"; - this.Text = "Компонент пиццы"; - this.ResumeLayout(false); - this.PerformLayout(); - + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(370, 153); + Controls.Add(comboBoxComponent); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxCount); + Controls.Add(label2); + Controls.Add(label1); + Name = "FormPizzaComponent"; + Text = "Компонент пиццы"; + ResumeLayout(false); + PerformLayout(); } #endregion diff --git a/Pizzeria/Pizzeria/FormSellPizza.Designer.cs b/Pizzeria/Pizzeria/FormSellPizza.Designer.cs new file mode 100644 index 0000000..53a2aa2 --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.Designer.cs @@ -0,0 +1,118 @@ +namespace Pizzeria +{ + partial class FormSellPizza + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + comboBoxPizza = new ComboBox(); + buttonCancel = new Button(); + buttonSell = new Button(); + textBoxCount = new TextBox(); + label2 = new Label(); + label1 = new Label(); + SuspendLayout(); + // + // comboBoxPizza + // + comboBoxPizza.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxPizza.FormattingEnabled = true; + comboBoxPizza.Location = new Point(113, 25); + comboBoxPizza.Name = "comboBoxPizza"; + comboBoxPizza.Size = new Size(224, 28); + comboBoxPizza.TabIndex = 18; + // + // buttonCancel + // + buttonCancel.Location = new Point(242, 101); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 17; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + // + // buttonSell + // + buttonSell.Location = new Point(128, 101); + buttonSell.Name = "buttonSell"; + buttonSell.Size = new Size(94, 29); + buttonSell.TabIndex = 16; + buttonSell.Text = "Продать"; + buttonSell.UseVisualStyleBackColor = true; + buttonSell.Click += buttonSell_Click; + // + // textBoxCount + // + textBoxCount.Location = new Point(111, 59); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(225, 27); + textBoxCount.TabIndex = 15; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(16, 66); + label2.Name = "label2"; + label2.Size = new Size(93, 20); + label2.TabIndex = 14; + label2.Text = "Количество:"; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(16, 33); + label1.Name = "label1"; + label1.Size = new Size(55, 20); + label1.TabIndex = 13; + label1.Text = "Пицца"; + // + // FormSellPizza + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(352, 145); + Controls.Add(comboBoxPizza); + Controls.Add(buttonCancel); + Controls.Add(buttonSell); + Controls.Add(textBoxCount); + Controls.Add(label2); + Controls.Add(label1); + Name = "FormSellPizza"; + Text = "Продажа"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox comboBoxPizza; + private Button buttonCancel; + private Button buttonSell; + private TextBox textBoxCount; + private Label label2; + private Label label1; + } +} \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormSellPizza.cs b/Pizzeria/Pizzeria/FormSellPizza.cs new file mode 100644 index 0000000..ed79b48 --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.cs @@ -0,0 +1,93 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BusinessLogicsContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; + +namespace Pizzeria +{ + public partial class FormSellPizza : Form + { + private readonly List? _list; + private readonly IShopLogic _shopLogic; + private readonly ILogger _logger; + + public int Id + { + get + { + return Convert.ToInt32(comboBoxPizza.SelectedValue); + } + set + { + comboBoxPizza.SelectedValue = value; + } + } + public IPizzaModel? PizzaModel + { + get + { + if (_list == null) + { + return null; + } + foreach (var elem in _list) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + public FormSellPizza(ILogger logger, IPizzaLogic pizzaLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _logger = logger; + _list = pizzaLogic.ReadList(null); + if (_list != null) + { + comboBoxPizza.DisplayMember = "PizzaName"; + comboBoxPizza.ValueMember = "Id"; + comboBoxPizza.DataSource = _list; + comboBoxPizza.SelectedItem = null; + } + _shopLogic = shopLogic; + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxPizza.SelectedValue == null) + { + MessageBox.Show("Выберите пиццу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + try + { + _shopLogic.sellPizza(PizzaModel, Convert.ToInt32(textBoxCount.Text)); + MessageBox.Show("Продано успешно!"); + } + catch (ArgumentException ex) + { + MessageBox.Show("Ошибка продажи. Слишком большое количество"); + _logger.LogWarning(ex, "Ошибка продажи"); + return; + } catch(Exception err) + { + MessageBox.Show("Ошибка продажи."); + _logger.LogError(err, "Ошибка продажи"); + return; + } + + DialogResult = DialogResult.OK; + Close(); + } + } +} diff --git a/Pizzeria/Pizzeria/FormSellPizza.resx b/Pizzeria/Pizzeria/FormSellPizza.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Pizzeria/Pizzeria/FormSellPizza.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Pizzeria/Pizzeria/FormShop.cs b/Pizzeria/Pizzeria/FormShop.cs index 9d0875f..e12d2ed 100644 --- a/Pizzeria/Pizzeria/FormShop.cs +++ b/Pizzeria/Pizzeria/FormShop.cs @@ -37,6 +37,7 @@ namespace Pizzeria textBoxName.Text = view.ShopName; textBoxAddres.Text = view.Addres.ToString(); dateTimePicker.Text = view.OpenTime.ToString(); + textBoxCapacity.Text = view.Capacity.ToString(); _shopPizzas = view.ShopPizzas ?? new Dictionary(); LoadData(); } diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index 7c53e58..36064ec 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -53,6 +53,7 @@ namespace Pizzeria services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs index 33acfb6..52c74e0 100644 --- a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs @@ -121,9 +121,17 @@ namespace PizzeriaBusinessLogic if(model.ShopPizzas.TryGetValue(pizza.Id, out var pair)) { + if(model.Capacity - pair.Item2 < count) + { + throw new ArgumentException("Количество добавляемого изделия слишком велико"); + } model.ShopPizzas[pizza.Id] = (pair.Item1, pair.Item2 + count); } else { + if (model.Capacity < count) + { + throw new ArgumentException("Количество добавляемого изделия слишком велико"); + } model.ShopPizzas.Add(pizza.Id, (pizza, count)); } @@ -131,11 +139,24 @@ namespace PizzeriaBusinessLogic { Id = model.Id, ShopName = model.ShopName, + OpenTime = model.OpenTime, Addres = model.Addres, ShopPizzas = model.ShopPizzas, }); return true; } + + public bool sellPizza(IPizzaModel pizza, int count) + { + bool result = _shopStorage.TrySellPizza(pizza, count); + + if(!result) + { + throw new ArgumentException("Количество продаваемого изделия слишком велико"); + } + + return result; + } } } diff --git a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs index d26bdf8..d3d4ae5 100644 --- a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IShopLogic.cs @@ -10,6 +10,7 @@ namespace PizzeriaContracts.BusinessLogicsContracts List? ReadList(ShopSearchModel? model); ShopViewModel? ReadElement(ShopSearchModel model); bool AddPizza(ShopSearchModel shop, IPizzaModel pizza, int count); + bool sellPizza(IPizzaModel pizza, int count); bool Create(ShopBindingModel model); bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs index 99cdc9c..ee44e2d 100644 --- a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using PizzeriaContracts.BindingModels; using PizzeriaContracts.SearchModels; using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; namespace PizzeriaContracts.StoragesContracts { @@ -9,6 +10,7 @@ namespace PizzeriaContracts.StoragesContracts List GetFullList(); List GetFilteredList(ShopSearchModel model); ShopViewModel? GetElement(ShopSearchModel model); + bool TrySellPizza(IPizzaModel pizza, int count); ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); diff --git a/Pizzeria/PizzeriaListImplement/Models/Shop.cs b/Pizzeria/PizzeriaListImplement/Models/Shop.cs index 1195166..178b228 100644 --- a/Pizzeria/PizzeriaListImplement/Models/Shop.cs +++ b/Pizzeria/PizzeriaListImplement/Models/Shop.cs @@ -10,6 +10,8 @@ namespace PizzeriaListImplement.Models public string ShopName { get; private set; } = string.Empty; public string Addres { get; private set; } = string.Empty; public DateTime OpenTime { get; private set; } = DateTime.Now; + public int Capacity { get; private set; } + public Dictionary ShopPizzas { @@ -29,6 +31,7 @@ namespace PizzeriaListImplement.Models ShopName = model.ShopName, Addres = model.Addres, OpenTime = model.OpenTime, + Capacity = model.Capacity, ShopPizzas = model.ShopPizzas }; } @@ -49,8 +52,10 @@ namespace PizzeriaListImplement.Models ShopName = ShopName, Addres = Addres, OpenTime = OpenTime, + Capacity = Capacity, ShopPizzas = ShopPizzas }; + } } diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs index 04f9a85..d5ab7d3 100644 --- a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using PizzeriaContracts.SearchModels; using PizzeriaContracts.StoragesContracts; using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; using PizzeriaFileImplement.models; namespace PizzeriaFileImplement.Implements @@ -41,6 +42,42 @@ namespace PizzeriaFileImplement.Implements (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } + + public bool TrySellPizza(IPizzaModel pizza, int count) + { + int hasCount = 0; + + source.Shops.ForEach(x => + { + if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair)) + { + hasCount += pair.Item2; + } + }); + + if (hasCount < count) return false; + + source.Shops.ForEach(x => + { + if (count < 0) return; + if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair)) + { + if(count >= pair.Item2) + { + count -= pair.Item2; + x.ShopPizzas[pizza.Id] = (pizza, 0); + } else + { + x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 - count); + count = 0; + } + } + }); + + source.SaveShops(); + return true; + } + public ShopViewModel? Insert(ShopBindingModel model) { model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs index 9af508f..fb23761 100644 --- a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs +++ b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs @@ -22,7 +22,7 @@ namespace PizzeriaFileImplement.models { var source = DataFileSingleton.GetInstance(); _shopPizzas = Pizzas.ToDictionary(x => x.Key, y => - ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!, + ((source.Pizzas.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!, y.Value)); } return _shopPizzas; -- 2.25.1 From 55dcc5a9a79b0689e557e16f2241acd5defe32c3 Mon Sep 17 00:00:00 2001 From: parap Date: Sat, 22 Apr 2023 20:55:10 +0400 Subject: [PATCH 5/6] lab 2 hard full --- Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs | 17 ++++- .../StoragesContracts/IShopStorage.cs | 1 + .../Implements/ShopStorage.cs | 68 ++++++++++++++++++- .../PizzeriaShopFileImplement/models/Shop.cs | 1 + 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs index f071a3f..fb51d68 100644 --- a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs @@ -12,10 +12,14 @@ namespace PizzeriaBusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopStorage _shopStorage; + private readonly IPizzaStorage _pizzaStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage, IPizzaStorage pizzaStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; + _pizzaStorage = pizzaStorage; } public List? ReadList(OrderSearchModel? model) { @@ -66,6 +70,17 @@ namespace PizzeriaBusinessLogic model.DateImplement = DateTime.Now; } + if (newStatus == OrderStatus.Готов) + { + var pizza = _pizzaStorage.GetElement(new PizzaSearchModel { Id = model.PizzaId }); + + if(!_shopStorage.TryRestoreShops(pizza, model.Count)) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Incorrect count."); + throw new ArgumentException("Количество изделий слишком велико"); + } + } + if (_orderStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs index ee44e2d..0c23f6b 100644 --- a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs @@ -11,6 +11,7 @@ namespace PizzeriaContracts.StoragesContracts List GetFilteredList(ShopSearchModel model); ShopViewModel? GetElement(ShopSearchModel model); bool TrySellPizza(IPizzaModel pizza, int count); + bool TryRestoreShops(IPizzaModel pizza, int count); ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs index d5ab7d3..9949785 100644 --- a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -43,6 +43,63 @@ namespace PizzeriaFileImplement.Implements (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } + public bool TryRestoreShops(IPizzaModel pizza, int count) + { + int freePlaces = 0; + + source.Shops.ForEach(x => + { + int currentFreePlaces = x.Capacity; + foreach (var pair in x.ShopPizzas) + { + currentFreePlaces -= pair.Value.Item2; + } + freePlaces += currentFreePlaces; + }); + + if (freePlaces < count) return false; + + source.Shops.ForEach(x => + { + int currentFreePlaces = x.Capacity; + foreach (var elem in x.ShopPizzas) + { + currentFreePlaces -= elem.Value.Item2; + } + + if(!x.ShopPizzas.TryGetValue(pizza.Id, out var check)) + { + x.ShopPizzas.Add(pizza.Id, (pizza, 0)); + } + + x.ShopPizzas.TryGetValue(pizza.Id, out var pair); + + if (count >= currentFreePlaces) + { + count -= currentFreePlaces; + x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + currentFreePlaces); + } + else + { + x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + count); + count = 0; + } + + x.Update(new ShopBindingModel + { + Id = x.Id, + Addres = x.Addres, + Capacity = x.Capacity, + OpenTime = x.OpenTime, + ShopName = x.ShopName, + ShopPizzas = x.ShopPizzas + }); + }); + + source.SaveShops(); + return true; + } + public bool TrySellPizza(IPizzaModel pizza, int count) { int hasCount = 0; @@ -59,7 +116,6 @@ namespace PizzeriaFileImplement.Implements source.Shops.ForEach(x => { - if (count < 0) return; if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair)) { if(count >= pair.Item2) @@ -71,6 +127,16 @@ namespace PizzeriaFileImplement.Implements x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 - count); count = 0; } + + x.Update(new ShopBindingModel + { + Id = x.Id, + Addres = x.Addres, + Capacity = x.Capacity, + OpenTime = x.OpenTime, + ShopName = x.ShopName, + ShopPizzas = x.ShopPizzas + }); } }); diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs index fb23761..7ce5d9e 100644 --- a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs +++ b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs @@ -74,6 +74,7 @@ namespace PizzeriaFileImplement.models ShopName = model.ShopName; Addres = model.Addres; OpenTime = model.OpenTime; + Capacity = model.Capacity; Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2); _shopPizzas = null; } -- 2.25.1 From d4b269564b9ddbc31aa040b33280ba8412e4de49 Mon Sep 17 00:00:00 2001 From: parap Date: Sun, 23 Apr 2023 20:52:05 +0400 Subject: [PATCH 6/6] refactoring --- Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs | 41 +++++++++++-- Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs | 1 + .../StoragesContracts/IShopStorage.cs | 1 - .../Implements/ShopStorage.cs | 58 ------------------- 4 files changed, 38 insertions(+), 63 deletions(-) diff --git a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs index fb51d68..66ca639 100644 --- a/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/OrderLogic.cs @@ -12,13 +12,13 @@ namespace PizzeriaBusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; private readonly IPizzaStorage _pizzaStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage, IPizzaStorage pizzaStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IPizzaStorage pizzaStorage) { _logger = logger; _orderStorage = orderStorage; - _shopStorage = shopStorage; + _shopLogic = shopLogic; _pizzaStorage = pizzaStorage; } public List? ReadList(OrderSearchModel? model) @@ -74,7 +74,7 @@ namespace PizzeriaBusinessLogic { var pizza = _pizzaStorage.GetElement(new PizzaSearchModel { Id = model.PizzaId }); - if(!_shopStorage.TryRestoreShops(pizza, model.Count)) + if(!TryRestoreShops(pizza, model.Count)) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Incorrect count."); throw new ArgumentException("Количество изделий слишком велико"); @@ -124,5 +124,38 @@ namespace PizzeriaBusinessLogic _logger.LogInformation("Sum:{ Sum}. Id: { Id}", model.Sum, model.Id); } + private bool TryRestoreShops(IPizzaModel pizza, int count) + { + int freePlaces = 0; + + _shopLogic.ReadList(null).ForEach(x => + { + int currentFreePlaces = x.Capacity; + foreach (var pair in x.ShopPizzas) + { + currentFreePlaces -= pair.Value.Item2; + } + freePlaces += currentFreePlaces; + }); + + if (freePlaces < count) return false; + + _shopLogic.ReadList(null).ForEach(x => + { + if (count <= 0) return; + + int currentFreePlaces = x.Capacity; + foreach (var elem in x.ShopPizzas) + { + currentFreePlaces -= elem.Value.Item2; + } + + _shopLogic.AddPizza(new ShopSearchModel { Id = x.Id }, pizza, Math.Min(currentFreePlaces, count)); + count -= currentFreePlaces; + }); + + return true; + } + } } diff --git a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs index 52c74e0..bf74f7a 100644 --- a/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs +++ b/Pizzeria/PizzeriaBusinessLogic/ShopLogic.cs @@ -141,6 +141,7 @@ namespace PizzeriaBusinessLogic ShopName = model.ShopName, OpenTime = model.OpenTime, Addres = model.Addres, + Capacity = model.Capacity, ShopPizzas = model.ShopPizzas, }); diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs index 0c23f6b..ee44e2d 100644 --- a/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IShopStorage.cs @@ -11,7 +11,6 @@ namespace PizzeriaContracts.StoragesContracts List GetFilteredList(ShopSearchModel model); ShopViewModel? GetElement(ShopSearchModel model); bool TrySellPizza(IPizzaModel pizza, int count); - bool TryRestoreShops(IPizzaModel pizza, int count); ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs index 9949785..20080cf 100644 --- a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -42,64 +42,6 @@ namespace PizzeriaFileImplement.Implements (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } - - public bool TryRestoreShops(IPizzaModel pizza, int count) - { - int freePlaces = 0; - - source.Shops.ForEach(x => - { - int currentFreePlaces = x.Capacity; - foreach (var pair in x.ShopPizzas) - { - currentFreePlaces -= pair.Value.Item2; - } - freePlaces += currentFreePlaces; - }); - - if (freePlaces < count) return false; - - source.Shops.ForEach(x => - { - int currentFreePlaces = x.Capacity; - foreach (var elem in x.ShopPizzas) - { - currentFreePlaces -= elem.Value.Item2; - } - - if(!x.ShopPizzas.TryGetValue(pizza.Id, out var check)) - { - x.ShopPizzas.Add(pizza.Id, (pizza, 0)); - } - - x.ShopPizzas.TryGetValue(pizza.Id, out var pair); - - if (count >= currentFreePlaces) - { - count -= currentFreePlaces; - x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + currentFreePlaces); - } - else - { - x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + count); - count = 0; - } - - x.Update(new ShopBindingModel - { - Id = x.Id, - Addres = x.Addres, - Capacity = x.Capacity, - OpenTime = x.OpenTime, - ShopName = x.ShopName, - ShopPizzas = x.ShopPizzas - }); - }); - - source.SaveShops(); - return true; - } - public bool TrySellPizza(IPizzaModel pizza, int count) { int hasCount = 0; -- 2.25.1