From 7a09fe916079ea498dee4f5b924b5d1acec367b9 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Mon, 27 Feb 2023 02:55:55 +0400 Subject: [PATCH 1/8] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ComputersShop/ComputersShop.sln | 10 +- .../ComputersShopFileImplement.csproj | 14 +++ .../DataFileSingleton.cs | 54 +++++++++ .../Implements/ComponentStorage.cs | 78 +++++++++++++ .../Implements/ComputerStorage.cs | 82 +++++++++++++ .../Implements/OrderStorage.cs | 96 ++++++++++++++++ .../Models/Component.cs | 64 +++++++++++ .../Models/Computer.cs | 95 +++++++++++++++ .../Models/Order.cs | 108 ++++++++++++++++++ .../ComputersShopView.csproj | 2 +- ComputersShop/ComputersShopView/Program.cs | 3 +- ComputersShop/Temp.txt | 1 + 12 files changed, 602 insertions(+), 5 deletions(-) create mode 100644 ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj create mode 100644 ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Models/Component.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Models/Computer.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Models/Order.cs create mode 100644 ComputersShop/Temp.txt diff --git a/ComputersShop/ComputersShop.sln b/ComputersShop/ComputersShop.sln index a517877..b51e620 100644 --- a/ComputersShop/ComputersShop.sln +++ b/ComputersShop/ComputersShop.sln @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopDataModels", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopContracts", "ComputersShopContracts\ComputersShopContracts.csproj", "{7404526F-8C94-457B-BF18-85445EADC75F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopBusinessLogic", "ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj", "{5270DCF9-79F8-45B5-90F4-D460B0F0F571}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopBusinessLogic", "ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj", "{5270DCF9-79F8-45B5-90F4-D460B0F0F571}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopListImplement", "ComputersShopListImplement\ComputersShopListImplement.csproj", "{F4C10F5F-A602-48E7-9820-C26B774203E7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopListImplement", "ComputersShopListImplement\ComputersShopListImplement.csproj", "{F4C10F5F-A602-48E7-9820-C26B774203E7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{AC4BC639-3B3F-48B1-87D2-22601554F418}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {F4C10F5F-A602-48E7-9820-C26B774203E7}.Debug|Any CPU.Build.0 = Debug|Any CPU {F4C10F5F-A602-48E7-9820-C26B774203E7}.Release|Any CPU.ActiveCfg = Release|Any CPU {F4C10F5F-A602-48E7-9820-C26B774203E7}.Release|Any CPU.Build.0 = Release|Any CPU + {AC4BC639-3B3F-48B1-87D2-22601554F418}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC4BC639-3B3F-48B1-87D2-22601554F418}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC4BC639-3B3F-48B1-87D2-22601554F418}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC4BC639-3B3F-48B1-87D2-22601554F418}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj b/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj new file mode 100644 index 0000000..8eb14fb --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..f59fd2a --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs @@ -0,0 +1,54 @@ +using ComputersShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputersShopFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string ComputerFileName = "Computer.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Computers { 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 SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Computers = LoadData(ComputerFileName, "Computer", x => Computer.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/ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..93aa931 --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,78 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs new file mode 100644 index 0000000..2c77fba --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs @@ -0,0 +1,82 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopFileImplement.Implements +{ + public class ComputerStorage : IComputerStorage + { + private readonly DataFileSingleton source; + + public ComputerStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public ComputerViewModel? GetElement(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName) && !model.Id.HasValue) + { + return null; + } + return source.Computers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && x.ComputerName == model.ComputerName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName)) + { + return new(); + } + return source.Computers.Where(x => x.ComputerName.Contains(model.ComputerName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.Computers.Select(x => x.GetViewModel).ToList(); + } + + public ComputerViewModel? Insert(ComputerBindingModel model) + { + model.Id = source.Computers.Count > 0 ? source.Computers.Max(x => x.Id) + 1 : 1; + var newDoc = Computer.Create(model); + if (newDoc == null) + { + return null; + } + source.Computers.Add(newDoc); + source.SaveComputers(); + return newDoc.GetViewModel; + } + + public ComputerViewModel? Update(ComputerBindingModel model) + { + var document = source.Computers.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveComputers(); + return document.GetViewModel; + } + public ComputerViewModel? Delete(ComputerBindingModel model) + { + var document = source.Computers.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveComputers(); + return document.GetViewModel; + } + } +} diff --git a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..b9ad9eb --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,96 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return GetViewModel(source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).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 GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetViewModel(order); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetViewModel(order); + } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + foreach (var comp in source.Computers) + { + if (comp.Id == order.ComputerId) + { + viewModel.ComputerName = comp.ComputerName; + break; + } + } + return viewModel; + } + } +} diff --git a/ComputersShop/ComputersShopFileImplement/Models/Component.cs b/ComputersShop/ComputersShopFileImplement/Models/Component.cs new file mode 100644 index 0000000..c25e38b --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Models/Component.cs @@ -0,0 +1,64 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputersShopFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/ComputersShop/ComputersShopFileImplement/Models/Computer.cs b/ComputersShop/ComputersShopFileImplement/Models/Computer.cs new file mode 100644 index 0000000..5854244 --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Models/Computer.cs @@ -0,0 +1,95 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputersShopFileImplement.Models +{ + public class Computer : IComputerModel + { + public int Id { get; private set; } + public string ComputerName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _ComputerComponents = null; + public Dictionary ComputerComponents + { + get + { + if (_ComputerComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _ComputerComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _ComputerComponents; + } + } + public static Computer? Create(ComputerBindingModel model) + { + if (model == null) + { + return null; + } + return new Computer() + { + Id = model.Id, + ComputerName = model.ComputerName, + Price = model.Price, + Components = model.ComputerComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Computer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Computer() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComputerName = element.Element("ComputerName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = + element.Element("ComputerComponents")!.Elements("ComputerComponent") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(ComputerBindingModel model) + { + if (model == null) + { + return; + } + ComputerName = model.ComputerName; + Price = model.Price; + Components = model.ComputerComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _ComputerComponents = null; + } + public ComputerViewModel GetViewModel => new() + { + Id = Id, + ComputerName = ComputerName, + Price = Price, + ComputerComponents = ComputerComponents + }; + public XElement GetXElement => new("Computer", + new XAttribute("Id", Id), + new XElement("ComputerName", ComputerName), + new XElement("Price", Price.ToString()), + new XElement("ComputerComponents", Components.Select(x => new XElement("ComputerComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))) + + .ToArray())); + + } +} diff --git a/ComputersShop/ComputersShopFileImplement/Models/Order.cs b/ComputersShop/ComputersShopFileImplement/Models/Order.cs new file mode 100644 index 0000000..24382c4 --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Models/Order.cs @@ -0,0 +1,108 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputersShopFileImplement.Models +{ + public class Order : IOrderModel + { + public int ComputerId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int Id { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ComputerId = model.ComputerId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + var order = new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComputerId = Convert.ToInt32(element.Element("ComputerId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null), + }; + DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); + + order.DateImplement = dateImpl; + + if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status)) + { + return null; + } + order.Status = status; + return order; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + ComputerId = model.ComputerId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + ComputerId = ComputerId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("ComputerId", ComputerId), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } +} diff --git a/ComputersShop/ComputersShopView/ComputersShopView.csproj b/ComputersShop/ComputersShopView/ComputersShopView.csproj index b5f4f28..ea5d5cb 100644 --- a/ComputersShop/ComputersShopView/ComputersShopView.csproj +++ b/ComputersShop/ComputersShopView/ComputersShopView.csproj @@ -18,7 +18,7 @@ - + \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index a93980f..9a8e87a 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -1,11 +1,10 @@ using ComputersShopBusinessLogic.BusinessLogics; using ComputersShopContracts.BusinessLogicContracts; using ComputersShopContracts.StoragesContracts; -using ComputersShopListImplement.Implements; +using ComputersShopFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using OrdersShopListImplement.Implements; namespace ComputersShopView { diff --git a/ComputersShop/Temp.txt b/ComputersShop/Temp.txt new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/ComputersShop/Temp.txt @@ -0,0 +1 @@ + \ No newline at end of file -- 2.25.1 From 44235082c6d50283be61af3ef0ea7cd3463ce042 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Wed, 8 Mar 2023 17:22:04 +0400 Subject: [PATCH 2/8] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D0=B0=D1=8F=20=D1=82=D0=BE=D1=87=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ComputersShop/ComputersShopFileImplement/Models/Order.cs | 4 ---- ComputersShop/Temp.txt | 1 - 2 files changed, 5 deletions(-) delete mode 100644 ComputersShop/Temp.txt diff --git a/ComputersShop/ComputersShopFileImplement/Models/Order.cs b/ComputersShop/ComputersShopFileImplement/Models/Order.cs index 24382c4..e41250c 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Order.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Order.cs @@ -77,11 +77,7 @@ namespace ComputersShopFileImplement.Models { return; } - ComputerId = model.ComputerId; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; } diff --git a/ComputersShop/Temp.txt b/ComputersShop/Temp.txt deleted file mode 100644 index 5f28270..0000000 --- a/ComputersShop/Temp.txt +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file -- 2.25.1 From 1bf503cc9231c7eb79f3720ffbce2976eafd4f2f Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Sun, 23 Apr 2023 22:59:36 +0400 Subject: [PATCH 3/8] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 25 +++- .../BusinessLogics/ShopLogic.cs | 73 +++++++++- .../BindingModels/ShopBindingModel.cs | 3 +- .../{IStoreLogic.cs => IShopLogic.cs} | 4 +- .../StoragesContracts/IShopStorage.cs | 4 +- .../ViewModels/ShopViewModel.cs | 4 +- .../Models/IShopModel.cs | 4 +- .../DataFileSingleton.cs | 12 +- .../Implements/ShopStorage.cs | 125 ++++++++++++++++++ .../ComputersShopFileImplement/Models/Shop.cs | 108 +++++++++++++++ .../Implements/ShopStorage.cs | 8 +- .../ComputersShopListImplement/Models/Shop.cs | 4 +- 12 files changed, 353 insertions(+), 21 deletions(-) rename ComputersShop/ComputersShopContracts/BusinessLogicContracts/{IStoreLogic.cs => IShopLogic.cs} (83%) create mode 100644 ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs create mode 100644 ComputersShop/ComputersShopFileImplement/Models/Shop.cs diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs index 17fc279..ff9403e 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,10 +17,14 @@ namespace ComputersShopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopLogic _shopLogic; + private readonly IComputerStorage _computerStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IComputerStorage computerStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _computerStorage = computerStorage; } public bool CreateOrder(OrderBindingModel model) { @@ -52,16 +56,29 @@ namespace ComputersShopBusinessLogic.BusinessLogics return false; } model.Status = newStatus; - if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now; + if (model.Status == OrderStatus.Готов) + { + + model.DateImplement = DateTime.Now; + var computer = _computerStorage.GetElement(new() { Id = viewModel.ComputerId }); + if (computer == null) + { + throw new ArgumentNullException(nameof(computer)); + } + if (!_shopLogic.AddComputers(computer, viewModel.Count)) + { + throw new Exception($"AddComputers operation failed - нет места"); + } + } else { model.DateImplement = viewModel.DateImplement; } - CheckModel(model); + CheckModel(model, false); if (_orderStorage.Update(model) == null) { model.Status--; - _logger.LogWarning("Update operation failed"); + _logger.LogWarning("Change status operation failed"); return false; } return true; diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs index eb93147..b33660f 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -43,9 +44,12 @@ namespace ComputersShopBusinessLogic.BusinessLogics return false; } - _logger.LogInformation("AddComputerInShop find. Id:{Id}", element.Id); + if (element.Capacity - element.Computers.Select(x => x.Value.Item2).Sum() < quantity) + { + throw new ArgumentNullException("В магазине не хватает места", nameof(quantity)); + } - if (element.Computers.TryGetValue(computer.Id, out var pair)) + if (element.Computers.TryGetValue(computer.Id, out var pair)) { element.Computers[computer.Id] = (computer, quantity + pair.Item2); _logger.LogInformation("AddComputerInShop. Has been added {quantity} {Computer} in {ShopName}", quantity, computer.ComputerName, element.ShopName); @@ -62,12 +66,64 @@ namespace ComputersShopBusinessLogic.BusinessLogics ShopAddress = element.ShopAddress, ShopName = element.ShopName, DateOpening = element.DateOpening, - Computers = element.Computers - }); + Computers = element.Computers, + Capacity = element.Capacity, + }); return true; } - public bool Create(ShopBindingModel model) + public bool AddComputers(IComputerModel computer, int quantity) + { + if (computer == null) + { + throw new ArgumentNullException(nameof(computer)); + } + if (quantity <= 0) + { + throw new ArgumentException("Количество документов должно быть больше 0", nameof(quantity)); + } + _logger.LogInformation("AddComputers. ShopName:{ShopName}. Id:{Id}", computer.ComputerName, computer.Id); + var allFreeQuantity = _shopStorage.GetFullList().Select(x => x.Capacity - x.Computers.Select(x => x.Value.Item2).Sum()).Sum(); + if (allFreeQuantity < quantity) + { + _logger.LogWarning("AddComputers operation failed."); + return false; + } + foreach (var shop in _shopStorage.GetFullList()) + { + int freeQuantity = shop.Capacity - shop.Computers.Select(x => x.Value.Item2).Sum(); + if (freeQuantity <= 0) + { + continue; + } + if (freeQuantity < quantity) + { + if (!AddComputer(new() { Id = shop.Id }, computer, freeQuantity)) + { + _logger.LogWarning("AddComputers operation failed."); + return false; + } + quantity -= freeQuantity; + } + else + { + if (!AddComputer(new() { Id = shop.Id }, computer, quantity)) + { + _logger.LogWarning("AddComputers operation failed."); + return false; + } + quantity = 0; + } + if (quantity == 0) + { + return true; + } + } + _logger.LogWarning("AddComputers operation failed."); + return false; + } + + public bool Create(ShopBindingModel model) { CheckModel(model); model.Computers = new(); @@ -131,7 +187,12 @@ namespace ComputersShopBusinessLogic.BusinessLogics return list; } - public bool Update(ShopBindingModel model) + public bool SellComputers(IComputerModel computer, int quantity) + { + return _shopStorage.SellComputers(computer, quantity); + } + + public bool Update(ShopBindingModel model) { CheckModel(model, false); diff --git a/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs index 9917239..f154e9d 100644 --- a/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs +++ b/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs @@ -14,5 +14,6 @@ namespace ComputersShopContracts.BindingModels public string ShopAddress { get; set; } = string.Empty; public DateTime DateOpening { get; set; } = DateTime.Now; public Dictionary Computers { get; set; } = new(); - } + public int Capacity { get; set; } + } } diff --git a/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IStoreLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IShopLogic.cs similarity index 83% rename from ComputersShop/ComputersShopContracts/BusinessLogicContracts/IStoreLogic.cs rename to ComputersShop/ComputersShopContracts/BusinessLogicContracts/IShopLogic.cs index 192fa4a..e2690a2 100644 --- a/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IStoreLogic.cs +++ b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IShopLogic.cs @@ -18,5 +18,7 @@ namespace ComputersShopContracts.BusinessLogicContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddComputer(ShopSearchModel model, IComputerModel computer, int quantity); - } + bool AddComputers(IComputerModel computer, int quantity); + bool SellComputers(IComputerModel computer, int quantity); + } } diff --git a/ComputersShop/ComputersShopContracts/StoragesContracts/IShopStorage.cs b/ComputersShop/ComputersShopContracts/StoragesContracts/IShopStorage.cs index 4f68769..9b1979f 100644 --- a/ComputersShop/ComputersShopContracts/StoragesContracts/IShopStorage.cs +++ b/ComputersShop/ComputersShopContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using ComputersShopContracts.BindingModels; using ComputersShopContracts.SearchModels; using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -17,5 +18,6 @@ namespace ComputersShopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); - } + bool SellComputers(IComputerModel model, int quantity); + } } diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs index 4a03f7a..9b28754 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs @@ -19,5 +19,7 @@ namespace ComputersShopContracts.ViewModels public string ShopAddress { get; set; } = string.Empty; [DisplayName("Дата открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; - } + [DisplayName("Вместимость магазина")] + public int Capacity { get; set; } + } } diff --git a/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs b/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs index 4767190..fdf703f 100644 --- a/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs +++ b/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs @@ -11,6 +11,8 @@ namespace ComputersShopDataModels.Models public string ShopName { get; } public string ShopAddress { get; } DateTime DateOpening { get; } - Dictionary Computers { get; } + + public int Capacity { get; } + Dictionary Computers { get; } } } diff --git a/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs index f59fd2a..783e35f 100644 --- a/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs +++ b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs @@ -14,10 +14,12 @@ namespace ComputersShopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string ComputerFileName = "Computer.xml"; - public List Components { get; private set; } + private readonly string ShopFileName = "Shop.xml"; + public List Components { get; private set; } public List Orders { get; private set; } public List Computers { get; private set; } - public static DataFileSingleton GetInstance() + public List Shops { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) { @@ -28,12 +30,14 @@ namespace ComputersShopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - private DataFileSingleton() + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); + private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Computers = LoadData(ComputerFileName, "Computer", x => Computer.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) { if (File.Exists(filename)) diff --git a/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..88c9961 --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,125 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using ComputersShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if(string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops + .Where(x => x.ShopName.Contains(model.ShopName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + + public bool SellComputers(IComputerModel model, int quantity) + { + int availableQuantity = source.Shops.Select(x => x.Computers.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum(); + if (availableQuantity < quantity) + { + return false; + } + var shops = source.Shops.Where(x => x.Computers.ContainsKey(model.Id)); + foreach (var shop in shops) + { + int countInCurrentShop = shop.Computers[model.Id].Item2; + if (countInCurrentShop <= quantity) + { + shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, 0); + quantity -= countInCurrentShop; + } + else + { + shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, countInCurrentShop - quantity); + quantity = 0; + } + Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + ShopAddress = shop.ShopAddress, + DateOpening = shop.DateOpening, + Computers = shop.Computers, + Capacity = shop.Capacity + }); + if (quantity == 0) + { + return true; + } + } + return false; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveShops(); + return shop.GetViewModel; + } + } +} diff --git a/ComputersShop/ComputersShopFileImplement/Models/Shop.cs b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..c805b81 --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs @@ -0,0 +1,108 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputersShopFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string ShopAddress { get; private set; } = string.Empty; + public DateTime DateOpening { get; private set; } + public int Capacity { get; private set; } + public Dictionary ComputersCount = new(); + public Dictionary? _computers = null; + public Dictionary Computers + { + get + { + if (_computers == null) + { + var source = DataFileSingleton.GetInstance(); + _computers = ComputersCount.ToDictionary( + x => x.Key, + y => ((source.Computers.FirstOrDefault(z => z.Id == y.Key) as IComputerModel)!, + y.Value) + ); + } + return _computers; + } + } + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + ShopAddress = model.ShopAddress, + DateOpening = model.DateOpening, + Capacity = model.Capacity, + ComputersCount = model.Computers.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + ShopAddress = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + Capacity = Convert.ToInt32(element.Element("Capacity")!.Value), + ComputersCount = element.Element("Computers")!.Elements("Computer") + .ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + ShopAddress = model.ShopAddress; + DateOpening = model.DateOpening; + Capacity = model.Capacity; + ComputersCount = model.Computers.ToDictionary(x => x.Key, x => x.Value.Item2); + _computers = null; + + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAddress = ShopAddress, + DateOpening = DateOpening, + Capacity = Capacity, + Computers = Computers + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", ShopAddress), + new XElement("DateOpening", DateOpening.ToString()), + new XElement("Capacity", Capacity.ToString()), + new XElement("Documents", ComputersCount.Select(x => + new XElement("Document", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} diff --git a/ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs index 5a3ba72..a16bf18 100644 --- a/ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs +++ b/ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using ComputersShopContracts.SearchModels; using ComputersShopContracts.StoragesContracts; using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; using ComputersShopListImplement.Models; using System; using System.Collections.Generic; @@ -107,7 +108,12 @@ namespace ComputersShopListImplement.Implements return newShop.GetViewModel; } - public ShopViewModel? Update(ShopBindingModel model) + public bool SellComputers(IComputerModel model, int quantity) + { + throw new NotImplementedException(); + } + + public ShopViewModel? Update(ShopBindingModel model) { foreach (var Shop in _source.Shops) { diff --git a/ComputersShop/ComputersShopListImplement/Models/Shop.cs b/ComputersShop/ComputersShopListImplement/Models/Shop.cs index 5f6aff8..41f480c 100644 --- a/ComputersShop/ComputersShopListImplement/Models/Shop.cs +++ b/ComputersShop/ComputersShopListImplement/Models/Shop.cs @@ -55,5 +55,7 @@ namespace ComputersShopListImplement.Models DateOpening = DateOpening, Computers = Computers }; - } + + public int Capacity => throw new NotImplementedException(); + } } -- 2.25.1 From 0626e3d50aefb305f95c6025163cbd59ee4181b7 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Mon, 24 Apr 2023 16:39:16 +0400 Subject: [PATCH 4/8] =?UTF-8?q?=D0=9E=D1=88=D0=B8=D0=B1=D0=BE=D1=87=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/IShopModel.cs | 1 - .../ComputersShopFileImplement/Models/Shop.cs | 2 +- .../ComputersShopView/FormMain.Designer.cs | 375 +++++++++--------- ComputersShop/ComputersShopView/FormMain.cs | 350 ++++++++-------- .../FormSellComputers.Designer.cs | 124 ++++++ .../ComputersShopView/FormSellComputers.cs | 86 ++++ .../ComputersShopView/FormSellComputers.resx | 60 +++ .../ComputersShopView/FormShop.Designer.cs | 283 +++++++------ ComputersShop/ComputersShopView/FormShop.cs | 2 + ComputersShop/ComputersShopView/FormShop.resx | 3 + ComputersShop/ComputersShopView/Program.cs | 1 + 11 files changed, 800 insertions(+), 487 deletions(-) create mode 100644 ComputersShop/ComputersShopView/FormSellComputers.Designer.cs create mode 100644 ComputersShop/ComputersShopView/FormSellComputers.cs create mode 100644 ComputersShop/ComputersShopView/FormSellComputers.resx diff --git a/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs b/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs index fdf703f..e5ff2be 100644 --- a/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs +++ b/ComputersShop/ComputersShopDataModels/Models/IShopModel.cs @@ -11,7 +11,6 @@ namespace ComputersShopDataModels.Models public string ShopName { get; } public string ShopAddress { get; } DateTime DateOpening { get; } - public int Capacity { get; } Dictionary Computers { get; } } diff --git a/ComputersShop/ComputersShopFileImplement/Models/Shop.cs b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs index c805b81..750a7bf 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Shop.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs @@ -61,7 +61,7 @@ namespace ComputersShopFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ShopName = element.Element("ShopName")!.Value, - ShopAddress = element.Element("Address")!.Value, + ShopAddress = element.Element("ShopAddress")!.Value, DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), Capacity = Convert.ToInt32(element.Element("Capacity")!.Value), ComputersCount = element.Element("Computers")!.Elements("Computer") diff --git a/ComputersShop/ComputersShopView/FormMain.Designer.cs b/ComputersShop/ComputersShopView/FormMain.Designer.cs index 46cc466..d93a89f 100644 --- a/ComputersShop/ComputersShopView/FormMain.Designer.cs +++ b/ComputersShop/ComputersShopView/FormMain.Designer.cs @@ -1,194 +1,197 @@ namespace ComputersShopView { - partial class FormMain - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; + partial class FormMain + { + /// + /// 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); - } + /// + /// 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 + #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.menuStrip = new System.Windows.Forms.MenuStrip(); - this.справочникToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.computerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.componentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.магазиныToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.пополнениеМагазинаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.dataGridView = new System.Windows.Forms.DataGridView(); - this.buttonCreateOrder = new System.Windows.Forms.Button(); - this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); - this.buttonOrderReady = new System.Windows.Forms.Button(); - this.buttonIssuedOrder = new System.Windows.Forms.Button(); - this.buttonRef = new System.Windows.Forms.Button(); - this.menuStrip.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); - this.SuspendLayout(); - // - // menuStrip - // - this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.справочникToolStripMenuItem, - this.пополнениеМагазинаToolStripMenuItem}); - this.menuStrip.Location = new System.Drawing.Point(0, 0); - this.menuStrip.Name = "menuStrip"; - this.menuStrip.Size = new System.Drawing.Size(1047, 24); - this.menuStrip.TabIndex = 0; - this.menuStrip.Text = "menuStrip1"; - // - // справочникToolStripMenuItem - // - this.справочникToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.computerToolStripMenuItem, - this.componentsToolStripMenuItem, - this.магазиныToolStripMenuItem}); - this.справочникToolStripMenuItem.Name = "справочникToolStripMenuItem"; - this.справочникToolStripMenuItem.Size = new System.Drawing.Size(92, 20); - this.справочникToolStripMenuItem.Text = "справочники"; - // - // computerToolStripMenuItem - // - this.computerToolStripMenuItem.Name = "computerToolStripMenuItem"; - this.computerToolStripMenuItem.Size = new System.Drawing.Size(146, 22); - this.computerToolStripMenuItem.Text = "компьютеры"; - this.computerToolStripMenuItem.Click += new System.EventHandler(this.ComputersToolStripMenuItem_Click); - // - // componentsToolStripMenuItem - // - this.componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; - this.componentsToolStripMenuItem.Size = new System.Drawing.Size(146, 22); - this.componentsToolStripMenuItem.Text = "компоненты"; - this.componentsToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); - // - // магазиныToolStripMenuItem - // - this.магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(146, 22); - this.магазиныToolStripMenuItem.Text = "магазины"; - this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.ShopToolStripMenuItem_Click); - // - // пополнениеМагазинаToolStripMenuItem - // - this.пополнениеМагазинаToolStripMenuItem.Name = "пополнениеМагазинаToolStripMenuItem"; - this.пополнениеМагазинаToolStripMenuItem.Size = new System.Drawing.Size(141, 20); - this.пополнениеМагазинаToolStripMenuItem.Text = "пополнение магазина"; - this.пополнениеМагазинаToolStripMenuItem.Click += new System.EventHandler(this.shopReplenishmentToolStripMenuItem_Click); - // - // dataGridView - // - this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - 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(817, 411); - this.dataGridView.TabIndex = 1; - // - // buttonCreateOrder - // - this.buttonCreateOrder.Location = new System.Drawing.Point(835, 50); - this.buttonCreateOrder.Name = "buttonCreateOrder"; - this.buttonCreateOrder.Size = new System.Drawing.Size(200, 23); - this.buttonCreateOrder.TabIndex = 2; - this.buttonCreateOrder.Text = "Создать заказ"; - this.buttonCreateOrder.UseVisualStyleBackColor = true; - this.buttonCreateOrder.Click += new System.EventHandler(this.ButtonCreateOrder_Click); - // - // buttonTakeOrderInWork - // - this.buttonTakeOrderInWork.Location = new System.Drawing.Point(835, 101); - this.buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; - this.buttonTakeOrderInWork.Size = new System.Drawing.Size(200, 23); - this.buttonTakeOrderInWork.TabIndex = 3; - this.buttonTakeOrderInWork.Text = "Отдать на выполнение"; - this.buttonTakeOrderInWork.UseVisualStyleBackColor = true; - this.buttonTakeOrderInWork.Click += new System.EventHandler(this.ButtonTakeOrderInWork_Click); - // - // buttonOrderReady - // - this.buttonOrderReady.Location = new System.Drawing.Point(835, 156); - this.buttonOrderReady.Name = "buttonOrderReady"; - this.buttonOrderReady.Size = new System.Drawing.Size(200, 23); - this.buttonOrderReady.TabIndex = 4; - this.buttonOrderReady.Text = "Заказ готов"; - this.buttonOrderReady.UseVisualStyleBackColor = true; - this.buttonOrderReady.Click += new System.EventHandler(this.ButtonOrderReady_Click); - // - // buttonIssuedOrder - // - this.buttonIssuedOrder.Location = new System.Drawing.Point(835, 212); - this.buttonIssuedOrder.Name = "buttonIssuedOrder"; - this.buttonIssuedOrder.Size = new System.Drawing.Size(200, 23); - this.buttonIssuedOrder.TabIndex = 5; - this.buttonIssuedOrder.Text = "Заказ выдан"; - this.buttonIssuedOrder.UseVisualStyleBackColor = true; - this.buttonIssuedOrder.Click += new System.EventHandler(this.ButtonIssuedOrder_Click); - // - // buttonRef - // - this.buttonRef.Location = new System.Drawing.Point(835, 269); - this.buttonRef.Name = "buttonRef"; - this.buttonRef.Size = new System.Drawing.Size(200, 23); - this.buttonRef.TabIndex = 6; - this.buttonRef.Text = "Обновить список"; - this.buttonRef.UseVisualStyleBackColor = true; - this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); - // - // FormMain - // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1047, 450); - this.Controls.Add(this.buttonRef); - this.Controls.Add(this.buttonIssuedOrder); - this.Controls.Add(this.buttonOrderReady); - this.Controls.Add(this.buttonTakeOrderInWork); - this.Controls.Add(this.buttonCreateOrder); - this.Controls.Add(this.dataGridView); - this.Controls.Add(this.menuStrip); - this.MainMenuStrip = this.menuStrip; - this.Name = "FormMain"; - this.Text = "Магазин электроники"; - this.Load += new System.EventHandler(this.FormMain_Load); - this.menuStrip.ResumeLayout(false); - this.menuStrip.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); - this.ResumeLayout(false); - this.PerformLayout(); + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + menuStrip = new MenuStrip(); + справочникToolStripMenuItem = new ToolStripMenuItem(); + computerToolStripMenuItem = new ToolStripMenuItem(); + componentsToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); + пополнениеМагазинаToolStripMenuItem = new ToolStripMenuItem(); + продатьКомпьютеромToolStripMenuItem = new ToolStripMenuItem(); + dataGridView = new DataGridView(); + buttonCreateOrder = new Button(); + buttonTakeOrderInWork = new Button(); + buttonOrderReady = new Button(); + buttonIssuedOrder = new Button(); + buttonRef = new Button(); + menuStrip.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { справочникToolStripMenuItem, пополнениеМагазинаToolStripMenuItem, продатьКомпьютеромToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(1047, 24); + menuStrip.TabIndex = 0; + menuStrip.Text = "menuStrip1"; + // + // справочникToolStripMenuItem + // + справочникToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { computerToolStripMenuItem, componentsToolStripMenuItem, магазиныToolStripMenuItem }); + справочникToolStripMenuItem.Name = "справочникToolStripMenuItem"; + справочникToolStripMenuItem.Size = new Size(92, 20); + справочникToolStripMenuItem.Text = "справочники"; + // + // computerToolStripMenuItem + // + computerToolStripMenuItem.Name = "computerToolStripMenuItem"; + computerToolStripMenuItem.Size = new Size(146, 22); + computerToolStripMenuItem.Text = "компьютеры"; + computerToolStripMenuItem.Click += ComputersToolStripMenuItem_Click; + // + // componentsToolStripMenuItem + // + componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; + componentsToolStripMenuItem.Size = new Size(146, 22); + componentsToolStripMenuItem.Text = "компоненты"; + componentsToolStripMenuItem.Click += ComponentsToolStripMenuItem_Click; + // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(146, 22); + магазиныToolStripMenuItem.Text = "магазины"; + магазиныToolStripMenuItem.Click += ShopToolStripMenuItem_Click; + // + // пополнениеМагазинаToolStripMenuItem + // + пополнениеМагазинаToolStripMenuItem.Name = "пополнениеМагазинаToolStripMenuItem"; + пополнениеМагазинаToolStripMenuItem.Size = new Size(141, 20); + пополнениеМагазинаToolStripMenuItem.Text = "пополнение магазина"; + пополнениеМагазинаToolStripMenuItem.Click += shopReplenishmentToolStripMenuItem_Click; + // + // продатьКомпьютеромToolStripMenuItem + // + продатьКомпьютеромToolStripMenuItem.Name = "продатьКомпьютеромToolStripMenuItem"; + продатьКомпьютеромToolStripMenuItem.Size = new Size(129, 20); + продатьКомпьютеромToolStripMenuItem.Text = "продать компьютер"; + продатьКомпьютеромToolStripMenuItem.Click += ButtonSellComputer_Click; + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(12, 27); + dataGridView.Name = "dataGridView"; + dataGridView.RowTemplate.Height = 25; + dataGridView.Size = new Size(817, 411); + dataGridView.TabIndex = 1; + // + // buttonCreateOrder + // + buttonCreateOrder.Location = new Point(835, 50); + buttonCreateOrder.Name = "buttonCreateOrder"; + buttonCreateOrder.Size = new Size(200, 23); + buttonCreateOrder.TabIndex = 2; + buttonCreateOrder.Text = "Создать заказ"; + buttonCreateOrder.UseVisualStyleBackColor = true; + buttonCreateOrder.Click += ButtonCreateOrder_Click; + // + // buttonTakeOrderInWork + // + buttonTakeOrderInWork.Location = new Point(835, 101); + buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; + buttonTakeOrderInWork.Size = new Size(200, 23); + buttonTakeOrderInWork.TabIndex = 3; + buttonTakeOrderInWork.Text = "Отдать на выполнение"; + buttonTakeOrderInWork.UseVisualStyleBackColor = true; + buttonTakeOrderInWork.Click += ButtonTakeOrderInWork_Click; + // + // buttonOrderReady + // + buttonOrderReady.Location = new Point(835, 156); + buttonOrderReady.Name = "buttonOrderReady"; + buttonOrderReady.Size = new Size(200, 23); + buttonOrderReady.TabIndex = 4; + buttonOrderReady.Text = "Заказ готов"; + buttonOrderReady.UseVisualStyleBackColor = true; + buttonOrderReady.Click += ButtonOrderReady_Click; + // + // buttonIssuedOrder + // + buttonIssuedOrder.Location = new Point(835, 212); + buttonIssuedOrder.Name = "buttonIssuedOrder"; + buttonIssuedOrder.Size = new Size(200, 23); + buttonIssuedOrder.TabIndex = 5; + buttonIssuedOrder.Text = "Заказ выдан"; + buttonIssuedOrder.UseVisualStyleBackColor = true; + buttonIssuedOrder.Click += ButtonIssuedOrder_Click; + // + // buttonRef + // + buttonRef.Location = new Point(835, 269); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(200, 23); + buttonRef.TabIndex = 6; + buttonRef.Text = "Обновить список"; + buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += ButtonRef_Click; + // + // FormMain + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1047, 450); + Controls.Add(buttonRef); + Controls.Add(buttonIssuedOrder); + Controls.Add(buttonOrderReady); + Controls.Add(buttonTakeOrderInWork); + Controls.Add(buttonCreateOrder); + Controls.Add(dataGridView); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; + Name = "FormMain"; + Text = "Магазин электроники"; + Load += FormMain_Load; + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } - } + #endregion - #endregion - - private MenuStrip menuStrip; - private ToolStripMenuItem справочникToolStripMenuItem; - private DataGridView dataGridView; - private Button buttonCreateOrder; - private Button buttonTakeOrderInWork; - private Button buttonOrderReady; - private Button buttonIssuedOrder; - private Button buttonRef; - private ToolStripMenuItem computerToolStripMenuItem; - private ToolStripMenuItem componentsToolStripMenuItem; - private ToolStripMenuItem магазиныToolStripMenuItem; - private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; - } + private MenuStrip menuStrip; + private ToolStripMenuItem справочникToolStripMenuItem; + private DataGridView dataGridView; + private Button buttonCreateOrder; + private Button buttonTakeOrderInWork; + private Button buttonOrderReady; + private Button buttonIssuedOrder; + private Button buttonRef; + private ToolStripMenuItem computerToolStripMenuItem; + private ToolStripMenuItem componentsToolStripMenuItem; + private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; + private ToolStripMenuItem продатьКомпьютеромToolStripMenuItem; + } } \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormMain.cs b/ComputersShop/ComputersShopView/FormMain.cs index a307ef4..5c9d5ed 100644 --- a/ComputersShop/ComputersShopView/FormMain.cs +++ b/ComputersShop/ComputersShopView/FormMain.cs @@ -14,179 +14,189 @@ using System.Windows.Forms; namespace ComputersShopView { - public partial class FormMain : Form - { - private readonly ILogger _logger; - private readonly IOrderLogic _orderLogic; + public partial class FormMain : Form + { + private readonly ILogger _logger; + private readonly IOrderLogic _orderLogic; - public FormMain(ILogger logger, IOrderLogic orderLogic) - { - InitializeComponent(); - _logger = logger; - _orderLogic = orderLogic; - } + public FormMain(ILogger logger, IOrderLogic orderLogic) + { + InitializeComponent(); + _logger = logger; + _orderLogic = orderLogic; + } - private void FormMain_Load(object sender, EventArgs e) - { - LoadData(); - } - private void LoadData() - { - _logger.LogInformation("Загрузка заказов"); - try - { - var list = _orderLogic.ReadList(null); - if (list != null) - { - dataGridView.DataSource = list; - dataGridView.Columns["ComputerId"].Visible = false; - } - _logger.LogInformation("Загрузка заказов"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки заказов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); - if (service is FormComponents form) - { - form.ShowDialog(); - } - } - private void ComputersToolStripMenuItem_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormComputers)); - if (service is FormComputers form) - { - form.ShowDialog(); - } - } - private void ShopToolStripMenuItem_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + private void FormMain_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData() + { + _logger.LogInformation("Загрузка заказов"); + try + { + var list = _orderLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["ComputerId"].Visible = false; + } + _logger.LogInformation("Загрузка заказов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки заказов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ComponentsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); + if (service is FormComponents form) + { + form.ShowDialog(); + } + } + private void ComputersToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComputers)); + if (service is FormComputers form) + { + form.ShowDialog(); + } + } + private void ShopToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); - if (service is FormShops form) - { - form.ShowDialog(); - } - } - private void ButtonCreateOrder_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); - if (service is FormCreateOrder form) - { - form.ShowDialog(); - LoadData(); - } - } - private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id); - try - { - var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel - { - Id = id, - ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) - }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка передачи заказа в работу"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void ButtonOrderReady_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); - try - { - var operationResult = _orderLogic.FinishOrder(new OrderBindingModel - { - Id = id, - ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) - }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка отметки о готовности заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void ButtonIssuedOrder_Click(object sender, EventArgs e) - { - if (dataGridView.SelectedRows.Count == 1) - { - int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id); - try - { - var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel - { - Id = id, - ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) - }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } - _logger.LogInformation("Заказ №{id} выдан", id); - LoadData(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка отметки о выдачи заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } - private void ButtonRef_Click(object sender, EventArgs e) - { - LoadData(); - } + if (service is FormShops form) + { + form.ShowDialog(); + } + } + private void ButtonCreateOrder_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); + if (service is FormCreateOrder form) + { + form.ShowDialog(); + LoadData(); + } + } + private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id); + try + { + var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel + { + Id = id, + ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка передачи заказа в работу"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void ButtonOrderReady_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); + try + { + var operationResult = _orderLogic.FinishOrder(new OrderBindingModel + { + Id = id, + ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void ButtonIssuedOrder_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id); + try + { + var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = id, + ComputerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ComputerId"].Value), + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + _logger.LogInformation("Заказ №{id} выдан", id); + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о выдачи заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } - private void shopReplenishmentToolStripMenuItem_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormShopReplenishment)); + private void shopReplenishmentToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopReplenishment)); - if (service is FormShopReplenishment form) - { - form.ShowDialog(); - } - } - } + if (service is FormShopReplenishment form) + { + form.ShowDialog(); + } + } + + private void ButtonSellComputer_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellComputers)); + if (service is FormSellComputers form) + { + form.ShowDialog(); + LoadData(); + } + } + } } diff --git a/ComputersShop/ComputersShopView/FormSellComputers.Designer.cs b/ComputersShop/ComputersShopView/FormSellComputers.Designer.cs new file mode 100644 index 0000000..9e933c7 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormSellComputers.Designer.cs @@ -0,0 +1,124 @@ +namespace ComputersShopView +{ + partial class FormSellComputers + { + /// + /// 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() + { + ButtonCancel = new Button(); + comboBoxDocuments = new ComboBox(); + numericUpDownCount = new NumericUpDown(); + labelDocument = new Label(); + labelCount = new Label(); + ButtonSave = new Button(); + ((System.ComponentModel.ISupportInitialize)numericUpDownCount).BeginInit(); + SuspendLayout(); + // + // ButtonCancel + // + ButtonCancel.Location = new Point(233, 60); + ButtonCancel.Margin = new Padding(3, 2, 3, 2); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(82, 22); + ButtonCancel.TabIndex = 1; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; + // + // comboBoxDocuments + // + comboBoxDocuments.FormattingEnabled = true; + comboBoxDocuments.Location = new Point(89, 6); + comboBoxDocuments.Margin = new Padding(3, 2, 3, 2); + comboBoxDocuments.Name = "comboBoxDocuments"; + comboBoxDocuments.Size = new Size(226, 23); + comboBoxDocuments.TabIndex = 2; + // + // numericUpDownCount + // + numericUpDownCount.Location = new Point(90, 33); + numericUpDownCount.Margin = new Padding(3, 2, 3, 2); + numericUpDownCount.Name = "numericUpDownCount"; + numericUpDownCount.Size = new Size(225, 23); + numericUpDownCount.TabIndex = 3; + // + // labelDocument + // + labelDocument.AutoSize = true; + labelDocument.Location = new Point(12, 9); + labelDocument.Name = "labelDocument"; + labelDocument.Size = new Size(71, 15); + labelDocument.TabIndex = 4; + labelDocument.Text = "Компьютер"; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(11, 35); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(72, 15); + labelCount.TabIndex = 5; + labelCount.Text = "Количество"; + // + // ButtonSave + // + ButtonSave.Location = new Point(145, 60); + ButtonSave.Margin = new Padding(3, 2, 3, 2); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(82, 22); + ButtonSave.TabIndex = 6; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; + // + // FormSellComputers + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(326, 93); + Controls.Add(ButtonSave); + Controls.Add(labelCount); + Controls.Add(labelDocument); + Controls.Add(numericUpDownCount); + Controls.Add(comboBoxDocuments); + Controls.Add(ButtonCancel); + Margin = new Padding(3, 2, 3, 2); + Name = "FormSellComputers"; + Text = "Продажа компьютеров"; + ((System.ComponentModel.ISupportInitialize)numericUpDownCount).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + private Button ButtonCancel; + private ComboBox comboBoxDocuments; + private NumericUpDown numericUpDownCount; + private Label labelDocument; + private Label labelCount; + private Button ButtonSave; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormSellComputers.cs b/ComputersShop/ComputersShopView/FormSellComputers.cs new file mode 100644 index 0000000..323ff81 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormSellComputers.cs @@ -0,0 +1,86 @@ +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ComputersShopView +{ + public partial class FormSellComputers : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _shopLogic; + private readonly IComputerLogic _computerLogic; + private readonly List? _listComputer; + public FormSellComputers(ILogger logger, IShopLogic shopLogic, IComputerLogic computerLogic) + { + InitializeComponent(); + _logger = logger; + _shopLogic = shopLogic; + _computerLogic = computerLogic; + _listComputer = computerLogic.ReadList(null); + if (_listComputer != null) + { + comboBoxDocuments.DisplayMember = "ComputerName"; + comboBoxDocuments.ValueMember = "Id"; + comboBoxDocuments.DataSource = _listComputer; + comboBoxDocuments.SelectedItem = null; + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (comboBoxDocuments.SelectedValue == null) + { + MessageBox.Show("Выберите корабль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(numericUpDownCount.Text)) + { + MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Продажа поездок"); + try + { + var comp = _computerLogic.ReadElement(new() + { + Id = (int)comboBoxDocuments.SelectedValue + }); + if (comp == null) + { + throw new Exception("Компьютер не найден. Дополнительная информация в логах."); + } + var operationResult = _shopLogic.SellComputers( + computer: comp, + quantity: (int)numericUpDownCount.Value + ); + if (!operationResult) + { + throw new Exception("Ошибка при продаже компьютера. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения компьютера"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/ComputersShop/ComputersShopView/FormSellComputers.resx b/ComputersShop/ComputersShopView/FormSellComputers.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShopView/FormSellComputers.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/ComputersShop/ComputersShopView/FormShop.Designer.cs b/ComputersShop/ComputersShopView/FormShop.Designer.cs index 512ad30..94397db 100644 --- a/ComputersShop/ComputersShopView/FormShop.Designer.cs +++ b/ComputersShop/ComputersShopView/FormShop.Designer.cs @@ -28,170 +28,195 @@ /// private void InitializeComponent() { - dateTimePicker = new DateTimePicker(); + labelShop = new Label(); + labelAddress = new Label(); + labelDate = new Label(); textBoxName = new TextBox(); textBoxAddress = new TextBox(); - labelTime = new Label(); - labelAddress = new Label(); + dateTimePicker = new DateTimePicker(); dataGridView = new DataGridView(); - ColumnID = new DataGridViewTextBoxColumn(); - ColumnManufactureName = new DataGridViewTextBoxColumn(); - Цена = new DataGridViewTextBoxColumn(); - ColumnCount = new DataGridViewTextBoxColumn(); - labelShop = new Label(); - buttonSave = new Button(); - buttonCancel = new Button(); + ButtonSave = new Button(); + ButtonCancel = new Button(); + numericUpDownCapacity = new NumericUpDown(); + labelCapacity = new Label(); + ID = new DataGridViewTextBoxColumn(); + DocumentName = new DataGridViewTextBoxColumn(); + Price = new DataGridViewTextBoxColumn(); + Count = new DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownCapacity).BeginInit(); SuspendLayout(); // - // dateTimePicker - // - dateTimePicker.Location = new Point(384, 27); - dateTimePicker.Name = "dateTimePicker"; - dateTimePicker.Size = new Size(207, 23); - dateTimePicker.TabIndex = 26; - // - // textBoxName - // - textBoxName.Location = new Point(10, 27); - textBoxName.Name = "textBoxName"; - textBoxName.Size = new Size(141, 23); - textBoxName.TabIndex = 25; - // - // textBoxAddress - // - textBoxAddress.Location = new Point(158, 27); - textBoxAddress.Name = "textBoxAddress"; - textBoxAddress.Size = new Size(221, 23); - textBoxAddress.TabIndex = 24; - // - // labelTime - // - labelTime.AutoSize = true; - labelTime.Location = new Point(384, 9); - labelTime.Name = "labelTime"; - labelTime.Size = new Size(87, 15); - labelTime.TabIndex = 23; - labelTime.Text = "Дата открытия"; - // - // labelAddress - // - labelAddress.AutoSize = true; - labelAddress.Location = new Point(158, 9); - labelAddress.Name = "labelAddress"; - labelAddress.Size = new Size(40, 15); - labelAddress.TabIndex = 22; - labelAddress.Text = "Адрес"; - // - // dataGridView - // - dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; - dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnID, ColumnManufactureName, Цена, ColumnCount }); - dataGridView.Location = new Point(10, 56); - dataGridView.Name = "dataGridView"; - dataGridView.RowHeadersWidth = 62; - dataGridView.RowTemplate.Height = 25; - dataGridView.Size = new Size(581, 327); - dataGridView.TabIndex = 21; - // - // ColumnID - // - ColumnID.HeaderText = "ID"; - ColumnID.MinimumWidth = 8; - ColumnID.Name = "ColumnID"; - ColumnID.Visible = false; - ColumnID.Width = 150; - // - // ColumnManufactureName - // - ColumnManufactureName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - ColumnManufactureName.HeaderText = "Название компьютера"; - ColumnManufactureName.MinimumWidth = 8; - ColumnManufactureName.Name = "ColumnManufactureName"; - // - // Цена - // - Цена.HeaderText = "Цена"; - Цена.MinimumWidth = 6; - Цена.Name = "Цена"; - Цена.Width = 125; - // - // ColumnCount - // - ColumnCount.HeaderText = "Количество"; - ColumnCount.MinimumWidth = 8; - ColumnCount.Name = "ColumnCount"; - ColumnCount.Width = 150; - // // labelShop // labelShop.AutoSize = true; - labelShop.Location = new Point(10, 9); + labelShop.Location = new Point(10, 16); labelShop.Name = "labelShop"; labelShop.Size = new Size(54, 15); - labelShop.TabIndex = 20; + labelShop.TabIndex = 0; labelShop.Text = "Магазин"; // - // buttonSave + // labelAddress // - buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonSave.Location = new Point(363, 392); - buttonSave.Name = "buttonSave"; - buttonSave.Size = new Size(120, 22); - buttonSave.TabIndex = 28; - buttonSave.Text = "Сохранить"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += ButtonSave_Click; + labelAddress.AutoSize = true; + labelAddress.Location = new Point(156, 16); + labelAddress.Name = "labelAddress"; + labelAddress.Size = new Size(40, 15); + labelAddress.TabIndex = 1; + labelAddress.Text = "Адрес"; // - // buttonCancel + // labelDate // - buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonCancel.Location = new Point(488, 392); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(103, 22); - buttonCancel.TabIndex = 27; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += ButtonCancel_Click; + labelDate.AutoSize = true; + labelDate.Location = new Point(375, 16); + labelDate.Name = "labelDate"; + labelDate.Size = new Size(87, 15); + labelDate.TabIndex = 2; + labelDate.Text = "Дата открытия"; + // + // textBoxName + // + textBoxName.Location = new Point(10, 33); + textBoxName.Margin = new Padding(3, 2, 3, 2); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(140, 23); + textBoxName.TabIndex = 3; + // + // textBoxAddress + // + textBoxAddress.Location = new Point(156, 33); + textBoxAddress.Margin = new Padding(3, 2, 3, 2); + textBoxAddress.Name = "textBoxAddress"; + textBoxAddress.Size = new Size(216, 23); + textBoxAddress.TabIndex = 4; + // + // dateTimePicker + // + dateTimePicker.Location = new Point(375, 33); + dateTimePicker.Margin = new Padding(3, 2, 3, 2); + dateTimePicker.Name = "dateTimePicker"; + dateTimePicker.Size = new Size(123, 23); + dateTimePicker.TabIndex = 5; + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ID, DocumentName, Price, Count }); + dataGridView.Location = new Point(10, 58); + dataGridView.Margin = new Padding(3, 2, 3, 2); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 29; + dataGridView.Size = new Size(623, 248); + dataGridView.TabIndex = 6; + // + // ButtonSave + // + ButtonSave.Location = new Point(429, 310); + ButtonSave.Margin = new Padding(3, 2, 3, 2); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(97, 22); + ButtonSave.TabIndex = 7; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; + // + // ButtonCancel + // + ButtonCancel.Location = new Point(551, 310); + ButtonCancel.Margin = new Padding(3, 2, 3, 2); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(82, 22); + ButtonCancel.TabIndex = 8; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; + // + // numericUpDownCapacity + // + numericUpDownCapacity.Location = new Point(513, 33); + numericUpDownCapacity.Name = "numericUpDownCapacity"; + numericUpDownCapacity.Size = new Size(120, 23); + numericUpDownCapacity.TabIndex = 9; + // + // labelCapacity + // + labelCapacity.AutoSize = true; + labelCapacity.Location = new Point(513, 16); + labelCapacity.Name = "labelCapacity"; + labelCapacity.Size = new Size(80, 15); + labelCapacity.TabIndex = 10; + labelCapacity.Text = "Вместимость"; + // + // ID + // + ID.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + ID.HeaderText = "ID"; + ID.MinimumWidth = 6; + ID.Name = "ID"; + ID.Visible = false; + // + // DocumentName + // + DocumentName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + DocumentName.HeaderText = "Название компьютера"; + DocumentName.MinimumWidth = 6; + DocumentName.Name = "DocumentName"; + // + // Price + // + Price.HeaderText = "Стоимость"; + Price.Name = "Price"; + // + // Count + // + Count.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + Count.HeaderText = "Количество"; + Count.MinimumWidth = 6; + Count.Name = "Count"; // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(616, 425); - Controls.Add(buttonSave); - Controls.Add(buttonCancel); - Controls.Add(dateTimePicker); - Controls.Add(textBoxName); - Controls.Add(textBoxAddress); - Controls.Add(labelTime); - Controls.Add(labelAddress); + ClientSize = new Size(648, 338); + Controls.Add(labelCapacity); + Controls.Add(numericUpDownCapacity); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); Controls.Add(dataGridView); + Controls.Add(dateTimePicker); + Controls.Add(textBoxAddress); + Controls.Add(textBoxName); + Controls.Add(labelDate); + Controls.Add(labelAddress); Controls.Add(labelShop); Margin = new Padding(3, 2, 3, 2); Name = "FormShop"; - Text = "FormShop"; + Text = "Магазин"; Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownCapacity).EndInit(); ResumeLayout(false); PerformLayout(); } #endregion - private DateTimePicker dateTimePicker; + private Label labelShop; + private Label labelAddress; + private Label labelDate; private TextBox textBoxName; private TextBox textBoxAddress; - private Label labelTime; - private Label labelAddress; + private DateTimePicker dateTimePicker; private DataGridView dataGridView; - private Label labelShop; - private Button buttonSave; - private Button buttonCancel; - private DataGridViewTextBoxColumn ColumnID; - private DataGridViewTextBoxColumn ColumnManufactureName; - private DataGridViewTextBoxColumn Цена; - private DataGridViewTextBoxColumn ColumnCount; + private Button ButtonSave; + private Button ButtonCancel; + private NumericUpDown numericUpDownCapacity; + private Label labelCapacity; + private DataGridViewTextBoxColumn ID; + private DataGridViewTextBoxColumn DocumentName; + private DataGridViewTextBoxColumn Price; + private DataGridViewTextBoxColumn Count; } } \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormShop.cs b/ComputersShop/ComputersShopView/FormShop.cs index 6a1fda4..91ff44f 100644 --- a/ComputersShop/ComputersShopView/FormShop.cs +++ b/ComputersShop/ComputersShopView/FormShop.cs @@ -46,6 +46,7 @@ namespace ComputersShopView textBoxName.Text = view.ShopName; textBoxAddress.Text = view.ShopAddress.ToString(); dateTimePicker.Text = view.DateOpening.ToString(); + numericUpDownCapacity.Value = view.Capacity; _shopComputers = view.Computers ?? new Dictionary(); LoadData(); } @@ -100,6 +101,7 @@ namespace ComputersShopView ShopName = textBoxName.Text, ShopAddress = textBoxAddress.Text, DateOpening = dateTimePicker.Value.Date, + Capacity = (int)numericUpDownCapacity.Value, Computers = _shopComputers }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); diff --git a/ComputersShop/ComputersShopView/FormShop.resx b/ComputersShop/ComputersShopView/FormShop.resx index f298a7b..7920df3 100644 --- a/ComputersShop/ComputersShopView/FormShop.resx +++ b/ComputersShop/ComputersShopView/FormShop.resx @@ -57,4 +57,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index 6b7049f..68bf655 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -54,6 +54,7 @@ namespace ComputersShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file -- 2.25.1 From fe67f3f997f19f8eabca71a7de08b90a4406a403 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Mon, 24 Apr 2023 17:28:51 +0400 Subject: [PATCH 5/8] =?UTF-8?q?=D0=B2=D1=81=D1=91=20=D0=B5=D1=89=D1=91=20?= =?UTF-8?q?=D0=BD=D0=BE=D1=80=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ComputersShop/ComputersShopFileImplement/Models/Shop.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ComputersShop/ComputersShopFileImplement/Models/Shop.cs b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs index 750a7bf..29f7631 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Shop.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs @@ -96,11 +96,11 @@ namespace ComputersShopFileImplement.Models public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("ShopName", ShopName), - new XElement("Address", ShopAddress), + new XElement("ShopAddress", ShopAddress), new XElement("DateOpening", DateOpening.ToString()), new XElement("Capacity", Capacity.ToString()), - new XElement("Documents", ComputersCount.Select(x => - new XElement("Document", + new XElement("Сomputers", ComputersCount.Select(x => + new XElement("Сomputer", new XElement("Key", x.Key), new XElement("Value", x.Value))) .ToArray())); -- 2.25.1 From b0b92c6be33a13ae91e89e5a6193d8b9a31c4498 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Mon, 24 Apr 2023 20:40:36 +0400 Subject: [PATCH 6/8] =?UTF-8?q?=D0=B2=D1=81=D1=91=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs | 3 ++- ComputersShop/ComputersShopFileImplement/Models/Shop.cs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs index b33660f..1bd7990 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -32,7 +32,8 @@ namespace ComputersShopBusinessLogic.BusinessLogics if (quantity <= 0) { - throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity)); + return false; + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity)); } _logger.LogInformation("AddComputerInShop. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id); diff --git a/ComputersShop/ComputersShopFileImplement/Models/Shop.cs b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs index 29f7631..e2eb630 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Shop.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Shop.cs @@ -99,8 +99,8 @@ namespace ComputersShopFileImplement.Models new XElement("ShopAddress", ShopAddress), new XElement("DateOpening", DateOpening.ToString()), new XElement("Capacity", Capacity.ToString()), - new XElement("Сomputers", ComputersCount.Select(x => - new XElement("Сomputer", + new XElement("Computers", ComputersCount.Select(x => + new XElement("Computer", new XElement("Key", x.Key), new XElement("Value", x.Value))) .ToArray())); -- 2.25.1 From 76e2135bf29af8cd5b1dc63c153b712194bde172 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Mon, 1 May 2023 17:54:02 +0400 Subject: [PATCH 7/8] =?UTF-8?q?=D0=9E=D1=81=D1=82=D0=B0=D0=BB=D1=81=D1=8F?= =?UTF-8?q?=20=D0=B2=D0=BE=D0=BF=D1=80=D0=BE=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 77 ++++++++++--------- .../ComputersShopView/FormSellComputers.cs | 2 +- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs index 88c9961..a499e3a 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/ShopStorage.cs @@ -74,41 +74,48 @@ namespace ComputersShopFileImplement.Implements public bool SellComputers(IComputerModel model, int quantity) { - int availableQuantity = source.Shops.Select(x => x.Computers.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum(); - if (availableQuantity < quantity) - { - return false; - } - var shops = source.Shops.Where(x => x.Computers.ContainsKey(model.Id)); - foreach (var shop in shops) - { - int countInCurrentShop = shop.Computers[model.Id].Item2; - if (countInCurrentShop <= quantity) - { - shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, 0); - quantity -= countInCurrentShop; - } - else - { - shop.Computers[model.Id] = (shop.Computers[model.Id].Item1, countInCurrentShop - quantity); - quantity = 0; - } - Update(new ShopBindingModel - { - Id = shop.Id, - ShopName = shop.ShopName, - ShopAddress = shop.ShopAddress, - DateOpening = shop.DateOpening, - Computers = shop.Computers, - Capacity = shop.Capacity - }); - if (quantity == 0) - { - return true; - } - } - return false; - } + int hasCount = 0; + + source.Shops.ForEach(x => + { + if (x.Computers.TryGetValue(model.Id, out var pair)) + { + hasCount += pair.Item2; + } + }); + + if (hasCount < quantity) return false; + + source.Shops.ForEach(x => + { + if (x.Computers.TryGetValue(model.Id, out var pair)) + { + if (quantity >= pair.Item2) + { + quantity -= pair.Item2; + x.Computers[model.Id] = (model, 0); + } + else + { + x.Computers[model.Id] = (model, pair.Item2 - quantity); + quantity = 0; + } + + x.Update(new ShopBindingModel + { + Id = x.Id, + ShopAddress = x.ShopAddress, + Capacity = x.Capacity, + DateOpening = x.DateOpening, + ShopName = x.ShopName, + Computers = x.Computers + }); + } + }); + + source.SaveShops(); + return true; + } public ShopViewModel? Update(ShopBindingModel model) { diff --git a/ComputersShop/ComputersShopView/FormSellComputers.cs b/ComputersShop/ComputersShopView/FormSellComputers.cs index 323ff81..e301cec 100644 --- a/ComputersShop/ComputersShopView/FormSellComputers.cs +++ b/ComputersShop/ComputersShopView/FormSellComputers.cs @@ -39,7 +39,7 @@ namespace ComputersShopView { if (comboBoxDocuments.SelectedValue == null) { - MessageBox.Show("Выберите корабль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(numericUpDownCount.Text)) -- 2.25.1 From fb04d39405e03c6558e609b869542f78b93fe831 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Fri, 5 May 2023 10:56:10 +0400 Subject: [PATCH 8/8] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ca1c7a3..a299f6f 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +*.dll -- 2.25.1