From e90f9d47cc10e75a3a2b8a2a4878e32711df30a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 03:21:09 +0400 Subject: [PATCH 01/17] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D0=B2=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/Component.cs | 7 ++ .../ConfectioneryFileImplement.csproj | 9 +++ .../DataFileSingleton.cs | 52 +++++++++++++ ConfectionaryFileImplement/Order.cs | 76 +++++++++++++++++++ ConfectionaryFileImplement/Pastry.cs | 12 +++ Confectionery.sln | 10 ++- 6 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 ConfectionaryFileImplement/Component.cs create mode 100644 ConfectionaryFileImplement/ConfectioneryFileImplement.csproj create mode 100644 ConfectionaryFileImplement/DataFileSingleton.cs create mode 100644 ConfectionaryFileImplement/Order.cs create mode 100644 ConfectionaryFileImplement/Pastry.cs diff --git a/ConfectionaryFileImplement/Component.cs b/ConfectionaryFileImplement/Component.cs new file mode 100644 index 0000000..f24745d --- /dev/null +++ b/ConfectionaryFileImplement/Component.cs @@ -0,0 +1,7 @@ +namespace ConfectionaryFileImplement +{ + public class Class1 + { + + } +} \ No newline at end of file diff --git a/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/ConfectionaryFileImplement/DataFileSingleton.cs b/ConfectionaryFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..27c597d --- /dev/null +++ b/ConfectionaryFileImplement/DataFileSingleton.cs @@ -0,0 +1,52 @@ +using ConfectioneryFileImplement.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string PastryFileName = "Pastry.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Pastrys { 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 SavePastrys() => SaveData(Pastrys, PastryFileName, "Pastrys", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x + => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => + Component.Create(x)!)!; + Pastrys = LoadData(PastryFileName, "Pastry", x => + Pastry.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/ConfectionaryFileImplement/Order.cs b/ConfectionaryFileImplement/Order.cs new file mode 100644 index 0000000..66eed4f --- /dev/null +++ b/ConfectionaryFileImplement/Order.cs @@ -0,0 +1,76 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int PastryId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } + + public DateTime DateCreate { get; private set; } + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + PastryId = model.PastryId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + PastryId = model.PastryId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + Id = model.Id; + } + public OrderViewModel GetViewModel => new() + { + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("PastryId", PastryId), + new XElement("Count", Count), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status), + new XElement("DateCreate", DateCreate), + new XElement("DateImplement", DateImplement) + ); + } +} diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs new file mode 100644 index 0000000..7baf7e7 --- /dev/null +++ b/ConfectionaryFileImplement/Pastry.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectionaryFileImplement +{ + internal class Pastry + { + } +} diff --git a/Confectionery.sln b/Confectionery.sln index 91ace29..108d8a2 100644 --- a/Confectionery.sln +++ b/Confectionery.sln @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryContracts", "ConfectioneryContracts\ConfectioneryContracts.csproj", "{28EC043D-88E8-48DA-B5E8-2DE5659EB1BD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectionaryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{82EF78A8-98EC-490A-8D66-66909E5431E2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic", "ConfectionaryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{82EF78A8-98EC-490A-8D66-66909E5431E2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement", "ConfectionaryListImplement\ConfectioneryListImplement.csproj", "{0A7B118B-9B7C-4796-9846-66026159723E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectionaryListImplement\ConfectioneryListImplement.csproj", "{0A7B118B-9B7C-4796-9846-66026159723E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectionaryFileImplement\ConfectioneryFileImplement.csproj", "{47A2EA59-4443-487E-85F4-AC49C04B7211}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {0A7B118B-9B7C-4796-9846-66026159723E}.Debug|Any CPU.Build.0 = Debug|Any CPU {0A7B118B-9B7C-4796-9846-66026159723E}.Release|Any CPU.ActiveCfg = Release|Any CPU {0A7B118B-9B7C-4796-9846-66026159723E}.Release|Any CPU.Build.0 = Release|Any CPU + {47A2EA59-4443-487E-85F4-AC49C04B7211}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {47A2EA59-4443-487E-85F4-AC49C04B7211}.Debug|Any CPU.Build.0 = Debug|Any CPU + {47A2EA59-4443-487E-85F4-AC49C04B7211}.Release|Any CPU.ActiveCfg = Release|Any CPU + {47A2EA59-4443-487E-85F4-AC49C04B7211}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- 2.25.1 From 2a0c31d5c09e581a5191bb679092693375c4dda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 03:53:54 +0400 Subject: [PATCH 02/17] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D1=84=D0=B5=D0=B9=D1=81=D1=8B=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=BE=D0=B2=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89?= =?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 --- .../ComponentStorage.cs | 79 ++++++++++++++++++ .../DataFileSingleton.cs | 14 ++-- ConfectionaryFileImplement/OrderStorage.cs | 12 +++ ConfectionaryFileImplement/PastryStorage.cs | 81 +++++++++++++++++++ 4 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 ConfectionaryFileImplement/ComponentStorage.cs create mode 100644 ConfectionaryFileImplement/OrderStorage.cs create mode 100644 ConfectionaryFileImplement/PastryStorage.cs diff --git a/ConfectionaryFileImplement/ComponentStorage.cs b/ConfectionaryFileImplement/ComponentStorage.cs new file mode 100644 index 0000000..2293b9a --- /dev/null +++ b/ConfectionaryFileImplement/ComponentStorage.cs @@ -0,0 +1,79 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; + +namespace ConfectioneryFileImplement.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; + } + } +} \ No newline at end of file diff --git a/ConfectionaryFileImplement/DataFileSingleton.cs b/ConfectionaryFileImplement/DataFileSingleton.cs index 27c597d..71de12c 100644 --- a/ConfectionaryFileImplement/DataFileSingleton.cs +++ b/ConfectionaryFileImplement/DataFileSingleton.cs @@ -11,7 +11,8 @@ namespace ConfectioneryFileImplement private readonly string PastryFileName = "Pastry.xml"; public List Components { get; private set; } public List Orders { get; private set; } - public List Pastrys { get; private set; } + public List Pastries { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) @@ -21,14 +22,14 @@ namespace ConfectioneryFileImplement return instance; } public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); - public void SavePastrys() => SaveData(Pastrys, PastryFileName, "Pastrys", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x - => x.GetXElement); + public void SavePastries() => SaveData(Pastries, PastryFileName, "Pastries", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - Pastrys = LoadData(PastryFileName, "Pastry", x => + Pastries = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } @@ -44,8 +45,7 @@ namespace ConfectioneryFileImplement { if (data != null) { - new XDocument(new XElement(xmlNodeName, - data.Select(selectFunction).ToArray())).Save(filename); + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); } } } diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs new file mode 100644 index 0000000..5b79348 --- /dev/null +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement +{ + internal class OrderStorage + { + } +} diff --git a/ConfectionaryFileImplement/PastryStorage.cs b/ConfectionaryFileImplement/PastryStorage.cs new file mode 100644 index 0000000..9fbdedd --- /dev/null +++ b/ConfectionaryFileImplement/PastryStorage.cs @@ -0,0 +1,81 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement +{ + public class PastryStorage : IPastryStorage + { + private readonly DataFileSingleton _source; + public PastryStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public PastryViewModel? Delete(PastryBindingModel model) + { + var element = _source.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + _source.Pastries.Remove(element); + _source.Savepastrys(); + return element.GetViewModel; + } + return null; + } + + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue) + { + return null; + } + return _source.Pastries.FirstOrDefault + (x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == model.Id) + )?.GetViewModel; + } + + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + return _source.Pastries + .Select(x => x.GetViewModel) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList(); + } + + public List GetFullList() + { + return _source.Pastries + .Select(x => x.GetViewModel) + .ToList(); + } + + public PastryViewModel? Insert(PastryBindingModel model) + { + throw new NotImplementedException(); + } + + public PastryViewModel? Update(PastryBindingModel model) + { + var pastry = _source.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + _source.SavePastries(); + return pastry.GetViewModel; + } + } +} -- 2.25.1 From 9e8a3d4e872fc7601102f7d6e8a1a76410d13183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 04:03:18 +0400 Subject: [PATCH 03/17] =?UTF-8?q?=D0=92=D0=BD=D0=B5=D0=B4=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B2=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/DataFileSingleton.cs | 6 ++---- Confectionery/ConfectioneryView.csproj | 1 + Confectionery/Program.cs | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/ConfectionaryFileImplement/DataFileSingleton.cs b/ConfectionaryFileImplement/DataFileSingleton.cs index 71de12c..ff056a2 100644 --- a/ConfectionaryFileImplement/DataFileSingleton.cs +++ b/ConfectionaryFileImplement/DataFileSingleton.cs @@ -27,10 +27,8 @@ namespace ConfectioneryFileImplement private DataFileSingleton() { - Components = LoadData(ComponentFileName, "Component", x => - Component.Create(x)!)!; - Pastries = LoadData(PastryFileName, "Pastry", x => - Pastry.Create(x)!)!; + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Pastries = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/Confectionery/ConfectioneryView.csproj b/Confectionery/ConfectioneryView.csproj index 1635e6d..4947544 100644 --- a/Confectionery/ConfectioneryView.csproj +++ b/Confectionery/ConfectioneryView.csproj @@ -15,6 +15,7 @@ + diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index 77b8767..bd6722d 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -1,8 +1,8 @@ -using ConfectioneryListImplement.Implements; +using ConfectioneryFileImplement.Implements; using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; -using ConfectioneryListImplement; +using ConfectioneryFileImplement; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 3a777a93df2c59bcbce4933aa6eaa0715d8a3faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 04:07:03 +0400 Subject: [PATCH 04/17] fix --- ConfectionaryFileImplement/Component.cs | 61 ++++++++++++- .../ConfectioneryFileImplement.csproj | 5 + ConfectionaryFileImplement/Order.cs | 22 ++++- ConfectionaryFileImplement/OrderStorage.cs | 76 ++++++++++++++-- ConfectionaryFileImplement/Pastry.cs | 91 +++++++++++++++++-- ConfectionaryFileImplement/PastryStorage.cs | 18 ++-- 6 files changed, 246 insertions(+), 27 deletions(-) diff --git a/ConfectionaryFileImplement/Component.cs b/ConfectionaryFileImplement/Component.cs index f24745d..91da900 100644 --- a/ConfectionaryFileImplement/Component.cs +++ b/ConfectionaryFileImplement/Component.cs @@ -1,7 +1,60 @@ -namespace ConfectionaryFileImplement -{ - public class Class1 - { +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); } } \ No newline at end of file diff --git a/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj index 132c02c..41b6c3e 100644 --- a/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj +++ b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/ConfectionaryFileImplement/Order.cs b/ConfectionaryFileImplement/Order.cs index 66eed4f..ce5c0ed 100644 --- a/ConfectionaryFileImplement/Order.cs +++ b/ConfectionaryFileImplement/Order.cs @@ -4,7 +4,7 @@ using ConfectioneryDataModels.Enums; using ConfectioneryDataModels.Models; using System.Xml.Linq; -namespace ConfectioneryFileImplement +namespace ConfectioneryFileImplement.Models { public class Order : IOrderModel { @@ -39,6 +39,24 @@ namespace ConfectioneryFileImplement Id = model.Id, }; } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")), + Sum = Convert.ToDouble(element.Element("Sum")), + Count = Convert.ToInt32(element.Element("Count")), + Status = (OrderStatus)Convert.ToInt32(element.Element("Status")), + PastryId = Convert.ToInt32(element.Element("PastryId")), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")), + DateImplement = Convert.ToDateTime(element.Element("DateImplement")), + }; + } + public void Update(OrderBindingModel? model) { if (model == null) @@ -68,7 +86,7 @@ namespace ConfectioneryFileImplement new XElement("PastryId", PastryId), new XElement("Count", Count), new XElement("Sum", Sum.ToString()), - new XElement("Status", Status), + new XElement("Status", (int)Status), new XElement("DateCreate", DateCreate), new XElement("DateImplement", DateImplement) ); diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs index 5b79348..aae2076 100644 --- a/ConfectionaryFileImplement/OrderStorage.cs +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -1,12 +1,76 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; namespace ConfectioneryFileImplement { - internal class OrderStorage + public class OrderStorage : IOrderStorage { + private readonly DataFileSingleton _source; + public OrderStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + 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; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return _source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = GetElement(model); + return result != null ? new() { result } : new(); + } + + public List GetFullList() + { + return _source.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + + 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 pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + _source.SaveOrders(); + return pastry.GetViewModel; + } } } diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index 7baf7e7..f32067c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,12 +1,87 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; -namespace ConfectionaryFileImplement +namespace ConfectioneryFileImplement.Models { - internal class Pastry + public class Pastry : IPastryModel { + public int Id { get; private set; } + public string PastryName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _PastryComponents = null; + public Dictionary PastryComponents + { + get + { + if (_PastryComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _PastryComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _PastryComponents; + } + } + public static Pastry? Create(PastryBindingModel model) + { + if (model == null) + { + return null; + } + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Pastry? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pastry() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PastryName = element.Element("PastryName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PastryComponents")!.Elements("PastryComponent").ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PastryBindingModel model) + { + if (model == null) + { + return; + } + PastryName = model.PastryName; + Price = model.Price; + Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _PastryComponents = null; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + public XElement GetXElement => new("Pastry", + new XAttribute("Id", Id), + new XElement("PastryName", PastryName), + new XElement("Price", Price.ToString()), + new XElement("PastryComponents", Components.Select(x => + new XElement("PastryComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } -} +} \ No newline at end of file diff --git a/ConfectionaryFileImplement/PastryStorage.cs b/ConfectionaryFileImplement/PastryStorage.cs index 9fbdedd..4e03c2c 100644 --- a/ConfectionaryFileImplement/PastryStorage.cs +++ b/ConfectionaryFileImplement/PastryStorage.cs @@ -2,11 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryFileImplement.Models; namespace ConfectioneryFileImplement { @@ -24,7 +20,7 @@ namespace ConfectioneryFileImplement if (element != null) { _source.Pastries.Remove(element); - _source.Savepastrys(); + _source.SavePastries(); return element.GetViewModel; } return null; @@ -63,7 +59,15 @@ namespace ConfectioneryFileImplement public PastryViewModel? Insert(PastryBindingModel model) { - throw new NotImplementedException(); + model.Id = _source.Pastries.Count > 0 ? _source.Pastries.Max(x => x.Id) + 1 : 1; + var newPastry = Pastry.Create(model); + if (newPastry == null) + { + return null; + } + _source.Pastries.Add(newPastry); + _source.SavePastries(); + return newPastry.GetViewModel; } public PastryViewModel? Update(PastryBindingModel model) -- 2.25.1 From a83bd28099cc9837adf46edaf0f879a19b09f81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 04:07:03 +0400 Subject: [PATCH 05/17] fix --- ConfectionaryFileImplement/Component.cs | 61 ++++++++++++- .../ConfectioneryFileImplement.csproj | 5 + ConfectionaryFileImplement/Order.cs | 23 ++++- ConfectionaryFileImplement/OrderStorage.cs | 76 ++++++++++++++-- ConfectionaryFileImplement/Pastry.cs | 91 +++++++++++++++++-- ConfectionaryFileImplement/PastryStorage.cs | 18 ++-- 6 files changed, 247 insertions(+), 27 deletions(-) diff --git a/ConfectionaryFileImplement/Component.cs b/ConfectionaryFileImplement/Component.cs index f24745d..91da900 100644 --- a/ConfectionaryFileImplement/Component.cs +++ b/ConfectionaryFileImplement/Component.cs @@ -1,7 +1,60 @@ -namespace ConfectionaryFileImplement -{ - public class Class1 - { +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); } } \ No newline at end of file diff --git a/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj index 132c02c..41b6c3e 100644 --- a/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj +++ b/ConfectionaryFileImplement/ConfectioneryFileImplement.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/ConfectionaryFileImplement/Order.cs b/ConfectionaryFileImplement/Order.cs index 66eed4f..d3067f1 100644 --- a/ConfectionaryFileImplement/Order.cs +++ b/ConfectionaryFileImplement/Order.cs @@ -4,7 +4,7 @@ using ConfectioneryDataModels.Enums; using ConfectioneryDataModels.Models; using System.Xml.Linq; -namespace ConfectioneryFileImplement +namespace ConfectioneryFileImplement.Models { public class Order : IOrderModel { @@ -39,6 +39,25 @@ namespace ConfectioneryFileImplement Id = model.Id, }; } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + var dateImplement = element.Element("DateImplement")!.Value; + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value), + PastryId = Convert.ToInt32(element.Element("PastryId")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement), + }; + } + public void Update(OrderBindingModel? model) { if (model == null) @@ -68,7 +87,7 @@ namespace ConfectioneryFileImplement new XElement("PastryId", PastryId), new XElement("Count", Count), new XElement("Sum", Sum.ToString()), - new XElement("Status", Status), + new XElement("Status", (int)Status), new XElement("DateCreate", DateCreate), new XElement("DateImplement", DateImplement) ); diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs index 5b79348..aae2076 100644 --- a/ConfectionaryFileImplement/OrderStorage.cs +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -1,12 +1,76 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; namespace ConfectioneryFileImplement { - internal class OrderStorage + public class OrderStorage : IOrderStorage { + private readonly DataFileSingleton _source; + public OrderStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + 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; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return _source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = GetElement(model); + return result != null ? new() { result } : new(); + } + + public List GetFullList() + { + return _source.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + + 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 pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + _source.SaveOrders(); + return pastry.GetViewModel; + } } } diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index 7baf7e7..f32067c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,12 +1,87 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; -namespace ConfectionaryFileImplement +namespace ConfectioneryFileImplement.Models { - internal class Pastry + public class Pastry : IPastryModel { + public int Id { get; private set; } + public string PastryName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _PastryComponents = null; + public Dictionary PastryComponents + { + get + { + if (_PastryComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _PastryComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _PastryComponents; + } + } + public static Pastry? Create(PastryBindingModel model) + { + if (model == null) + { + return null; + } + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Pastry? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pastry() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PastryName = element.Element("PastryName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PastryComponents")!.Elements("PastryComponent").ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PastryBindingModel model) + { + if (model == null) + { + return; + } + PastryName = model.PastryName; + Price = model.Price; + Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _PastryComponents = null; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + public XElement GetXElement => new("Pastry", + new XAttribute("Id", Id), + new XElement("PastryName", PastryName), + new XElement("Price", Price.ToString()), + new XElement("PastryComponents", Components.Select(x => + new XElement("PastryComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } -} +} \ No newline at end of file diff --git a/ConfectionaryFileImplement/PastryStorage.cs b/ConfectionaryFileImplement/PastryStorage.cs index 9fbdedd..4e03c2c 100644 --- a/ConfectionaryFileImplement/PastryStorage.cs +++ b/ConfectionaryFileImplement/PastryStorage.cs @@ -2,11 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryFileImplement.Models; namespace ConfectioneryFileImplement { @@ -24,7 +20,7 @@ namespace ConfectioneryFileImplement if (element != null) { _source.Pastries.Remove(element); - _source.Savepastrys(); + _source.SavePastries(); return element.GetViewModel; } return null; @@ -63,7 +59,15 @@ namespace ConfectioneryFileImplement public PastryViewModel? Insert(PastryBindingModel model) { - throw new NotImplementedException(); + model.Id = _source.Pastries.Count > 0 ? _source.Pastries.Max(x => x.Id) + 1 : 1; + var newPastry = Pastry.Create(model); + if (newPastry == null) + { + return null; + } + _source.Pastries.Add(newPastry); + _source.SavePastries(); + return newPastry.GetViewModel; } public PastryViewModel? Update(PastryBindingModel model) -- 2.25.1 From 1d77f16d44178a98b9eec78396a21188312f88bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 05:01:38 +0400 Subject: [PATCH 06/17] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 4 + ConfectionaryFileImplement/Shop.cs | 103 +++++++++++++++++ ConfectionaryFileImplement/ShopStorage.cs | 107 ++++++++++++++++++ Confectionery/Program.cs | 4 +- 4 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 ConfectionaryFileImplement/Shop.cs create mode 100644 ConfectionaryFileImplement/ShopStorage.cs diff --git a/ConfectionaryFileImplement/DataFileSingleton.cs b/ConfectionaryFileImplement/DataFileSingleton.cs index ff056a2..ac19587 100644 --- a/ConfectionaryFileImplement/DataFileSingleton.cs +++ b/ConfectionaryFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace ConfectioneryFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PastryFileName = "Pastry.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pastries { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { @@ -24,12 +26,14 @@ namespace ConfectioneryFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePastries() => SaveData(Pastries, PastryFileName, "Pastries", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Pastries = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs new file mode 100644 index 0000000..39f14be --- /dev/null +++ b/ConfectionaryFileImplement/Shop.cs @@ -0,0 +1,103 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement +{ + public class Shop : IShopModel + { + public string Name { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary CountPastries { get; private set; } = new(); + + private Dictionary? _cachedPastries = null; + public Dictionary Pastries + { + get + { + if (_cachedPastries == null) + { + var source = DataFileSingleton.GetInstance(); + _cachedPastries = CountPastries + .ToDictionary(x => x.Key, x => (source.Pastries + .FirstOrDefault(y => y.Id == x.Key)! as IPastryModel, x.Value)); + } + return _cachedPastries; + } + } + + public int Id { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + DateOpening = model.DateOpening, + CountPastries = new() + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + CountPastries = element.Element("CountPastries")!.Elements("CountPastry") + .ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2); + _cachedPastries = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + Pastries = Pastries, + DateOpening = DateOpening, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("Name", Name), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening), + new XElement("CountPastries", CountPastries + .Select(x => new XElement("CountPastry", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + )) + ); + } +} diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs new file mode 100644 index 0000000..6ae843a --- /dev/null +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -0,0 +1,107 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryFileImplement +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton _source; + public ShopStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && + shop.Name == model.Name) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.Name.Contains(model.Name ?? string.Empty)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; + } + } +} diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index f9437a4..6f0ea6d 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -1,8 +1,8 @@ -using ConfectioneryListImplement.Implements; +using ConfectioneryFileImplement.Implements; using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; -using ConfectioneryListImplement; +using ConfectioneryFileImplement; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 771985a191c706a80352cefd7f1fe30562c16c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 05:47:23 +0400 Subject: [PATCH 07/17] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BB=D0=B5=20=D0=B2=20=D1=81?= =?UTF-8?q?=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20"=D0=9C=D0=B0?= =?UTF-8?q?=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 6 ++++++ ConfectionaryFileImplement/Pastry.cs | 1 - ConfectionaryFileImplement/Shop.cs | 17 ++++++++++++----- ConfectionaryListImplement/Shop.cs | 5 +++++ .../BindingModels/ShopBindingModel.cs | 2 ++ .../ViewModels/ShopViewModel.cs | 4 ++++ ConfectioneryDataModels/IShopModel.cs | 1 + 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 36d618a..d127dfa 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -133,6 +133,12 @@ namespace ConfectioneryBusinessLogic } _logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); + if (element.MaxCountPastries < 0) + { + throw new ArgumentException( + "Максимальное количество изделий в магазине не должно быть отрицательным", + nameof(element.MaxCountPastries)); + } if (element.Pastries.TryGetValue(pastry.Id, out var pair)) { diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index f32067c..6138c0c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,7 +1,6 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels.Models; -using ConfectioneryDataModels.Models; using System.Xml.Linq; namespace ConfectioneryFileImplement.Models diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs index 39f14be..fadd63f 100644 --- a/ConfectionaryFileImplement/Shop.cs +++ b/ConfectionaryFileImplement/Shop.cs @@ -12,6 +12,8 @@ namespace ConfectioneryFileImplement public string Address { get; private set; } = string.Empty; + public int MaxCountPastries { get; private set; } + public DateTime DateOpening { get; private set; } public Dictionary CountPastries { get; private set; } = new(); @@ -46,6 +48,7 @@ namespace ConfectioneryFileImplement Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, + MaxCountPastries = model.MaxCountPastries, CountPastries = new() }; } @@ -57,11 +60,12 @@ namespace ConfectioneryFileImplement } return new() { - Id = Convert.ToInt32(element.Attribute("Id")!.Value), - Name = element.Element("Name")!.Value, - Address = element.Element("Address")!.Value, - DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), - CountPastries = element.Element("CountPastries")!.Elements("CountPastry") + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + MaxCountPastries = Convert.ToInt32(element.Element("MaxCountPastries")!.Value), + CountPastries = element.Element("CountPastries")!.Elements("CountPastry") .ToDictionary( x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value) @@ -77,6 +81,7 @@ namespace ConfectioneryFileImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; + MaxCountPastries = model.MaxCountPastries; CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2); _cachedPastries = null; } @@ -87,12 +92,14 @@ namespace ConfectioneryFileImplement Address = Address, Pastries = Pastries, DateOpening = DateOpening, + MaxCountPastries = MaxCountPastries, }; public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("Name", Name), new XElement("Address", Address), new XElement("DateOpening", DateOpening), + new XElement("MaxCountPastries", MaxCountPastries), new XElement("CountPastries", CountPastries .Select(x => new XElement("CountPastry", new XElement("Key", x.Key), diff --git a/ConfectionaryListImplement/Shop.cs b/ConfectionaryListImplement/Shop.cs index 93e754c..42b553d 100644 --- a/ConfectionaryListImplement/Shop.cs +++ b/ConfectionaryListImplement/Shop.cs @@ -11,6 +11,8 @@ namespace ConfectioneryListImplement public string Address { get; private set; } = string.Empty; + public int MaxCountPastries { get; private set; } + public DateTime DateOpening { get; private set; } public Dictionary Pastries @@ -33,6 +35,7 @@ namespace ConfectioneryListImplement Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, + MaxCountPastries = model.MaxCountPastries, Pastries = new() }; } @@ -45,6 +48,7 @@ namespace ConfectioneryListImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; + MaxCountPastries = model.MaxCountPastries; Pastries = model.Pastries; } public ShopViewModel GetViewModel => new() @@ -54,6 +58,7 @@ namespace ConfectioneryListImplement Address = Address, Pastries = Pastries, DateOpening = DateOpening, + MaxCountPastries = MaxCountPastries, }; } } diff --git a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs index 38d8750..f627224 100644 --- a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs +++ b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs @@ -9,6 +9,8 @@ namespace ConfectioneryContracts.BindingModels public string Address { get; set; } = string.Empty; + public int MaxCountPastries { get; set; } + public DateTime DateOpening { get; set; } = DateTime.Now; public Dictionary Pastries diff --git a/ConfectioneryContracts/ViewModels/ShopViewModel.cs b/ConfectioneryContracts/ViewModels/ShopViewModel.cs index a87ca8d..f7fce9b 100644 --- a/ConfectioneryContracts/ViewModels/ShopViewModel.cs +++ b/ConfectioneryContracts/ViewModels/ShopViewModel.cs @@ -12,6 +12,9 @@ namespace ConfectioneryContracts.ViewModels [DisplayName("Адрес магазина")] public string Address { get; set; } = string.Empty; + [DisplayName("Максимальное количество изделий в магазине")] + public int MaxCountPastries { get; set; } + [DisplayName("Время открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; @@ -22,5 +25,6 @@ namespace ConfectioneryContracts.ViewModels } = new(); public int Id { get; set; } + } } diff --git a/ConfectioneryDataModels/IShopModel.cs b/ConfectioneryDataModels/IShopModel.cs index c0883e9..31936ea 100644 --- a/ConfectioneryDataModels/IShopModel.cs +++ b/ConfectioneryDataModels/IShopModel.cs @@ -6,6 +6,7 @@ namespace ConfectioneryDataModels { string Name { get; } string Address { get; } + int MaxCountPastries { get; } DateTime DateOpening { get; } Dictionary Pastries { get; } } -- 2.25.1 From 4368d4d6ccec34caff488d082a7201848c470f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 06:34:44 +0400 Subject: [PATCH 08/17] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/ShopStorage.cs | 99 ++++++++++--------- ConfectionaryListImplement/ShopStorage.cs | 11 +++ .../StoragesContract/IShopStorage.cs | 4 + 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 6ae843a..3e9d4da 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement { @@ -15,93 +16,97 @@ namespace ConfectioneryFileImplement public ShopViewModel? Delete(ShopBindingModel model) { - for (int i = 0; i < _source.Shops.Count; ++i) + var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) { - if (_source.Shops[i].Id == model.Id) - { - var element = _source.Shops[i]; - _source.Shops.RemoveAt(i); - return element.GetViewModel; - } + _source.Shops.Remove(element); + _source.SaveShops(); + return element.GetViewModel; } return null; } public ShopViewModel? GetElement(ShopSearchModel model) { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + if (!model.Id.HasValue) { return null; } - foreach (var shop in _source.Shops) - { - if ((!string.IsNullOrEmpty(model.Name) && - shop.Name == model.Name) || - (model.Id.HasValue && shop.Id == model.Id)) - { - return shop.GetViewModel; - } - } - return null; + return _source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } public List GetFilteredList(ShopSearchModel model) { - var result = new List(); if (string.IsNullOrEmpty(model.Name)) { - return result; + return new(); } - foreach (var shop in _source.Shops) - { - if (shop.Name.Contains(model.Name ?? string.Empty)) - { - result.Add(shop.GetViewModel); - } - } - return result; + return _source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); } public List GetFullList() { - var result = new List(); - foreach (var shop in _source.Shops) - { - result.Add(shop.GetViewModel); - } - return result; + return _source.Shops + .Select(shop => shop.GetViewModel) + .ToList(); } public ShopViewModel? Insert(ShopBindingModel model) { - model.Id = 1; - foreach (var shop in _source.Shops) - { - if (model.Id <= shop.Id) - { - model.Id = shop.Id + 1; - } - } + 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.SaveShops(); return newShop.GetViewModel; } public ShopViewModel? Update(ShopBindingModel model) { - foreach (var shop in _source.Shops) + var shop = _source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) { - if (shop.Id == model.Id) + return null; + } + shop.Update(model); + _source.SaveShops(); + return shop.GetViewModel; + } + + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + var resultCount = _source.Shops + .Select(shop => shop.Pastries + .FirstOrDefault(x => x.Key == pastry.Id).Value.Item2) + .Sum(); + return resultCount >= needCount; + } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + if (!HasNeedPastries(pastry, needCount)) + { + return false; + } + foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + { + var tuple = pair.Pastries[pastry.Id]; + var diff = Math.Min(tuple.Item2, needCount); + pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; + if (needCount <= 0) { - shop.Update(model); - return shop.GetViewModel; + return true; } } - return null; + + return true; } } } diff --git a/ConfectionaryListImplement/ShopStorage.cs b/ConfectionaryListImplement/ShopStorage.cs index 893b6ad..c9ff5c3 100644 --- a/ConfectionaryListImplement/ShopStorage.cs +++ b/ConfectionaryListImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryListImplement { @@ -72,6 +73,11 @@ namespace ConfectioneryListImplement return result; } + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + throw new NotImplementedException(); + } + public ShopViewModel? Insert(ShopBindingModel model) { model.Id = 1; @@ -91,6 +97,11 @@ namespace ConfectioneryListImplement return newShop.GetViewModel; } + public bool SellPastries(IPastryModel pastry, int needCount) + { + throw new NotImplementedException(); + } + public ShopViewModel? Update(ShopBindingModel model) { foreach (var shop in _source.Shops) diff --git a/ConfectioneryContracts/StoragesContract/IShopStorage.cs b/ConfectioneryContracts/StoragesContract/IShopStorage.cs index 4276f2d..1f53aa5 100644 --- a/ConfectioneryContracts/StoragesContract/IShopStorage.cs +++ b/ConfectioneryContracts/StoragesContract/IShopStorage.cs @@ -1,6 +1,7 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryContracts.StoragesContract { @@ -12,5 +13,8 @@ namespace ConfectioneryContracts.StoragesContract ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + + bool HasNeedPastries(IPastryModel pastry, int needCount); + public bool SellPastries(IPastryModel pastry, int needCount); } } -- 2.25.1 From d6bf33d45b2c54b4c1e8c9c48e6b7768f6b9c688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 16:14:11 +0400 Subject: [PATCH 09/17] fix --- ConfectionaryFileImplement/OrderStorage.cs | 8 ++++---- ConfectionaryFileImplement/Pastry.cs | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs index aae2076..c2dc788 100644 --- a/ConfectionaryFileImplement/OrderStorage.cs +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -63,14 +63,14 @@ namespace ConfectioneryFileImplement public OrderViewModel? Update(OrderBindingModel model) { - var pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (pastry == null) + var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { return null; } - pastry.Update(model); + order.Update(model); _source.SaveOrders(); - return pastry.GetViewModel; + return order.GetViewModel; } } } diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index f32067c..6138c0c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,7 +1,6 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels.Models; -using ConfectioneryDataModels.Models; using System.Xml.Linq; namespace ConfectioneryFileImplement.Models -- 2.25.1 From 23be4237609537d31f2e66502294f7db119829a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 17:15:00 +0400 Subject: [PATCH 10/17] =?UTF-8?q?=D0=92=D0=BD=D0=B5=D0=B4=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BC=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=D0=B0=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D0=BE=D0=B5=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=BE=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B2=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=9C=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 26 ++++++++------ Confectionery/FormShop.Designer.cs | 45 ++++++++++++++++++++----- Confectionery/FormShop.cs | 6 ++-- Confectionery/FormShop.resx | 9 +++++ 4 files changed, 65 insertions(+), 21 deletions(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index d127dfa..3974880 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -101,6 +101,13 @@ namespace ConfectioneryBusinessLogic throw new ArgumentNullException("Нет названия магазина", nameof(model.Name)); } + if (model.MaxCountPastries < 0) + { + throw new ArgumentException( + "Максимальное количество изделий в магазине не должно быть отрицательным", + nameof(model.MaxCountPastries)); + } + _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.Name, model.Address, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel @@ -133,27 +140,24 @@ namespace ConfectioneryBusinessLogic } _logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); - if (element.MaxCountPastries < 0) + var currentCountPasties = element.Pastries.Select(x => x.Value.Item2).Sum(); + if (currentCountPasties + count > element.MaxCountPastries) { - throw new ArgumentException( - "Максимальное количество изделий в магазине не должно быть отрицательным", - nameof(element.MaxCountPastries)); + _logger.LogWarning("AddPastryInShop. The number of pastry {count} exceeds maximum {max}", + currentCountPasties + count, element.MaxCountPastries); + return false; } - if (element.Pastries.TryGetValue(pastry.Id, out var pair)) { element.Pastries[pastry.Id] = (pastry, count + pair.Item2); - _logger.LogInformation( - "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", - count, pastry.PastryName, element.Name); } else { element.Pastries[pastry.Id] = (pastry, count); - _logger.LogInformation( - "AddPastryInShop. Has been added {count} new Pastry {pastry} in {ShopName}", - count, pastry.PastryName, element.Name); } + _logger.LogInformation( + "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", + count, pastry.PastryName, element.Name); _shopStorage.Update(new() { Id = element.Id, diff --git a/Confectionery/FormShop.Designer.cs b/Confectionery/FormShop.Designer.cs index 694a22f..3a9ec82 100644 --- a/Confectionery/FormShop.Designer.cs +++ b/Confectionery/FormShop.Designer.cs @@ -40,7 +40,10 @@ this.textBoxDateOpening = new System.Windows.Forms.TextBox(); this.buttonCancel = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button(); + this.label4 = new System.Windows.Forms.Label(); + this.numericUpDownMaxPastry = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaxPastry)).BeginInit(); this.SuspendLayout(); // // label1 @@ -74,7 +77,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 64); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(583, 213); + this.dataGridView.Size = new System.Drawing.Size(640, 213); this.dataGridView.TabIndex = 2; // // PastryName @@ -105,7 +108,7 @@ // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(386, 9); + this.label3.Location = new System.Drawing.Point(366, 9); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(100, 15); this.label3.TabIndex = 4; @@ -115,20 +118,20 @@ // this.textBoxAddress.Location = new System.Drawing.Point(159, 27); this.textBoxAddress.Name = "textBoxAddress"; - this.textBoxAddress.Size = new System.Drawing.Size(221, 23); + this.textBoxAddress.Size = new System.Drawing.Size(201, 23); this.textBoxAddress.TabIndex = 5; // // textBoxDateOpening // - this.textBoxDateOpening.Location = new System.Drawing.Point(386, 27); + this.textBoxDateOpening.Location = new System.Drawing.Point(366, 27); this.textBoxDateOpening.Name = "textBoxDateOpening"; - this.textBoxDateOpening.Size = new System.Drawing.Size(209, 23); + this.textBoxDateOpening.Size = new System.Drawing.Size(160, 23); this.textBoxDateOpening.TabIndex = 6; // // 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(492, 283); + this.buttonCancel.Location = new System.Drawing.Point(549, 283); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(103, 23); this.buttonCancel.TabIndex = 7; @@ -139,7 +142,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(366, 284); + this.buttonSave.Location = new System.Drawing.Point(423, 284); this.buttonSave.Name = "buttonSave"; this.buttonSave.Size = new System.Drawing.Size(120, 22); this.buttonSave.TabIndex = 8; @@ -147,11 +150,34 @@ this.buttonSave.UseVisualStyleBackColor = true; this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(530, 9); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(118, 15); + this.label4.TabIndex = 9; + this.label4.Text = "Максимум изделий:"; + // + // numericUpDownMaxPastry + // + this.numericUpDownMaxPastry.Location = new System.Drawing.Point(532, 27); + this.numericUpDownMaxPastry.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownMaxPastry.Name = "numericUpDownMaxPastry"; + this.numericUpDownMaxPastry.Size = new System.Drawing.Size(120, 23); + this.numericUpDownMaxPastry.TabIndex = 10; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(607, 317); + this.ClientSize = new System.Drawing.Size(664, 317); + this.Controls.Add(this.numericUpDownMaxPastry); + this.Controls.Add(this.label4); this.Controls.Add(this.buttonSave); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.textBoxDateOpening); @@ -165,6 +191,7 @@ this.Text = "Просмотр изделий магазина"; this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaxPastry)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -184,5 +211,7 @@ private DataGridViewTextBoxColumn PastryName; private DataGridViewTextBoxColumn Price; private DataGridViewTextBoxColumn Count; + private Label label4; + private NumericUpDown numericUpDownMaxPastry; } } \ No newline at end of file diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs index c755270..fa5c0f5 100644 --- a/Confectionery/FormShop.cs +++ b/Confectionery/FormShop.cs @@ -66,7 +66,8 @@ namespace ConfectioneryView { var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.SelectedValue)); if (model != null) - { + { + numericUpDownMaxPastry.Value = model.MaxCountPastries; comboBoxShop.Text = model.Name; textBoxAddress.Text = model.Address; textBoxDateOpening.Text = Convert.ToString(model.DateOpening); @@ -113,7 +114,8 @@ namespace ConfectioneryView { Name = comboBoxShop.Text, Address = textBoxAddress.Text, - DateOpening = dateTime + DateOpening = dateTime, + MaxCountPastries = (int)numericUpDownMaxPastry.Value, }; var vmodel = GetShop(Id); bool operationResult = false; diff --git a/Confectionery/FormShop.resx b/Confectionery/FormShop.resx index c4ce247..5191e62 100644 --- a/Confectionery/FormShop.resx +++ b/Confectionery/FormShop.resx @@ -66,4 +66,13 @@ True + + True + + + True + + + True + \ No newline at end of file -- 2.25.1 From 977d357ea189a8b349198075bdaaa4cf83df542f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 17:19:29 +0400 Subject: [PATCH 11/17] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/Shop.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs index fadd63f..a873e7d 100644 --- a/ConfectionaryFileImplement/Shop.cs +++ b/ConfectionaryFileImplement/Shop.cs @@ -94,7 +94,7 @@ namespace ConfectioneryFileImplement DateOpening = DateOpening, MaxCountPastries = MaxCountPastries, }; - public XElement GetXElement => new("Order", + public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("Name", Name), new XElement("Address", Address), -- 2.25.1 From 64b59948b7717914077b22d6dfb1cedfc027e900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 18:21:15 +0400 Subject: [PATCH 12/17] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BA=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=B8=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2=20=D0=B8=D0=B7=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B8=D1=8F=D0=BC=D0=B8=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B2=D0=BE=D0=B4=D0=B5=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BA=D0=B0=D0=B7=D0=B0=20=D0=B2=20=D1=81=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D1=83=D1=81=20=D0=93=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/OrderLogic.cs | 15 +++++++- ConfectionaryBusinessLogic/ShopLogic.cs | 36 +++++++++++++++++++ ConfectionaryFileImplement/Shop.cs | 1 - .../BusinessLogicsContracts/IShopLogic.cs | 2 ++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index 1471185..3a57520 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -12,10 +12,14 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IPastryStorage _pastryStorage; + private readonly IShopLogic _shopLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic) { _logger = logger; + _shopLogic = shopLogic; + _pastryStorage = pastryStorage; _orderStorage = orderStorage; } @@ -96,6 +100,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics $"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}", nameof(vmodel)); } + if (orderStatus == OrderStatus.Готов) + { + var vpastry = _pastryStorage.GetElement(new() { Id = vmodel.PastryId }); + + if (vpastry == null || !_shopLogic.AddPastriesInShops(vpastry, vmodel.Count)) + { + throw new Exception($"Не удалось заполнить магазины изделием '{vpastry?.PastryName ?? string.Empty}' из заказа {vmodel.Id}"); + } + } model.Status = orderStatus; model.DateCreate = vmodel.DateCreate; if (model.DateImplement == null) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 3974880..5956da2 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -168,5 +168,41 @@ namespace ConfectioneryBusinessLogic }); return true; } + + + public int GetFreePlacesWithPastriesInShops(int countPastries) + { + // Сумма разностей между максимальный кол-вом изделий и суммой всех изделий в магазине + return _shopStorage.GetFullList() + .Select(x => x.MaxCountPastries - x.Pastries + .Select(p => p.Value.Item2).Sum()) + .Sum() - countPastries; + } + + public bool AddPastriesInShops(IPastryModel pastry, int count) + { + var freePlaces = GetFreePlacesWithPastriesInShops(count); + if (freePlaces < 0) + { + _logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." + + $"Освободите магазины на {-freePlaces} изделий"); + return false; + } + foreach (var shop in _shopStorage.GetFullList()) + { + var cnt = Math.Min(count, shop.MaxCountPastries - shop.Pastries.Select(x => x.Value.Item2).Sum()); + if (!AddPastry(new() { Id = shop.Id }, pastry, cnt)) + { + _logger.LogWarning("При добавления изделий во все магазины произошла ошибка"); + return false; + } + count -= cnt; + if (count == 0) + { + return true; + } + } + return true; + } } } diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs index a873e7d..928faf6 100644 --- a/ConfectionaryFileImplement/Shop.cs +++ b/ConfectionaryFileImplement/Shop.cs @@ -81,7 +81,6 @@ namespace ConfectioneryFileImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - MaxCountPastries = model.MaxCountPastries; CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2); _cachedPastries = null; } diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index 3ba0749..cd2e634 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -13,5 +13,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); + int GetFreePlacesWithPastriesInShops(int countPastries); + bool AddPastriesInShops(IPastryModel pastry, int count); } } -- 2.25.1 From 77889d887eb2326dd50a35421c55eabef048cca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 16:14:11 +0400 Subject: [PATCH 13/17] fix --- ConfectionaryFileImplement/OrderStorage.cs | 8 ++++---- ConfectionaryFileImplement/Pastry.cs | 1 - Confectionery/FormCreateOrder.Designer.cs | 2 +- Confectionery/FormCreateOrder.cs | 8 +++----- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs index aae2076..c2dc788 100644 --- a/ConfectionaryFileImplement/OrderStorage.cs +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -63,14 +63,14 @@ namespace ConfectioneryFileImplement public OrderViewModel? Update(OrderBindingModel model) { - var pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (pastry == null) + var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { return null; } - pastry.Update(model); + order.Update(model); _source.SaveOrders(); - return pastry.GetViewModel; + return order.GetViewModel; } } } diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index f32067c..6138c0c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,7 +1,6 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels.Models; -using ConfectioneryDataModels.Models; using System.Xml.Linq; namespace ConfectioneryFileImplement.Models diff --git a/Confectionery/FormCreateOrder.Designer.cs b/Confectionery/FormCreateOrder.Designer.cs index 7047902..ab9a95b 100644 --- a/Confectionery/FormCreateOrder.Designer.cs +++ b/Confectionery/FormCreateOrder.Designer.cs @@ -103,7 +103,7 @@ this.textBoxCount.Name = "textBoxCount"; this.textBoxCount.Size = new System.Drawing.Size(214, 23); this.textBoxCount.TabIndex = 6; - this.textBoxCount.Click += new System.EventHandler(this.TextBoxCount_TextChanged); + this.textBoxCount.ValueChanged += new System.EventHandler(this.TextBoxCount_TextChanged); // // textBoxSum // diff --git a/Confectionery/FormCreateOrder.cs b/Confectionery/FormCreateOrder.cs index 47cd686..01b08ad 100644 --- a/Confectionery/FormCreateOrder.cs +++ b/Confectionery/FormCreateOrder.cs @@ -39,8 +39,7 @@ namespace ConfectioneryView } private void CalcSum() { - if (comboBoxPastry.SelectedValue != null && - !string.IsNullOrEmpty(textBoxCount.Text)) + if (comboBoxPastry.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text)) { try { @@ -49,9 +48,8 @@ namespace ConfectioneryView { Id = id }); - int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (pastry?.Price ?? 0), - 2).ToString(); + int count = Convert.ToInt32(textBoxCount.Value); + textBoxSum.Text = Math.Round(count * (pastry?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } catch (Exception ex) -- 2.25.1 From 39bf5685390412c1c2cb42cfae2772c8f63015d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 19:15:28 +0400 Subject: [PATCH 14/17] =?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=BE=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B2=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 5956da2..bf255f6 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -181,16 +181,25 @@ namespace ConfectioneryBusinessLogic public bool AddPastriesInShops(IPastryModel pastry, int count) { + if (count <= 0) + { + _logger.LogWarning("AddPastriesInShops. Количество добавляемых изделий должно быть больше 0. Количество - {count}", count); + return false; + } var freePlaces = GetFreePlacesWithPastriesInShops(count); if (freePlaces < 0) { _logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." + - $"Освободите магазины на {-freePlaces} изделий"); + "Освободите магазины на {places} изделий", -freePlaces); return false; } foreach (var shop in _shopStorage.GetFullList()) { var cnt = Math.Min(count, shop.MaxCountPastries - shop.Pastries.Select(x => x.Value.Item2).Sum()); + if (cnt <= 0) + { + continue; + } if (!AddPastry(new() { Id = shop.Id }, pastry, cnt)) { _logger.LogWarning("При добавления изделий во все магазины произошла ошибка"); -- 2.25.1 From b2a18b6a98a64c6cbcc821ed7ba0dab56cefc969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 20:10:51 +0400 Subject: [PATCH 15/17] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 9 ++ ConfectionaryFileImplement/ShopStorage.cs | 8 +- Confectionery/FormMain.Designer.cs | 20 ++- Confectionery/FormMain.cs | 9 ++ Confectionery/FormSellPastry.Designer.cs | 133 ++++++++++++++++++ Confectionery/FormSellPastry.cs | 67 +++++++++ Confectionery/FormSellPastry.resx | 60 ++++++++ Confectionery/Program.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 2 + 9 files changed, 303 insertions(+), 6 deletions(-) create mode 100644 Confectionery/FormSellPastry.Designer.cs create mode 100644 Confectionery/FormSellPastry.cs create mode 100644 Confectionery/FormSellPastry.resx diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index bf255f6..fe157d8 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -213,5 +213,14 @@ namespace ConfectioneryBusinessLogic } return true; } + + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + return _shopStorage.HasNeedPastries(pastry, needCount); + } + public bool SellPastries(IPastryModel pastry, int needCount) + { + return _shopStorage.SellPastries(pastry, needCount); + } } } diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 3e9d4da..e155218 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement @@ -94,11 +95,12 @@ namespace ConfectioneryFileImplement { return false; } - foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + foreach (var shop in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) { - var tuple = pair.Pastries[pastry.Id]; + var tuple = shop.Pastries[pastry.Id]; var diff = Math.Min(tuple.Item2, needCount); - pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; if (needCount <= 0) { diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 0710300..35f58fe 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.buttonAddPastryInShop = new System.Windows.Forms.Button(); + this.buttonSellPastry = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -94,7 +95,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 27); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(606, 341); + this.dataGridView.Size = new System.Drawing.Size(606, 399); this.dataGridView.TabIndex = 1; // // buttonCreateOrder @@ -155,7 +156,7 @@ // buttonAddPastryInShop // this.buttonAddPastryInShop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 326); + this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 384); this.buttonAddPastryInShop.Name = "buttonAddPastryInShop"; this.buttonAddPastryInShop.Size = new System.Drawing.Size(147, 31); this.buttonAddPastryInShop.TabIndex = 7; @@ -163,11 +164,23 @@ this.buttonAddPastryInShop.UseVisualStyleBackColor = true; this.buttonAddPastryInShop.Click += new System.EventHandler(this.ButtonAddPastryInShop_Click); // + // buttonSellPastry + // + this.buttonSellPastry.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSellPastry.Location = new System.Drawing.Point(624, 331); + this.buttonSellPastry.Name = "buttonSellPastry"; + this.buttonSellPastry.Size = new System.Drawing.Size(147, 31); + this.buttonSellPastry.TabIndex = 8; + this.buttonSellPastry.Text = "Продать изделие"; + this.buttonSellPastry.UseVisualStyleBackColor = true; + this.buttonSellPastry.Click += new System.EventHandler(this.ButtonSellPastry_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(783, 380); + this.ClientSize = new System.Drawing.Size(783, 438); + this.Controls.Add(this.buttonSellPastry); this.Controls.Add(this.buttonAddPastryInShop); this.Controls.Add(this.button4); this.Controls.Add(this.button3); @@ -202,5 +215,6 @@ private ToolStripMenuItem componentToolStripMenuItem; private ToolStripMenuItem ShopsToolStripMenuItem; private Button buttonAddPastryInShop; + private Button buttonSellPastry; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 3c6c955..6dcaf07 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -170,5 +170,14 @@ namespace ConfectioneryView form.ShowDialog(); } } + + private void ButtonSellPastry_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPastry)); + if (service is FormSellPastry form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/Confectionery/FormSellPastry.Designer.cs b/Confectionery/FormSellPastry.Designer.cs new file mode 100644 index 0000000..46f5949 --- /dev/null +++ b/Confectionery/FormSellPastry.Designer.cs @@ -0,0 +1,133 @@ +namespace ConfectioneryView +{ + partial class FormSellPastry + { + /// + /// 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() + { + this.comboBoxPastry = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.numericUpDownCount = new System.Windows.Forms.NumericUpDown(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit(); + this.SuspendLayout(); + // + // comboBoxPastry + // + this.comboBoxPastry.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxPastry.FormattingEnabled = true; + this.comboBoxPastry.Location = new System.Drawing.Point(141, 12); + this.comboBoxPastry.Name = "comboBoxPastry"; + this.comboBoxPastry.Size = new System.Drawing.Size(121, 23); + this.comboBoxPastry.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(123, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Изделие на продажу:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(60, 46); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(75, 15); + this.label2.TabIndex = 2; + this.label2.Text = "Количество:"; + // + // numericUpDownCount + // + this.numericUpDownCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.numericUpDownCount.Location = new System.Drawing.Point(142, 46); + this.numericUpDownCount.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownCount.Name = "numericUpDownCount"; + this.numericUpDownCount.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.numericUpDownCount.Size = new System.Drawing.Size(120, 23); + this.numericUpDownCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSell.Location = new System.Drawing.Point(12, 90); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(123, 23); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // 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(142, 90); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(123, 23); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellPastry + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(277, 122); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.numericUpDownCount); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.comboBoxPastry); + this.Name = "FormSellPastry"; + this.Text = "Продажа изделия"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxPastry; + private Label label1; + private Label label2; + private NumericUpDown numericUpDownCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Confectionery/FormSellPastry.cs b/Confectionery/FormSellPastry.cs new file mode 100644 index 0000000..17731cd --- /dev/null +++ b/Confectionery/FormSellPastry.cs @@ -0,0 +1,67 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using System; +using System.Collections; +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; + +namespace ConfectioneryView +{ + public partial class FormSellPastry : Form + { + private readonly IShopLogic _shopLogic; + private readonly IPastryLogic _pastryLogic; + + public FormSellPastry(IPastryLogic logic, IShopLogic shopLogic) + { + InitializeComponent(); + _pastryLogic = logic; + _shopLogic = shopLogic; + var list = logic.ReadList(null); + if (list != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember = "Id"; + comboBoxPastry.DataSource = list; + comboBoxPastry.SelectedItem = null; + } + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (numericUpDownCount.Value <= 0) + { + MessageBox.Show("Количество должно быть больше нуля", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + var count = (int)numericUpDownCount.Value; + var pastry = _pastryLogic.ReadElement(new() { Id = (int)comboBoxPastry.SelectedValue }); + if (pastry == null || !_shopLogic.SellPastries(pastry, count)) + { + 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/Confectionery/FormSellPastry.resx b/Confectionery/FormSellPastry.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormSellPastry.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/Confectionery/Program.cs b/Confectionery/Program.cs index 6f0ea6d..e76fcdb 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -53,6 +53,7 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index cd2e634..f0947f9 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -15,5 +15,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); int GetFreePlacesWithPastriesInShops(int countPastries); bool AddPastriesInShops(IPastryModel pastry, int count); + bool HasNeedPastries(IPastryModel pastry, int needCount); + public bool SellPastries(IPastryModel pastry, int needCount); } } -- 2.25.1 From 5d93f4126cc4f3c53a941288bac8c1bce830b688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 20:10:51 +0400 Subject: [PATCH 16/17] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83?= =?UTF-8?q?=D0=B5=D0=BC=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 5 + ConfectionaryFileImplement/ShopStorage.cs | 8 +- Confectionery/FormMain.Designer.cs | 20 ++- Confectionery/FormMain.cs | 9 ++ Confectionery/FormSellPastry.Designer.cs | 133 ++++++++++++++++++ Confectionery/FormSellPastry.cs | 67 +++++++++ Confectionery/FormSellPastry.resx | 60 ++++++++ Confectionery/Program.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 1 + 9 files changed, 298 insertions(+), 6 deletions(-) create mode 100644 Confectionery/FormSellPastry.Designer.cs create mode 100644 Confectionery/FormSellPastry.cs create mode 100644 Confectionery/FormSellPastry.resx diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index bf255f6..045428b 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -213,5 +213,10 @@ namespace ConfectioneryBusinessLogic } return true; } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + return _shopStorage.SellPastries(pastry, needCount); + } } } diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 3e9d4da..e155218 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement @@ -94,11 +95,12 @@ namespace ConfectioneryFileImplement { return false; } - foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + foreach (var shop in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) { - var tuple = pair.Pastries[pastry.Id]; + var tuple = shop.Pastries[pastry.Id]; var diff = Math.Min(tuple.Item2, needCount); - pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; if (needCount <= 0) { diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 0710300..35f58fe 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.buttonAddPastryInShop = new System.Windows.Forms.Button(); + this.buttonSellPastry = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -94,7 +95,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 27); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(606, 341); + this.dataGridView.Size = new System.Drawing.Size(606, 399); this.dataGridView.TabIndex = 1; // // buttonCreateOrder @@ -155,7 +156,7 @@ // buttonAddPastryInShop // this.buttonAddPastryInShop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 326); + this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 384); this.buttonAddPastryInShop.Name = "buttonAddPastryInShop"; this.buttonAddPastryInShop.Size = new System.Drawing.Size(147, 31); this.buttonAddPastryInShop.TabIndex = 7; @@ -163,11 +164,23 @@ this.buttonAddPastryInShop.UseVisualStyleBackColor = true; this.buttonAddPastryInShop.Click += new System.EventHandler(this.ButtonAddPastryInShop_Click); // + // buttonSellPastry + // + this.buttonSellPastry.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSellPastry.Location = new System.Drawing.Point(624, 331); + this.buttonSellPastry.Name = "buttonSellPastry"; + this.buttonSellPastry.Size = new System.Drawing.Size(147, 31); + this.buttonSellPastry.TabIndex = 8; + this.buttonSellPastry.Text = "Продать изделие"; + this.buttonSellPastry.UseVisualStyleBackColor = true; + this.buttonSellPastry.Click += new System.EventHandler(this.ButtonSellPastry_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(783, 380); + this.ClientSize = new System.Drawing.Size(783, 438); + this.Controls.Add(this.buttonSellPastry); this.Controls.Add(this.buttonAddPastryInShop); this.Controls.Add(this.button4); this.Controls.Add(this.button3); @@ -202,5 +215,6 @@ private ToolStripMenuItem componentToolStripMenuItem; private ToolStripMenuItem ShopsToolStripMenuItem; private Button buttonAddPastryInShop; + private Button buttonSellPastry; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 3c6c955..6dcaf07 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -170,5 +170,14 @@ namespace ConfectioneryView form.ShowDialog(); } } + + private void ButtonSellPastry_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPastry)); + if (service is FormSellPastry form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/Confectionery/FormSellPastry.Designer.cs b/Confectionery/FormSellPastry.Designer.cs new file mode 100644 index 0000000..46f5949 --- /dev/null +++ b/Confectionery/FormSellPastry.Designer.cs @@ -0,0 +1,133 @@ +namespace ConfectioneryView +{ + partial class FormSellPastry + { + /// + /// 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() + { + this.comboBoxPastry = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.numericUpDownCount = new System.Windows.Forms.NumericUpDown(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit(); + this.SuspendLayout(); + // + // comboBoxPastry + // + this.comboBoxPastry.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxPastry.FormattingEnabled = true; + this.comboBoxPastry.Location = new System.Drawing.Point(141, 12); + this.comboBoxPastry.Name = "comboBoxPastry"; + this.comboBoxPastry.Size = new System.Drawing.Size(121, 23); + this.comboBoxPastry.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(123, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Изделие на продажу:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(60, 46); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(75, 15); + this.label2.TabIndex = 2; + this.label2.Text = "Количество:"; + // + // numericUpDownCount + // + this.numericUpDownCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.numericUpDownCount.Location = new System.Drawing.Point(142, 46); + this.numericUpDownCount.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownCount.Name = "numericUpDownCount"; + this.numericUpDownCount.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.numericUpDownCount.Size = new System.Drawing.Size(120, 23); + this.numericUpDownCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSell.Location = new System.Drawing.Point(12, 90); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(123, 23); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // 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(142, 90); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(123, 23); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellPastry + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(277, 122); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.numericUpDownCount); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.comboBoxPastry); + this.Name = "FormSellPastry"; + this.Text = "Продажа изделия"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxPastry; + private Label label1; + private Label label2; + private NumericUpDown numericUpDownCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Confectionery/FormSellPastry.cs b/Confectionery/FormSellPastry.cs new file mode 100644 index 0000000..17731cd --- /dev/null +++ b/Confectionery/FormSellPastry.cs @@ -0,0 +1,67 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using System; +using System.Collections; +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; + +namespace ConfectioneryView +{ + public partial class FormSellPastry : Form + { + private readonly IShopLogic _shopLogic; + private readonly IPastryLogic _pastryLogic; + + public FormSellPastry(IPastryLogic logic, IShopLogic shopLogic) + { + InitializeComponent(); + _pastryLogic = logic; + _shopLogic = shopLogic; + var list = logic.ReadList(null); + if (list != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember = "Id"; + comboBoxPastry.DataSource = list; + comboBoxPastry.SelectedItem = null; + } + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (numericUpDownCount.Value <= 0) + { + MessageBox.Show("Количество должно быть больше нуля", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + var count = (int)numericUpDownCount.Value; + var pastry = _pastryLogic.ReadElement(new() { Id = (int)comboBoxPastry.SelectedValue }); + if (pastry == null || !_shopLogic.SellPastries(pastry, count)) + { + 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/Confectionery/FormSellPastry.resx b/Confectionery/FormSellPastry.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormSellPastry.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/Confectionery/Program.cs b/Confectionery/Program.cs index 6f0ea6d..e76fcdb 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -53,6 +53,7 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index cd2e634..6390a91 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -15,5 +15,6 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); int GetFreePlacesWithPastriesInShops(int countPastries); bool AddPastriesInShops(IPastryModel pastry, int count); + public bool SellPastries(IPastryModel pastry, int needCount); } } -- 2.25.1 From 746f5be7a42c64b11497102d1fc6f28b2d42e26b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 21:59:49 +0400 Subject: [PATCH 17/17] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BE=20=D0=B2=D1=8B=D1=87=D0=B8=D1=81?= =?UTF-8?q?=D0=BB=D1=8F=D0=B5=D0=BC=D0=BE=D0=B5=20=D0=BF=D0=BE=D0=BB=D0=B5?= =?UTF-8?q?=20PastryName?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/Order.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ConfectionaryFileImplement/Order.cs b/ConfectionaryFileImplement/Order.cs index d3067f1..8a00130 100644 --- a/ConfectionaryFileImplement/Order.cs +++ b/ConfectionaryFileImplement/Order.cs @@ -74,6 +74,7 @@ namespace ConfectioneryFileImplement.Models } public OrderViewModel GetViewModel => new() { + PastryName = DataFileSingleton.GetInstance().Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty, PastryId = PastryId, Count = Count, Sum = Sum, -- 2.25.1