From f8f49343f4addc80b50f757df4c574a78c4f5ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Sun, 26 Mar 2023 17:22:40 +0400 Subject: [PATCH 1/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=202=20=D0=BF=D0=BE=D1=81?= =?UTF-8?q?=D0=BB=D0=B5=20=D0=BF=D0=B0=D0=B4=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BlacksmithWorkshop/BlacksmithWorkshop.sln | 6 ++ .../BlacksmithWorkshopView.csproj | 1 + .../BlacksmithWorkshop/Program.cs | 2 +- .../BlacksmithWorkshopFileImplement.csproj | 13 +++ .../DataFileSingleton.cs | 47 ++++++++++ .../Implements/ComponentStorage.cs | 76 +++++++++++++++ .../Implements/ManufactureStorage.cs | 76 +++++++++++++++ .../Implements/OrderStorage.cs | 87 +++++++++++++++++ .../Models/Component.cs | 63 +++++++++++++ .../Models/Manufacture.cs | 94 +++++++++++++++++++ .../Models/Order.cs | 91 ++++++++++++++++++ 11 files changed, 555 insertions(+), 1 deletion(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/BlacksmithWorkshopFileImplement.csproj create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ComponentStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Component.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Manufacture.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop.sln b/BlacksmithWorkshop/BlacksmithWorkshop.sln index 57e91a7..2037fc1 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop.sln +++ b/BlacksmithWorkshop/BlacksmithWorkshop.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopBusinessL EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopListImplement", "BlacksmithWorkshopListImplement\BlacksmithWorkshopListImplement.csproj", "{D57FFF62-0A87-4995-9693-317A94689469}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{0FFB9D8A-1B02-4622-AD31-DB88F4201350}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {D57FFF62-0A87-4995-9693-317A94689469}.Debug|Any CPU.Build.0 = Debug|Any CPU {D57FFF62-0A87-4995-9693-317A94689469}.Release|Any CPU.ActiveCfg = Release|Any CPU {D57FFF62-0A87-4995-9693-317A94689469}.Release|Any CPU.Build.0 = Release|Any CPU + {0FFB9D8A-1B02-4622-AD31-DB88F4201350}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0FFB9D8A-1B02-4622-AD31-DB88F4201350}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0FFB9D8A-1B02-4622-AD31-DB88F4201350}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0FFB9D8A-1B02-4622-AD31-DB88F4201350}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj index 197531c..55b8a2d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj +++ b/BlacksmithWorkshop/BlacksmithWorkshop/BlacksmithWorkshopView.csproj @@ -26,6 +26,7 @@ + diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs index 8716cd5..49a2371 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs @@ -1,7 +1,7 @@ using BlacksmithWorkShopBusinessLogic.BusinessLogics; using BlacksmithWorkshopContracts.BusinessLogicContracts; using BlacksmithWorkshopContracts.StoragesContracts; -using BlacksmithWorkshopListImplement.Implements; +using BlacksmithWorkshopFileImplement.Implements; using BlacksmithWorkshopView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/BlacksmithWorkshopFileImplement.csproj b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/BlacksmithWorkshopFileImplement.csproj new file mode 100644 index 0000000..4b2145e --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/BlacksmithWorkshopFileImplement.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..a2b8f6c --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs @@ -0,0 +1,47 @@ +using BlacksmithWorkshopFileImplement.Models; +using System.Xml.Linq; +namespace BlacksmithWorkshopFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string ManufactureFileName = "Manufacture.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Manufactures { 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 SaveManufactures() => SaveData(Manufactures, ManufactureFileName, "Manufactures", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Manufactures = LoadData(ManufactureFileName, "Manufacture", x => Manufacture.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); + } + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ComponentStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..863c2e3 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,76 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs new file mode 100644 index 0000000..7805dd5 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ManufactureStorage.cs @@ -0,0 +1,76 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + public class ManufactureStorage : IManufactureStorage + { + private readonly DataFileSingleton source; + public ManufactureStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Manufactures + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName)) + { + return new(); + } + return source.Manufactures + .Where(x => x.ManufactureName.Contains(model.ManufactureName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ManufactureViewModel? GetElement(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName) && !model.Id.HasValue) + { + return null; + } + return source.Manufactures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ManufactureName) && x.ManufactureName == model.ManufactureName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ManufactureViewModel? Insert(ManufactureBindingModel model) + { + model.Id = source.Manufactures.Count > 0 ? source.Manufactures.Max(x => x.Id) + 1 : 1; + var newManufacture = Manufacture.Create(model); + if (newManufacture == null) + { + return null; + } + source.Manufactures.Add(newManufacture); + source.SaveManufactures(); + return newManufacture.GetViewModel; + } + public ManufactureViewModel? Update(ManufactureBindingModel model) + { + var manufacture = source.Manufactures.FirstOrDefault(x => x.Id == model.Id); + if (manufacture == null) + { + return null; + } + manufacture.Update(model); + source.SaveManufactures(); + return manufacture.GetViewModel; + } + public ManufactureViewModel? Delete(ManufactureBindingModel model) + { + var element = source.Manufactures.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Manufactures.Remove(element); + source.SaveManufactures(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..b0d6550 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,87 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; +using System.Collections.Generic; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + private OrderViewModel AddManufactureName (OrderViewModel model) + { + model.ManufactureName = source.Manufactures.SingleOrDefault(x => x.Id == model.ManufactureId)?.ManufactureName ?? string.Empty; + return model; + } + public List GetFullList() + { + return source.Orders + .Select(x => AddManufactureName(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (model.Id == null) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => AddManufactureName(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + var order = source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + if (order == null) + { + return null; + } + return AddManufactureName(order); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Component.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Component.cs new file mode 100644 index 0000000..a3aa6cc --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.Xml.Linq; + + +namespace BlacksmithWorkshopFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost {get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new + ( + "Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString()) + ); + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Manufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Manufacture.cs new file mode 100644 index 0000000..2ae1b94 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Manufacture.cs @@ -0,0 +1,94 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.Xml.Linq; +namespace BlacksmithWorkshopFileImplement.Models +{ + public class Manufacture : IManufactureModel + { + public int Id { get; private set; } + public string ManufactureName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _manufactureComponents = null; + public Dictionary ManufactureComponents + { + get + { + if (_manufactureComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _manufactureComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _manufactureComponents; + } + } + public static Manufacture? Create(ManufactureBindingModel model) + { + if (model == null) + { + return null; + } + return new Manufacture() + { + Id = model.Id, + ManufactureName = model.ManufactureName, + Price = model.Price, + Components = model.ManufactureComponents.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Manufacture? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Manufacture() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ManufactureName = element.Element("ManufactureName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("ManufactureComponents")!.Elements("ManufactureComponent") .ToDictionary + ( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + public void Update(ManufactureBindingModel model) + { + if (model == null) + { + return; + } + ManufactureName = model.ManufactureName; + Price = model.Price; + Components = model.ManufactureComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _manufactureComponents = null; + } + public ManufactureViewModel GetViewModel => new() + { + Id = Id, + ManufactureName = ManufactureName, + Price = Price, + ManufactureComponents = ManufactureComponents + }; + public XElement GetXElement => new + ( + "Manufacture", + new XAttribute("Id", Id), + new XElement("ManufactureName", ManufactureName), + new XElement("Price", Price.ToString()), + new XElement("ManufactureComponents", Components.Select + (x => new XElement + ( + "ManufactureComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value) + ) + ).ToArray()) + ); + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs new file mode 100644 index 0000000..8ce8605 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Order.cs @@ -0,0 +1,91 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Enums; +using BlacksmithWorkshopDataModels.Models; +using System.Xml.Linq; + +namespace BlacksmithWorkshopFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int ManufactureId {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 static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ManufactureId = model.ManufactureId, + 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; + } + DateTime? dateImplement = null; + if (element.Element("DateImplement")!.Value != "") + { + dateImplement = Convert.ToDateTime(element.Element("DateImplement")!.Value); + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ManufactureId = Convert.ToInt32(element.Element("ManufactureId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = dateImplement + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.DateImplement != null) + { + DateImplement = model.DateImplement; + } + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ManufactureId = ManufactureId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new + ( + "Order", + new XAttribute("Id", Id), + new XElement("ManufactureId", ManufactureId), + new XElement("Count", Count), + new XElement("Sum", Sum), + new XElement("Status", (int)Status), + new XElement("DateCreate", DateCreate), + new XElement("DateImplement", DateImplement) + ); + } +} \ No newline at end of file -- 2.25.1 From 349663d4995575d1be07e1f691ea5d39341572a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Mon, 24 Apr 2023 04:08:36 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=BB=D0=B0=D0=B12=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlacksmithWorkshop/FormMain.Designer.cs | 19 ++- .../BlacksmithWorkshop/FormMain.cs | 8 ++ .../FormSellManufacture.Designer.cs | 121 ++++++++++++++++++ .../BlacksmithWorkshop/FormSellManufacture.cs | 65 ++++++++++ .../FormSellManufacture.resx | 60 +++++++++ .../BlacksmithWorkshop/Program.cs | 1 + .../BusinessLogics/OrderLogic.cs | 24 +++- .../BusinessLogics/StoreLogic.cs | 55 +++++++- .../BindingModels/StoreBindingModel.cs | 1 + .../BusinessLogicsContracts/IStoreLogic.cs | 3 + .../StoragesContracts/IStoreStorage.cs | 14 ++ .../ViewModels/StoreViewModel.cs | 2 + .../Models/IStoreModel.cs | 1 + .../DataFileSingleton.cs | 4 + .../Implements/StoreStorage.cs | 117 +++++++++++++++++ .../Models/Store.cs | 108 ++++++++++++++++ .../Implements/StoreStorage.cs | 8 ++ .../Models/Store.cs | 8 +- 18 files changed, 611 insertions(+), 8 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.resx create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs index c593b65..305d5d8 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.buttonIssuedOrder = new System.Windows.Forms.Button(); this.buttonRef = new System.Windows.Forms.Button(); this.buttonFillStore = new System.Windows.Forms.Button(); + this.buttonSellManufacture = new System.Windows.Forms.Button(); this.menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -67,21 +68,21 @@ // componentsToolStripMenuItem // this.componentsToolStripMenuItem.Name = "componentsToolStripMenuItem"; - this.componentsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.componentsToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.componentsToolStripMenuItem.Text = "Компоненты"; this.componentsToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); // // manufacturesToolStripMenuItem // this.manufacturesToolStripMenuItem.Name = "manufacturesToolStripMenuItem"; - this.manufacturesToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.manufacturesToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.manufacturesToolStripMenuItem.Text = "Изделия"; this.manufacturesToolStripMenuItem.Click += new System.EventHandler(this.ManufacturesToolStripMenuItem_Click); // // storesToolStripMenuItem // this.storesToolStripMenuItem.Name = "storesToolStripMenuItem"; - this.storesToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.storesToolStripMenuItem.Size = new System.Drawing.Size(145, 22); this.storesToolStripMenuItem.Text = "Магазины"; this.storesToolStripMenuItem.Click += new System.EventHandler(this.StoresToolStripMenuItem_Click); // @@ -161,11 +162,22 @@ this.buttonFillStore.UseVisualStyleBackColor = true; this.buttonFillStore.Click += new System.EventHandler(this.ButtonFillStore_Click); // + // buttonSellManufacture + // + this.buttonSellManufacture.Location = new System.Drawing.Point(814, 201); + this.buttonSellManufacture.Name = "buttonSellManufacture"; + this.buttonSellManufacture.Size = new System.Drawing.Size(282, 23); + this.buttonSellManufacture.TabIndex = 3; + this.buttonSellManufacture.Text = "Продать изделие"; + this.buttonSellManufacture.UseVisualStyleBackColor = true; + this.buttonSellManufacture.Click += new System.EventHandler(this.ButtonSellManufacture_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1108, 504); + this.Controls.Add(this.buttonSellManufacture); this.Controls.Add(this.buttonFillStore); this.Controls.Add(this.buttonRef); this.Controls.Add(this.buttonIssuedOrder); @@ -200,5 +212,6 @@ private Button buttonRef; private ToolStripMenuItem storesToolStripMenuItem; private Button buttonFillStore; + private Button buttonSellManufacture; } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs index 2b86baf..9206973 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs @@ -162,5 +162,13 @@ namespace BlacksmithWorkshopView form.ShowDialog(); } } + private void ButtonSellManufacture_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellManufacture)); + if (service is FormSellManufacture form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.Designer.cs new file mode 100644 index 0000000..b5e000f --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.Designer.cs @@ -0,0 +1,121 @@ +namespace BlacksmithWorkshopView +{ + partial class FormSellManufacture + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSell = new System.Windows.Forms.Button(); + this.labelCount = new System.Windows.Forms.Label(); + this.labelManufacture = new System.Windows.Forms.Label(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.comboBoxManufacture = new System.Windows.Forms.ComboBox(); + this.SuspendLayout(); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(256, 70); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(75, 23); + this.buttonCancel.TabIndex = 15; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(175, 70); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(75, 23); + this.buttonSell.TabIndex = 16; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(12, 44); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(72, 15); + this.labelCount.TabIndex = 13; + this.labelCount.Text = "Количество"; + // + // labelManufacture + // + this.labelManufacture.AutoSize = true; + this.labelManufacture.Location = new System.Drawing.Point(12, 15); + this.labelManufacture.Name = "labelManufacture"; + this.labelManufacture.Size = new System.Drawing.Size(53, 15); + this.labelManufacture.TabIndex = 14; + this.labelManufacture.Text = "Изделие"; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(107, 41); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(224, 23); + this.textBoxCount.TabIndex = 12; + // + // comboBoxManufacture + // + this.comboBoxManufacture.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxManufacture.FormattingEnabled = true; + this.comboBoxManufacture.Location = new System.Drawing.Point(107, 12); + this.comboBoxManufacture.Name = "comboBoxManufacture"; + this.comboBoxManufacture.Size = new System.Drawing.Size(224, 23); + this.comboBoxManufacture.TabIndex = 11; + // + // FormSellManufacture + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(347, 107); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.labelManufacture); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.comboBoxManufacture); + this.Name = "FormSellManufacture"; + this.Text = "Продажа изделия"; + this.Load += new System.EventHandler(this.FormFillStore_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonCancel; + private Button buttonSell; + private Label labelCount; + private Label labelManufacture; + private TextBox textBoxCount; + private ComboBox comboBoxManufacture; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.cs new file mode 100644 index 0000000..50c89a8 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.cs @@ -0,0 +1,65 @@ +using BlacksmithWorkshopContracts.BusinessLogicContracts; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace BlacksmithWorkshopView +{ + public partial class FormSellManufacture : Form + { + private readonly ILogger _logger; + private readonly IStoreLogic _logicS; + private readonly IManufactureLogic _logicM; + public FormSellManufacture(ILogger logger, IStoreLogic logicS, IManufactureLogic logicM) + { + InitializeComponent(); + _logger = logger; + _logicS = logicS; + _logicM = logicM; + } + private void FormFillStore_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка изделий"); + var listM = _logicM.ReadList(null); + if (listM != null) + { + comboBoxManufacture.DisplayMember = "ManufactureName"; + comboBoxManufacture.ValueMember = "Id"; + comboBoxManufacture.DataSource = listM; + comboBoxManufacture.SelectedItem = null; + } + } + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxManufacture.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (Convert.ToInt32(textBoxCount.Text) <= 0) + { + MessageBox.Show("Количество должно быть больше нуля", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + var count = Convert.ToInt32(textBoxCount.Text); + var manufacture = _logicM.ReadElement(new() + { + Id = Convert.ToInt32(comboBoxManufacture.SelectedValue) + }); + if (manufacture == null || !_logicS.SellManufactures(manufacture, count)) + { + MessageBox.Show("Не удалось продать изделия. Информацию смотрите в логах", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.resx b/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormSellManufacture.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/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs index f025d7e..f0c2952 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs @@ -51,6 +51,7 @@ namespace BlacksmithWorkshopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index 007c1c7..d0ff0fd 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -1,5 +1,6 @@ using BlacksmithWorkshopContracts.BindingModels; using BlacksmithWorkshopContracts.BusinessLogicContracts; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; using BlacksmithWorkshopContracts.SearchModels; using BlacksmithWorkshopContracts.StoragesContracts; using BlacksmithWorkshopContracts.ViewModels; @@ -12,10 +13,14 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IManufactureStorage _manufactureStorage; + private readonly IStoreLogic _storeLogic; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IManufactureStorage manufactureStorage, IStoreLogic storeLogic) { _logger = logger; _orderStorage = orderStorage; + _manufactureStorage = manufactureStorage; + _storeLogic = storeLogic; } public List? ReadList(OrderSearchModel? model) { @@ -48,7 +53,22 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics { return true; } - } + if (newstatus == OrderStatus.Выдан) + { + if (vm == null) + { + return false; + } + var manufacture = _manufactureStorage.GetElement(new ManufactureSearchModel + { + Id = vm.ManufactureId + }); + if (manufacture == null || !_storeLogic.FillStore(manufacture, vm.Count)) + { + throw new Exception("Не удалось заполнить магазины"); + } + } + } _logger.LogWarning($"Changing order status of order {model.Id} to {newstatus} failed"); return false; } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs index 1024bc3..cdea605 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs @@ -142,7 +142,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics throw new ArgumentNullException("Нет адреса магазина", nameof(model.StoreName)); } _logger.LogInformation("Store. StoreName: {StoreName}. Address: {Address}. Id: {Id}", model.StoreName, model.Address, model.Id); - var element = _storeStorage.GetElement(new StoreSearchModel//проверка на уникальность + var element = _storeStorage.GetElement(new StoreSearchModel { StoreName = model.StoreName }); @@ -150,6 +150,59 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics { throw new InvalidOperationException("Магазин с таким названием уже есть"); } + if (model.MaxManufactures <= 0) + { + throw new ArgumentException("Максимальное количество изделий не может быть меньше единицы", nameof(model.MaxManufactures)); + } + } + public int GetFreePlace(int count) + { + int result; + var stores = _storeStorage.GetFullList(); + result = stores//Получаем общее число свободных мест в магазине + .Select(x => x.MaxManufactures/*максимальное число мест в магазине*/ - x.Manufactures.Select(m => m.Value.Item2).Sum()).Sum()/*количество занятых мест до добавления изделия*/; + result -= count;//Получаем общее число свободных мест после добавления изделия + return result; + } + public bool FillStore(IManufactureModel manufacture, int count) + { + if (count <= 0) + { + _logger.LogWarning("Количество добавляемых изделий должно быть больше 0."); + return false; + } + var freePlaces = GetFreePlace(count); + if (freePlaces < 0) + { + _logger.LogInformation("Магазины переполнены."); + return false; + } + foreach (var store in _storeStorage.GetFullList())//добавляем изделие во все магазины + { + var cnt = Math.Min(count, store.MaxManufactures - store.Manufactures.Select(x => x.Value.Item2).Sum()); + if (cnt <= 0) + { + continue; + } + if (!AddManufacture(manufacture, new StoreSearchModel() + { + Id = store.Id + }, cnt)) + { + _logger.LogWarning("При добавлении изделий произошла ошибка"); + return false; + } + count -= cnt; + if (count == 0) + { + return true; + } + } + return true; + } + public bool SellManufactures(IManufactureModel manufacture, int count) + { + return _storeStorage.SellManufacture(manufacture, count); } } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/StoreBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/StoreBindingModel.cs index 054387c..0039b55 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/StoreBindingModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/StoreBindingModel.cs @@ -12,5 +12,6 @@ namespace BlacksmithWorkshopContracts.BindingModels /// Изделия в магазине /// public Dictionary Manufactures { get; set; } = new(); + public int MaxManufactures { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IStoreLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IStoreLogic.cs index d0941d2..63d3cc8 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IStoreLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IStoreLogic.cs @@ -20,5 +20,8 @@ namespace BlacksmithWorkshopContracts.BusinessLogicsContracts /// Количество изделий /// bool AddManufacture(IManufactureModel manufacture, StoreSearchModel model, int count); + int GetFreePlace(int count); + bool FillStore(IManufactureModel manufacture, int count); + public bool SellManufactures(IManufactureModel manufacture, int needCount); } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs index 2eab6c8..d8502eb 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs @@ -13,5 +13,19 @@ namespace BlacksmithWorkshopContracts.StoragesContracts StoreViewModel? Insert(StoreBindingModel model); StoreViewModel? Update(StoreBindingModel model); StoreViewModel? Delete(StoreBindingModel model); + /// + /// Проверка наличия изделия в нужном количестве + /// + /// Изделие + /// Количество + /// + bool HasManufactures(IManufactureModel manufacture, int count); + /// + /// Продажа изделия в нужном количестве + /// + /// Изделие + /// Количество + /// + bool SellManufacture(IManufactureModel manufacture, int count); } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/StoreViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/StoreViewModel.cs index b7675d2..c92c958 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/StoreViewModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/StoreViewModel.cs @@ -17,5 +17,7 @@ namespace BlacksmithWorkshopContracts.ViewModels /// Изделия в магазине /// public Dictionary Manufactures { get; set; } = new(); + [DisplayName("Макс. изделий")] + public int MaxManufactures { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IStoreModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IStoreModel.cs index cf653ec..124ab1c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IStoreModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDataModels/Models/IStoreModel.cs @@ -6,5 +6,6 @@ string Address { get; } DateTime OpeningDate { get; } Dictionary Manufactures { get; } + int MaxManufactures { get; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs index a2b8f6c..a59ca30 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs @@ -8,9 +8,11 @@ namespace BlacksmithWorkshopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string ManufactureFileName = "Manufacture.xml"; + private readonly string StoreFileName = "Store.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Manufactures { get; private set; } + public List Stores { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -22,11 +24,13 @@ namespace BlacksmithWorkshopFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveManufactures() => SaveData(Manufactures, ManufactureFileName, "Manufactures", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveStores() => SaveData(Stores, StoreFileName, "Stores", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Manufactures = LoadData(ManufactureFileName, "Manufacture", x => Manufacture.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Stores = LoadData(StoreFileName, "Store", x => Store.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs new file mode 100644 index 0000000..192f3ac --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs @@ -0,0 +1,117 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using BlacksmithWorkshopFileImplement.Models; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + public class StoreStorage : IStoreStorage + { + private readonly DataFileSingleton source; + public StoreStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Stores + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(StoreSearchModel model) + { + if (model.Id.HasValue)//сначала ищем по Id + { + return source.Stores + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (!string.IsNullOrEmpty(model.StoreName))//далее по названию магазина + { + return source.Stores + .Where(x => x.StoreName.Contains(model.StoreName)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + public StoreViewModel? GetElement(StoreSearchModel model) + { + if (model.Id.HasValue)//сначала ищем по Id + { + return source.Stores + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.StoreName))//далее по названию магазина + { + return source.Stores + .FirstOrDefault(x => x.StoreName.Contains(model.StoreName))?.GetViewModel; + } + return null; + } + public StoreViewModel? Insert(StoreBindingModel model) + { + model.Id = source.Stores.Count > 0 ? source.Stores.Max(x => x.Id) + 1 : 1; + var newStore = Store.Create(model); + if (newStore == null) + { + return null; + } + source.Stores.Add(newStore); + source.SaveStores(); + return newStore.GetViewModel; + } + public StoreViewModel? Update(StoreBindingModel model) + { + var store = source.Stores.FirstOrDefault(x => x.StoreName == model.StoreName); + if (store == null) + { + return null; + } + store.Update(model); + source.SaveStores(); + return store.GetViewModel; + } + public StoreViewModel? Delete(StoreBindingModel model) + { + var element = source.Stores.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Stores.Remove(element); + source.SaveStores(); + return element.GetViewModel; + } + return null; + } + public bool HasManufactures(IManufactureModel manufacture, int count) + { + var currentCount = source.Stores + .Select(x => x.Manufactures + .FirstOrDefault(x => x.Key == manufacture.Id).Value.Item2) + .Sum(); + return currentCount >= count; + } + public bool SellManufacture(IManufactureModel manufacture, int count) + { + if (!HasManufactures(manufacture, count)) + { + return false; + } + foreach (var store in source.Stores.Where(x => x.Manufactures.ContainsKey(manufacture.Id))) + { + var difference = Math.Min(store.Manufactures[manufacture.Id].Item2, count); + store.Manufactures[manufacture.Id] = (store.Manufactures[manufacture.Id].Item1, store.Manufactures[manufacture.Id].Item2 - difference); + + count -= difference; + if (count <= 0) + { + return true; + } + } + return true; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs new file mode 100644 index 0000000..4399b77 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs @@ -0,0 +1,108 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.Diagnostics; +using System.Xml.Linq; + +namespace BlacksmithWorkshopFileImplement.Models +{ + public class Store : IStoreModel + { + public int Id { get; private set; } + public string StoreName { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + public DateTime OpeningDate { get; private set; } = DateTime.Now; + public Dictionary SavedManufactures { get; private set; } = new(); + private Dictionary? _manufactures = null; + public Dictionary Manufactures + { + get + { + if (_manufactures == null) + { + var source = DataFileSingleton.GetInstance(); + _manufactures = SavedManufactures.ToDictionary(x => x.Key, y => + ((source.Manufactures.FirstOrDefault(z => z.Id == y.Key) as IManufactureModel)!, + y.Value)); + } + return _manufactures; + } + } + public int MaxManufactures { get; private set; } + public static Store? Create(StoreBindingModel model) + { + if (model == null) + { + return null; + } + return new Store() + { + Id = model.Id, + StoreName = model.StoreName, + Address = model.Address, + OpeningDate = model.OpeningDate, + SavedManufactures = model.Manufactures.ToDictionary(x => x.Value.Item1.Id, x => x.Value.Item2), + MaxManufactures = model.MaxManufactures, + }; + } + public static Store? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Store() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + StoreName = element.Element("StoreName")!.Value, + Address = element.Element("Address")!.Value, + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + SavedManufactures = element.Element("Manufactures")!.Elements("Manufacture").ToDictionary + ( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ), + MaxManufactures = Convert.ToInt32(element.Element("MaxManufactures")!.Value) + }; + } + public void Update(StoreBindingModel model) + { + if (model == null) + { + return; + } + StoreName = model.StoreName; + Address = model.Address; + OpeningDate= model.OpeningDate; + SavedManufactures = model.Manufactures.ToDictionary(x => x.Value.Item1.Id, x => x.Value.Item2); + _manufactures = null; + MaxManufactures = model.MaxManufactures; + } + public StoreViewModel GetViewModel => new() + { + Id = Id, + StoreName = StoreName, + Address = Address, + OpeningDate = OpeningDate, + Manufactures = Manufactures, + MaxManufactures = MaxManufactures + }; + public XElement GetXElement => new + ( + "Store", + new XAttribute("Id", Id), + new XElement("StoreName", StoreName), + new XElement("Address", Address), + new XElement("OpeningDate", OpeningDate), + new XElement("Manufactures", SavedManufactures.Select + (x => new XElement + ( + "Manufacture", + new XElement("Key", x.Key), + new XElement("Value", x.Value) + ) + ).ToArray()), + new XElement("MaxManufactures", MaxManufactures) + ); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs index 3fb8c5d..1fd4c2e 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs @@ -100,5 +100,13 @@ namespace BlacksmithWorkshopListImplement.Implements } return null; } + public bool HasManufactures(IManufactureModel manufacture, int count) + { + throw new NotImplementedException("Не применяется в данной реализации"); + } + public bool SellManufacture(IManufactureModel manufacture, int count) + { + throw new NotImplementedException("Не применяется в данной реализации"); + } } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Store.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Store.cs index 28ce5d2..7060570 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Store.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Store.cs @@ -11,6 +11,7 @@ namespace BlacksmithWorkshopListImplement.Models public string Address { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary Manufactures { get; private set; } = new(); + public int MaxManufactures { get; private set; } public static Store? Create(StoreBindingModel? model) { if (model == null) @@ -23,7 +24,8 @@ namespace BlacksmithWorkshopListImplement.Models StoreName = model.StoreName, Address = model.Address, OpeningDate = model.OpeningDate, - Manufactures = new() + Manufactures = new(), + MaxManufactures = model.MaxManufactures }; } public void Update(StoreBindingModel? model) @@ -36,6 +38,7 @@ namespace BlacksmithWorkshopListImplement.Models Address = model.Address; OpeningDate = model.OpeningDate; Manufactures = model.Manufactures; + MaxManufactures = model.MaxManufactures; } public StoreViewModel GetViewModel => new() { @@ -43,7 +46,8 @@ namespace BlacksmithWorkshopListImplement.Models StoreName = StoreName, Address = Address, OpeningDate = OpeningDate, - Manufactures = Manufactures + Manufactures = Manufactures, + MaxManufactures = MaxManufactures }; } } -- 2.25.1 From 376ad6e63ff2f96d77ab292cd75db4d57801a6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Mon, 24 Apr 2023 04:12:10 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlacksmithWorkshop/FormStore.Designer.cs | 22 +++++++++++++++++++ .../BlacksmithWorkshop/FormStore.cs | 19 +++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.Designer.cs index 79e5adf..8df428b 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.Designer.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.Designer.cs @@ -40,6 +40,8 @@ this.ColumnManufactureName = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ColumnPrice = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.textBoxMaxManufactures = new System.Windows.Forms.TextBox(); + this.labelMaxManufactures = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); // @@ -143,6 +145,22 @@ this.ColumnCount.HeaderText = "Количество"; this.ColumnCount.Name = "ColumnCount"; // + // textBoxMaxManufactures + // + this.textBoxMaxManufactures.Location = new System.Drawing.Point(493, 15); + this.textBoxMaxManufactures.Name = "textBoxMaxManufactures"; + this.textBoxMaxManufactures.Size = new System.Drawing.Size(132, 23); + this.textBoxMaxManufactures.TabIndex = 4; + // + // labelMaxManufactures + // + this.labelMaxManufactures.AutoSize = true; + this.labelMaxManufactures.Location = new System.Drawing.Point(400, 18); + this.labelMaxManufactures.Name = "labelMaxManufactures"; + this.labelMaxManufactures.Size = new System.Drawing.Size(87, 15); + this.labelMaxManufactures.TabIndex = 6; + this.labelMaxManufactures.Text = "Макс. изделий"; + // // FormStore // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -154,8 +172,10 @@ this.Controls.Add(this.buttonSave); this.Controls.Add(this.labelOpeningDate); this.Controls.Add(this.labelAddress); + this.Controls.Add(this.labelMaxManufactures); this.Controls.Add(this.labelName); this.Controls.Add(this.textBoxAddress); + this.Controls.Add(this.textBoxMaxManufactures); this.Controls.Add(this.textBoxName); this.Name = "FormStore"; this.Text = "Магазин"; @@ -180,5 +200,7 @@ private DataGridViewTextBoxColumn ColumnManufactureName; private DataGridViewTextBoxColumn ColumnPrice; private DataGridViewTextBoxColumn ColumnCount; + private TextBox textBoxMaxManufactures; + private Label labelMaxManufactures; } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.cs index 1242c50..43837ff 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormStore.cs @@ -38,6 +38,7 @@ namespace BlacksmithWorkshopView textBoxName.Text = view.StoreName; textBoxAddress.Text = view.Address; dateTimePicker.Value = view.OpeningDate; + textBoxMaxManufactures.Text = view.MaxManufactures.ToString(); } LoadData(); } @@ -89,6 +90,21 @@ namespace BlacksmithWorkshopView MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (string.IsNullOrEmpty(textBoxMaxManufactures.Text)) + { + MessageBox.Show("Заполните максимальное количество изделий", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + int maxManufactures; + try//штоб программа не упала + { + maxManufactures = Convert.ToInt32(textBoxMaxManufactures.Text); + } + catch + { + MessageBox.Show("Неверное значение в поле Макс. изделий", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Сохранение магазина"); try { @@ -97,7 +113,8 @@ namespace BlacksmithWorkshopView Id = _id ?? 0, StoreName = textBoxName.Text, Address = textBoxAddress.Text, - OpeningDate = dateTimePicker.Value + OpeningDate = dateTimePicker.Value, + MaxManufactures = maxManufactures }; var operationResult = _id.HasValue ? _logicS.Update(model) : _logicS.Create(model); if (!operationResult) -- 2.25.1 From 3ac21871a68e929f67da714752d4b79e75ef072b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Tue, 9 May 2023 14:35:02 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/StoreLogic.cs | 7 +++++++ .../StoragesContracts/IStoreStorage.cs | 1 + .../Implements/StoreStorage.cs | 9 +++++++++ .../BlacksmithWorkshopFileImplement/Models/Store.cs | 1 - .../Implements/StoreStorage.cs | 5 +++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs index cdea605..bc06417 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/StoreLogic.cs @@ -6,6 +6,7 @@ using BlacksmithWorkshopContracts.StoragesContracts; using BlacksmithWorkshopContracts.ViewModels; using BlacksmithWorkshopDataModels.Models; using Microsoft.Extensions.Logging; +using System.Security.Cryptography.X509Certificates; namespace BlacksmithWorkshopBusinessLogic.BusinessLogics { @@ -109,6 +110,12 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics _logger.LogWarning("Store with id {Id} not found", model.Id); return false; } + var placeLeft = _storeStorage.GetPlaceLeft(selectedStore); + if (placeLeft < count) + { + _logger.LogWarning("Store with id {Id} can't accomodate {Count} items", model.Id, count); + return false; + } //ищем изделие в магазине if (selectedStore.Manufactures.TryGetValue(manufacture.Id, out var manufactureInStore)) { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs index d8502eb..503d958 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IStoreStorage.cs @@ -27,5 +27,6 @@ namespace BlacksmithWorkshopContracts.StoragesContracts /// Количество /// bool SellManufacture(IManufactureModel manufacture, int count); + int GetPlaceLeft(IStoreModel store); } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs index 192f3ac..4d8b744 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/StoreStorage.cs @@ -113,5 +113,14 @@ namespace BlacksmithWorkshopFileImplement.Implements } return true; } + public int GetPlaceLeft(IStoreModel model) + { + var store = source.Stores.FirstOrDefault(x => x.Id == model.Id); + if (store == null) + { + return 0; + } + return store.MaxManufactures - store.Manufactures.Select(x => x.Value.Item2).Sum(); + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs index 4399b77..265d7d1 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Store.cs @@ -76,7 +76,6 @@ namespace BlacksmithWorkshopFileImplement.Models OpeningDate= model.OpeningDate; SavedManufactures = model.Manufactures.ToDictionary(x => x.Value.Item1.Id, x => x.Value.Item2); _manufactures = null; - MaxManufactures = model.MaxManufactures; } public StoreViewModel GetViewModel => new() { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs index 1fd4c2e..078650a 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/StoreStorage.cs @@ -108,5 +108,10 @@ namespace BlacksmithWorkshopListImplement.Implements { throw new NotImplementedException("Не применяется в данной реализации"); } + + public int GetPlaceLeft(IStoreModel store) + { + throw new NotImplementedException("Не применяется в данной реализации"); + } } } \ No newline at end of file -- 2.25.1