From 4cc264503a4566ee313f88f33649a5777b635dd0 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Tue, 12 Mar 2024 21:53:02 +0400 Subject: [PATCH 1/7] First --- .../AbstractLawFirmFileImplement.csproj | 14 +++ .../DataFileSingleton.cs | 62 +++++++++++ .../Implements/ComponentStorage.cs | 90 +++++++++++++++ .../Models/Component.cs | 64 +++++++++++ .../Models/Document.cs | 103 ++++++++++++++++++ LawFirm/LawFirm.sln | 10 +- LawFirm/LawFirmView/LawFirmView.csproj | 1 + 7 files changed, 342 insertions(+), 2 deletions(-) create mode 100644 LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj create mode 100644 LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs diff --git a/LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj b/LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj new file mode 100644 index 0000000..f54052f --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/AbstractLawFirmFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs new file mode 100644 index 0000000..20958a4 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; +using AbstractLawFirmFileImplement.Models; + +namespace AbstractLawFirmFileImpliment +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string ProductFileName = "Product.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Documents { 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 SaveProducts() => SaveData(Products, ProductFileName, + "Products", x => x.GetXElement); + public void SaveOrders() { } + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => + Component.Create(x)!)!; + Documents = LoadData(ProductFileName, "Product", x => + Document.Create(x)!)!; + Orders = new List(); + } + private static List? LoadData(string filename, string xmlNodeName, + Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string + xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, + data.Select(selectFunction).ToArray())).Save(filename); + } + } + + } +} diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs new file mode 100644 index 0000000..c07e161 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs @@ -0,0 +1,90 @@ +using AbstractLawFirmContracts.BindingModels.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmFileImplement.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.Implements +{ + public class ComponentStorage + { + 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/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs new file mode 100644 index 0000000..2ff458b --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs @@ -0,0 +1,64 @@ +using AbstractLawFirmContracts.BindingModels.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractLawFirmFileImplement.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/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs new file mode 100644 index 0000000..4f52077 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs @@ -0,0 +1,103 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractLawFirmFileImplement.Models +{ + public class Document : IDocumentModel + { + public int Id { get; private set; } + public string DocumentName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _productComponents = + null; + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _productComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _productComponents; + } + } + public static Document? Create(DocumentBindingModel model) + { + if (model == null) + { + return null; + } + return new Document() + { + Id = model.Id, + DocumentName = model.DocumentName, + Price = model.Price, + Components = model.DocumentComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Document? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Document() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DocumentName = element.Element("ProductName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = + element.Element("ProductComponents")!.Elements("ProductComponent") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(DocumentBindingModel model) + { + if (model == null) + { + return; + } + DocumentName = model.DocumentName; + Price = model.Price; + Components = model.DocumentComponents.ToDictionary(x => x.Key, x => + x.Value.Item2); + _productComponents = null; + } + public DocumentViewModel GetViewModel => new() + { + Id = Id, + DocumentName = DocumentName, + Price = Price, + DocumentComponents = ProductComponents + }; + public XElement GetXElement => new("Product", + new XAttribute("Id", Id), + new XElement("ProductName", DocumentName), + new XElement("Price", Price.ToString()), + new XElement("ProductComponents", Components.Select(x => + new XElement("ProductComponent", + + new XElement("Key", x.Key), + + new XElement("Value", x.Value))) + + .ToArray())); + + public Dictionary DocumentComponents => throw new NotImplementedException(); + } +} diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index 09da4ff..e41b11c 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmDataModels", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmContracts", "AbstractLawFirmContracts\AbstractLawFirmContracts\AbstractLawFirmContracts.csproj", "{C0155B2E-5974-4835-996B-6FDF0F5BC06B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmBusinessLogic", "AbstractLawFirmBusinessLogic\AbstractLawFirmBusinessLogic.csproj", "{E48808B1-5381-4774-97B6-CDB107DCF98C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmBusinessLogic", "AbstractLawFirmBusinessLogic\AbstractLawFirmBusinessLogic.csproj", "{E48808B1-5381-4774-97B6-CDB107DCF98C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmListImplement", "AbstractLawFirmListImplement\AbstractLawFirmListImplement.csproj", "{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmListImplement", "AbstractLawFirmListImplement\AbstractLawFirmListImplement.csproj", "{86D5FC6F-B262-44A0-9D30-970F9EA93E0A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmFileImplement", "AbstractLawFirmFileImpliment\AbstractLawFirmFileImplement.csproj", "{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Debug|Any CPU.Build.0 = Debug|Any CPU {86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Release|Any CPU.ActiveCfg = Release|Any CPU {86D5FC6F-B262-44A0-9D30-970F9EA93E0A}.Release|Any CPU.Build.0 = Release|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AF960C2-208F-4B7B-9F8B-36B63446B6B7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index b6cfc13..c62edf7 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -15,6 +15,7 @@ + -- 2.25.1 From d242771ff1ec38c7631f76d34759f40679fc2cd5 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Wed, 13 Mar 2024 01:12:49 +0400 Subject: [PATCH 2/7] second --- .../DataFileSingleton.cs | 16 ++-- .../Implements/DocumentStorage.cs | 84 +++++++++++++++++ .../Implements/OrderStorage.cs | 94 +++++++++++++++++++ .../Models/Document.cs | 41 ++++---- .../Models/Order.cs | 94 +++++++++++++++++++ 5 files changed, 297 insertions(+), 32 deletions(-) create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs diff --git a/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs index 20958a4..a220336 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs @@ -27,16 +27,16 @@ namespace AbstractLawFirmFileImpliment } public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); - public void SaveProducts() => SaveData(Products, ProductFileName, - "Products", x => x.GetXElement); - public void SaveOrders() { } + public void SaveDocuments() => SaveData(Documents, ProductFileName, + "Documents", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, + "Orders", x => x.GetXElement); private DataFileSingleton() { - Components = LoadData(ComponentFileName, "Component", x => - Component.Create(x)!)!; - Documents = LoadData(ProductFileName, "Product", x => - Document.Create(x)!)!; - Orders = new List(); + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Documents = LoadData(ProductFileName, "Document", x => Document.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs new file mode 100644 index 0000000..9252324 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs @@ -0,0 +1,84 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmFileImplement.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.Implements +{ + public class DocumentStorage : IDocumentStorage + { + private readonly DataFileSingleton source; + + public DocumentStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) + { + return null; + } + return source.Documents.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + return source.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.Documents.Select(x => x.GetViewModel).ToList(); + } + + public DocumentViewModel? Insert(DocumentBindingModel model) + { + model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; + var newDoc = Document.Create(model); + if (newDoc == null) + { + return null; + } + source.Documents.Add(newDoc); + source.SaveDocuments(); + return newDoc.GetViewModel; + } + + public DocumentViewModel? Update(DocumentBindingModel model) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveDocuments(); + return document.GetViewModel; + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + source.Documents.Remove(document); + source.SaveDocuments(); + return document.GetViewModel; + } + + } +} \ No newline at end of file diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs new file mode 100644 index 0000000..2674094 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmFileImplement.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.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; + } + source.Orders.Remove(order); + source.SaveOrders(); + return GetViewModel(order); + } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) + { + viewModel.DocumentName = document.DocumentName; + } + return viewModel; + } + } +} \ No newline at end of file diff --git a/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs index 4f52077..3aacd44 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs @@ -17,20 +17,19 @@ namespace AbstractLawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Components { get; private set; } = new(); - private Dictionary? _productComponents = - null; - public Dictionary ProductComponents + private Dictionary? _documentComponents = null; + public Dictionary DocumentComponents { get { - if (_productComponents == null) + if (_documentComponents == null) { var source = DataFileSingleton.GetInstance(); - _productComponents = Components.ToDictionary(x => x.Key, y => + _documentComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); } - return _productComponents; + return _documentComponents; } } public static Document? Create(DocumentBindingModel model) @@ -57,10 +56,10 @@ namespace AbstractLawFirmFileImplement.Models return new Document() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), - DocumentName = element.Element("ProductName")!.Value, + DocumentName = element.Element("DocumentName")!.Value, Price = Convert.ToDouble(element.Element("Price")!.Value), Components = - element.Element("ProductComponents")!.Elements("ProductComponent") + element.Element("DocumentComponents")!.Elements("DocumentComponent") .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) @@ -76,28 +75,22 @@ namespace AbstractLawFirmFileImplement.Models Price = model.Price; Components = model.DocumentComponents.ToDictionary(x => x.Key, x => x.Value.Item2); - _productComponents = null; + _documentComponents = null; } public DocumentViewModel GetViewModel => new() { Id = Id, DocumentName = DocumentName, Price = Price, - DocumentComponents = ProductComponents + DocumentComponents = DocumentComponents }; - public XElement GetXElement => new("Product", - new XAttribute("Id", Id), - new XElement("ProductName", DocumentName), - new XElement("Price", Price.ToString()), - new XElement("ProductComponents", Components.Select(x => - new XElement("ProductComponent", - - new XElement("Key", x.Key), - - new XElement("Value", x.Value))) - - .ToArray())); - - public Dictionary DocumentComponents => throw new NotImplementedException(); + public XElement GetXElement => new("Document", + new XAttribute("Id", Id), + new XElement("DocumentName", DocumentName), + new XElement("Price", Price.ToString()), + new XElement("DocumentComponents", Components.Select(x => + new XElement("DocumentComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } } diff --git a/LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs new file mode 100644 index 0000000..fe2eb6f --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs @@ -0,0 +1,94 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Enums; +using AbstractLawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractLawFirmFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int DocumentId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + DocumentId = model.DocumentId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Id = model.Id; + DocumentId = model.DocumentId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + DocumentId = DocumentId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("DocumentId", DocumentId.ToString()), + 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()) + ); + } +} -- 2.25.1 From b937f4be4f0de1975639d2492c5b207e64f18b62 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Wed, 13 Mar 2024 09:35:22 +0400 Subject: [PATCH 3/7] final --- .../Implements/ComponentStorage.cs | 3 ++- LawFirm/LawFirmView/Program.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs index c07e161..b0bbb22 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs @@ -1,5 +1,6 @@ using AbstractLawFirmContracts.BindingModels.BindingModels; using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; using AbstractLawFirmContracts.ViewModels; using AbstractLawFirmFileImplement.Models; using AbstractLawFirmFileImpliment; @@ -11,7 +12,7 @@ using System.Threading.Tasks; namespace AbstractLawFirmFileImplement.Implements { - public class ComponentStorage + public class ComponentStorage: IComponentStorage { private readonly DataFileSingleton source; public ComponentStorage() diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index ab37d8c..cffdc50 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -1,7 +1,7 @@ using AbstractLawFirmBusinessLogic.BusinessLogic; using AbstractLawFirmContracts.BusinessLogicsContracts; using AbstractLawFirmContracts.StoragesContracts; -using AbstractLawFirmListImplement.Implements; +using AbstractLawFirmFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From acdae9d645c1bdd0162807639105404cb879b409 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Tue, 7 May 2024 21:28:35 +0400 Subject: [PATCH 4/7] =?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 --- .../BusinessLogic/OrderLogic.cs | 113 ++++++++++-- .../BusinessLogic/ShopLogic.cs | 50 ++++-- .../BindingModels/ShopBindingModel.cs | 3 +- .../BusinessLogicsContracts/IShopLogic.cs | 3 +- .../StoragesContracts/IShopStorage.cs | 4 +- .../ViewModels/ShopViewModel.cs | 4 +- .../Models/IShopModel.cs | 3 +- .../DataFileSingleton.cs | 98 +++++------ .../Implements/ComponentStorage.cs | 141 +++++++-------- .../Implements/DocumentStorage.cs | 124 +++++++------- .../Implements/OrderStorage.cs | 144 ++++++++-------- .../Implements/ShopStorage.cs | 137 +++++++++++++++ .../Models/Component.cs | 102 +++++------ .../Models/Document.cs | 159 +++++++++-------- .../Models/Order.cs | 161 +++++++++--------- .../Models/Shop.cs | 115 +++++++++++++ 16 files changed, 859 insertions(+), 502 deletions(-) create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs create mode 100644 LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs diff --git a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs index cf6376c..4b58926 100644 --- a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs @@ -4,6 +4,7 @@ using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.StoragesContracts; using AbstractLawFirmContracts.ViewModels; using AbstractLawFirmDataModels.Enums; +using AbstractLawFirmDataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -17,12 +18,17 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + private readonly IDocumentStorage _documentStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; - } + _shopLogic = shopLogic; + _documentStorage = documentStorage; + _shopStorage = shopStorage; + } public List? ReadList(OrderSearchModel? model) { @@ -45,7 +51,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic model.Status = OrderStatus.Принят; if (_orderStorage.Insert(model) == null) { - _logger.LogWarning("Insert operation failed"); + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); return false; } return true; @@ -53,8 +60,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic public bool ChangeStatus(OrderBindingModel model, OrderStatus status) { - CheckModel(model); - var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); if (element == null) { _logger.LogWarning("Read operation failed"); @@ -65,10 +72,31 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic _logger.LogWarning("Status change operation failed"); throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); } - model.Status = status; + if (status == OrderStatus.Готов) + { + var document = _documentStorage.GetElement(new DocumentSearchModel() { Id = model.DocumentId }); + if (document == null) + { + _logger.LogWarning("Status change operation failed. Car not found."); + return false; + } + + if (!CheckThenSupplyMany(document, model.Count)) + { + _logger.LogWarning("Status change operation failed. Shop supply error."); + return false; + } + } + model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; _orderStorage.Update(model); - return true; + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; } public bool TakeOrderInWork(OrderBindingModel model) @@ -97,7 +125,11 @@ true) { return; } - if (model.Sum <= 0) + if (model.DocumentId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор документа", nameof(model.DocumentId)); + } + if (model.Sum <= 0) { throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); } @@ -106,6 +138,67 @@ true) throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); } _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); - } + public bool CheckThenSupplyMany(IDocumentModel document, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Car count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountDocuments; + foreach (var c in shop.ShopDocuments) + { + freeSpace -= c.Value.Item2; + } + } + + if (freeSpace < count) + { + _logger.LogWarning("Check then supply operation error. There's no place for new cars in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountDocuments; + + foreach (var c in shop.ShopDocuments) + freeSpace -= c.Value.Item2; + + if (freeSpace <= 0) + continue; + + if (freeSpace >= count) + { + if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, count)) + count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace < count) + { + if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, freeSpace)) + count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + } } } diff --git a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ShopLogic.cs b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ShopLogic.cs index 9685675..e3273b5 100644 --- a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/ShopLogic.cs @@ -113,25 +113,37 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic } _logger.LogInformation("AddPlaneInShop find. Id:{Id}", element.Id); - if (element.ShopDocuments.TryGetValue(document.Id, out var pair)) - { - element.ShopDocuments[document.Id] = (document, count + pair.Item2); - _logger.LogInformation("AddPlaneInShop. Added {count} {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName); - } + int countDocuments = 0; + foreach (var c in element.ShopDocuments) + countDocuments += c.Value.Item2; + if (count > element.MaxCountDocuments - countDocuments) + { + _logger.LogWarning("Required shop will be overflowed"); + return false; + } else { - element.ShopDocuments[document.Id] = (document, count); - _logger.LogInformation("AddPlaneInShop. Added {count} new plane {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName); - } + if (element.ShopDocuments.TryGetValue(document.Id, out var pair)) + { + element.ShopDocuments[document.Id] = (document, count + pair.Item2); + _logger.LogInformation("AddPlaneInShop. Added {count} {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName); + } + else + { + element.ShopDocuments[document.Id] = (document, count); + _logger.LogInformation("AddPlaneInShop. Added {count} new plane {plane} to '{ShopName}' shop", count, document.DocumentName, element.ShopName); + } + } - _shopStorage.Update(new() - { - Id = element.Id, - Address = element.Address, - ShopName = element.ShopName, - OpeningDate = element.OpeningDate, - ShopDocuments = element.ShopDocuments - }); + _shopStorage.Update(new() + { + Id = element.Id, + Address = element.Address, + ShopName = element.ShopName, + MaxCountDocuments = element.MaxCountDocuments, + OpeningDate = element.OpeningDate, + ShopDocuments = element.ShopDocuments + }); return true; } private void CheckModel(ShopBindingModel model, bool withParams = true) @@ -158,5 +170,9 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic throw new InvalidOperationException("Магазин с таким названием уже есть"); } } - } + public bool SellDocument(IDocumentModel document, int count) + { + return _shopStorage.SellDocument(document, count); + } + } } diff --git a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BindingModels/ShopBindingModel.cs b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BindingModels/ShopBindingModel.cs index b3fb02d..7b88f1b 100644 --- a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BindingModels/ShopBindingModel.cs +++ b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BindingModels/ShopBindingModel.cs @@ -22,5 +22,6 @@ namespace AbstractLawFirmContracts.BindingModels get; set; } = new(); - } + public int MaxCountDocuments { get; set; } + } } diff --git a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BusinessLogicsContracts/IShopLogic.cs b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BusinessLogicsContracts/IShopLogic.cs index da2f762..dea32a0 100644 --- a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,6 @@ namespace AbstractLawFirmContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool SupplyDocuments(ShopSearchModel model, IDocumentModel document, int count); - } + bool SellDocument(IDocumentModel document, int count); + } } diff --git a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/StoragesContracts/IShopStorage.cs b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/StoragesContracts/IShopStorage.cs index 61e307f..fed4e4a 100644 --- a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/StoragesContracts/IShopStorage.cs +++ b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using AbstractLawFirmContracts.BindingModels; using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -17,5 +18,6 @@ namespace AbstractLawFirmContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); - } + bool SellDocument(IDocumentModel model, int count); + } } diff --git a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/ViewModels/ShopViewModel.cs b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/ViewModels/ShopViewModel.cs index e4de58c..71fae44 100644 --- a/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/ViewModels/ShopViewModel.cs +++ b/LawFirm/AbstractLawFirmContracts/AbstractLawFirmContracts/ViewModels/ShopViewModel.cs @@ -25,5 +25,7 @@ namespace AbstractLawFirmContracts.ViewModels get; set; } = new(); - } + [DisplayName("Максимальное количество пакетов документов в магазине")] + public int MaxCountDocuments { get; set; } + } } diff --git a/LawFirm/AbstractLawFirmDataModels/AbstractLawFirmDataModels/Models/IShopModel.cs b/LawFirm/AbstractLawFirmDataModels/AbstractLawFirmDataModels/Models/IShopModel.cs index cdca195..88346e9 100644 --- a/LawFirm/AbstractLawFirmDataModels/AbstractLawFirmDataModels/Models/IShopModel.cs +++ b/LawFirm/AbstractLawFirmDataModels/AbstractLawFirmDataModels/Models/IShopModel.cs @@ -12,5 +12,6 @@ namespace AbstractLawFirmDataModels.Models String Address { get; } DateTime OpeningDate { get; } Dictionary ShopDocuments { get; } - } + int MaxCountDocuments { get; } + } } diff --git a/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs index a220336..19349ba 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/DataFileSingleton.cs @@ -8,55 +8,55 @@ using AbstractLawFirmFileImplement.Models; namespace AbstractLawFirmFileImpliment { - internal class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string ComponentFileName = "Component.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string ProductFileName = "Product.xml"; - public List Components { get; private set; } - public List Orders { get; private set; } - public List Documents { 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 SaveDocuments() => SaveData(Documents, ProductFileName, - "Documents", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, - "Orders", x => x.GetXElement); - private DataFileSingleton() - { - Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - Documents = LoadData(ProductFileName, "Document", x => Document.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string DocumentFileName = "Document.xml"; + private readonly string ShopFileName = "Shop.xml"; - } - 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); - } - } + public List Components { get; private set; } + public List Orders { get; private set; } + public List Documents { get; private set; } + public List Shops { 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 SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Documents = LoadData(DocumentFileName, "Document", x => Document.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)) + { + 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/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs index b0bbb22..2e96e16 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/ComponentStorage.cs @@ -12,80 +12,69 @@ using System.Threading.Tasks; namespace AbstractLawFirmFileImplement.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; - } - } + 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/LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs index 9252324..d644e5f 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/DocumentStorage.cs @@ -12,73 +12,73 @@ using System.Threading.Tasks; namespace AbstractLawFirmFileImplement.Implements { - public class DocumentStorage : IDocumentStorage - { - private readonly DataFileSingleton source; + public class DocumentStorage : IDocumentStorage + { + private readonly DataFileSingleton source; - public DocumentStorage() - { - source = DataFileSingleton.GetInstance(); - } + public DocumentStorage() + { + source = DataFileSingleton.GetInstance(); + } - public DocumentViewModel? GetElement(DocumentSearchModel model) - { - if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) - { - return null; - } - return source.Documents.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) + { + return null; + } + return source.Documents.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } - public List GetFilteredList(DocumentSearchModel model) - { - if (string.IsNullOrEmpty(model.DocumentName)) - { - return new(); - } - return source.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList(); - } + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + return source.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList(); + } - public List GetFullList() - { - return source.Documents.Select(x => x.GetViewModel).ToList(); - } + public List GetFullList() + { + return source.Documents.Select(x => x.GetViewModel).ToList(); + } - public DocumentViewModel? Insert(DocumentBindingModel model) - { - model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; - var newDoc = Document.Create(model); - if (newDoc == null) - { - return null; - } - source.Documents.Add(newDoc); - source.SaveDocuments(); - return newDoc.GetViewModel; - } + public DocumentViewModel? Insert(DocumentBindingModel model) + { + model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; + var newDoc = Document.Create(model); + if (newDoc == null) + { + return null; + } + source.Documents.Add(newDoc); + source.SaveDocuments(); + return newDoc.GetViewModel; + } - public DocumentViewModel? Update(DocumentBindingModel model) - { - var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (document == null) - { - return null; - } - document.Update(model); - source.SaveDocuments(); - return document.GetViewModel; - } - public DocumentViewModel? Delete(DocumentBindingModel model) - { - var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (document == null) - { - return null; - } - source.Documents.Remove(document); - source.SaveDocuments(); - return document.GetViewModel; - } + public DocumentViewModel? Update(DocumentBindingModel model) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveDocuments(); + return document.GetViewModel; + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + source.Documents.Remove(document); + source.SaveDocuments(); + return document.GetViewModel; + } - } + } } \ No newline at end of file diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs index 2674094..a92dc56 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/OrderStorage.cs @@ -12,83 +12,83 @@ using System.Threading.Tasks; namespace AbstractLawFirmFileImplement.Implements { - public class OrderStorage : IOrderStorage - { - private readonly DataFileSingleton source; + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; - public OrderStorage() - { - source = DataFileSingleton.GetInstance(); - } + 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 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 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 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? 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; - } - source.Orders.Remove(order); - source.SaveOrders(); - return GetViewModel(order); - } + 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; + } + source.Orders.Remove(order); + source.SaveOrders(); + return GetViewModel(order); + } - private OrderViewModel GetViewModel(Order order) - { - var viewModel = order.GetViewModel; - var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (document != null) - { - viewModel.DocumentName = document.DocumentName; - } - return viewModel; - } - } + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) + { + viewModel.DocumentName = document.DocumentName; + } + return viewModel; + } + } } \ No newline at end of file diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs new file mode 100644 index 0000000..1038019 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs @@ -0,0 +1,137 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.ShopName ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + return source.Shops.Select(shop => shop.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 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; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + + public bool SellDocument(IDocumentModel model, int count) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + + if (document == null) + { + return false; + } + + + var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(c => c.Value.Item1.Id == document.Id)); + + int countStore = 0; + + foreach (var it in shopDocuments) + countStore += it.Value.Item2; + + if (count > countStore) + return false; + + foreach (var shop in source.Shops) + { + var documents = shop.ShopDocuments; + + foreach (var c in documents.Where(x => x.Value.Item1.Id == document.Id)) + { + int min = Math.Min(c.Value.Item2, count); + documents[c.Value.Item1.Id] = (c.Value.Item1, c.Value.Item2 - min); + count -= min; + + if (count <= 0) + break; + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + MaxCountDocuments = shop.MaxCountDocuments, + OpeningDate = shop.OpeningDate, + ShopDocuments = documents + }); + + source.SaveShops(); + + if (count <= 0) + return true; + } + + return true; + } + } +} diff --git a/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs index 2ff458b..73d0ac8 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Component.cs @@ -10,55 +10,55 @@ using System.Xml.Linq; namespace AbstractLawFirmFileImplement.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())); - } + 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/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs index 3aacd44..2e0da08 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs @@ -11,86 +11,81 @@ using System.Xml.Linq; namespace AbstractLawFirmFileImplement.Models { - public class Document : IDocumentModel - { - public int Id { get; private set; } - public string DocumentName { get; private set; } = string.Empty; - public double Price { get; private set; } - public Dictionary Components { get; private set; } = new(); - private Dictionary? _documentComponents = null; - public Dictionary DocumentComponents - { - get - { - if (_documentComponents == null) - { - var source = DataFileSingleton.GetInstance(); - _documentComponents = Components.ToDictionary(x => x.Key, y => - ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, - y.Value)); - } - return _documentComponents; - } - } - public static Document? Create(DocumentBindingModel model) - { - if (model == null) - { - return null; - } - return new Document() - { - Id = model.Id, - DocumentName = model.DocumentName, - Price = model.Price, - Components = model.DocumentComponents.ToDictionary(x => x.Key, x - => x.Value.Item2) - }; - } - public static Document? Create(XElement element) - { - if (element == null) - { - return null; - } - return new Document() - { - Id = Convert.ToInt32(element.Attribute("Id")!.Value), - DocumentName = element.Element("DocumentName")!.Value, - Price = Convert.ToDouble(element.Element("Price")!.Value), - Components = - element.Element("DocumentComponents")!.Elements("DocumentComponent") - .ToDictionary(x => - Convert.ToInt32(x.Element("Key")?.Value), x => - Convert.ToInt32(x.Element("Value")?.Value)) - }; - } - public void Update(DocumentBindingModel model) - { - if (model == null) - { - return; - } - DocumentName = model.DocumentName; - Price = model.Price; - Components = model.DocumentComponents.ToDictionary(x => x.Key, x => - x.Value.Item2); - _documentComponents = null; - } - public DocumentViewModel GetViewModel => new() - { - Id = Id, - DocumentName = DocumentName, - Price = Price, - DocumentComponents = DocumentComponents - }; - public XElement GetXElement => new("Document", - new XAttribute("Id", Id), - new XElement("DocumentName", DocumentName), - new XElement("Price", Price.ToString()), - new XElement("DocumentComponents", Components.Select(x => - new XElement("DocumentComponent", - new XElement("Key", x.Key), - new XElement("Value", x.Value))).ToArray())); - } + public class Document : IDocumentModel + { + public int Id { get; private set; } + public string DocumentName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _documentComponents = null; + public Dictionary DocumentComponents + { + get + { + if (_documentComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _documentComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _documentComponents; + } + } + public static Document? Create(DocumentBindingModel model) + { + if (model == null) + { + return null; + } + return new Document() + { + Id = model.Id, + DocumentName = model.DocumentName, + Price = model.Price, + Components = model.DocumentComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Document? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Document() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DocumentName = element.Element("DocumentName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("DocumentComponents")!.Elements("DocumentComponent").ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(DocumentBindingModel model) + { + if (model == null) + { + return; + } + DocumentName = model.DocumentName; + Price = model.Price; + Components = model.DocumentComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _documentComponents = null; + } + public DocumentViewModel GetViewModel => new() + { + Id = Id, + DocumentName = DocumentName, + Price = Price, + DocumentComponents = DocumentComponents + }; + public XElement GetXElement => new("Document", + new XAttribute("Id", Id), + new XElement("DocumentName", DocumentName), + new XElement("Price", Price.ToString()), + new XElement("DocumentComponents", Components.Select(x => + new XElement("DocumentComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } } diff --git a/LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs index fe2eb6f..b0a6b78 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Order.cs @@ -12,83 +12,88 @@ using System.Xml.Linq; namespace AbstractLawFirmFileImplement.Models { - public class Order : IOrderModel - { - public int Id { get; private set; } - public int DocumentId { get; private set; } - public int Count { get; private set; } - public double Sum { get; private set; } - public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; private set; } - public DateTime? DateImplement { get; private set; } - public static Order? Create(OrderBindingModel? model) - { - if (model == null) - { - return null; - } - return new Order() - { - Id = model.Id, - DocumentId = model.DocumentId, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement, - }; - } - public static Order? Create(XElement element) - { - if (element == null) - { - return null; - } - return new Order() - { - Id = Convert.ToInt32(element.Attribute("Id")!.Value), - DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value), - Sum = Convert.ToDouble(element.Element("Sum")!.Value), - Count = Convert.ToInt32(element.Element("Count")!.Value), - Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), - DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), - DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) - }; - } - public void Update(OrderBindingModel? model) - { - if (model == null) - { - return; - } - Id = model.Id; - DocumentId = model.DocumentId; - Count = model.Count; - Sum = model.Sum; - Status = model.Status; - DateCreate = model.DateCreate; - DateImplement = model.DateImplement; - } - public OrderViewModel GetViewModel => new() - { - Id = Id, - DocumentId = DocumentId, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - }; + public class Order : IOrderModel + { + public int DocumentId { get; private set; } - public XElement GetXElement => new( - "Order", - new XAttribute("Id", Id), - new XElement("DocumentId", DocumentId.ToString()), - 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()) - ); - } + 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 + { + DocumentId = model.DocumentId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value) + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + DocumentId = DocumentId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + + public XElement GetXElement => new( + "Order", + new XAttribute("Id", Id), + new XElement("DocumentId", DocumentId.ToString()), + 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/LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs new file mode 100644 index 0000000..8ffb924 --- /dev/null +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs @@ -0,0 +1,115 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; +using AbstractLawFirmFileImpliment; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractLawFirmFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + public int MaxCountDocuments { get; private set; } + public DateTime OpeningDate { get; private set; } + public Dictionary Documents { get; private set; } = new(); + private Dictionary? _shopDocuments = null; + public Dictionary ShopDocuments + { + get + { + if (_shopDocuments == null) + { + var source = DataFileSingleton.GetInstance(); + _shopDocuments = Documents.ToDictionary( + x => x.Key, + y => ((source.Documents.FirstOrDefault(z => z.Id == y.Key) as IDocumentModel)!, y.Value) + ); + } + return _shopDocuments; + } + } + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + MaxCountDocuments = model.MaxCountDocuments, + OpeningDate = model.OpeningDate, + Documents = model.ShopDocuments.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, + Address = element.Element("Address")!.Value, + MaxCountDocuments = Convert.ToInt32(element.Element("MaxCountDocuments")!.Value), + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Documents = element.Element("ShopDocuments")!.Elements("ShopDocument").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; + Address = model.Address; + MaxCountDocuments = model.MaxCountDocuments; + OpeningDate = model.OpeningDate; + if (model.ShopDocuments.Count > 0) + { + Documents = model.ShopDocuments.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopDocuments = null; + } + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + MaxCountDocuments = MaxCountDocuments, + OpeningDate = OpeningDate, + ShopDocuments = ShopDocuments, + }; + + public XElement GetXElement => new( + "Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("MaxCountDocuments", MaxCountDocuments), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopDocuments", Documents.Select(x => + new XElement("ShopDocument", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} +} -- 2.25.1 From 8bb9a0fb5b478ac1ae75bc5796de460d6d133f9d Mon Sep 17 00:00:00 2001 From: GokaPek Date: Fri, 21 Jun 2024 17:24:02 +0400 Subject: [PATCH 5/7] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B4=D0=BE=D0=BB=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/OrderLogic.cs | 195 +++++++++--------- 1 file changed, 99 insertions(+), 96 deletions(-) diff --git a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs index 4b58926..581b132 100644 --- a/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/LawFirm/AbstractLawFirmBusinessLogic/BusinessLogic/OrderLogic.cs @@ -18,17 +18,18 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - private readonly IShopStorage _shopStorage; - private readonly IShopLogic _shopLogic; - private readonly IDocumentStorage _documentStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage) + private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + private readonly IDocumentStorage _documentStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IDocumentStorage documentStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; - _shopLogic = shopLogic; - _documentStorage = documentStorage; - _shopStorage = shopStorage; - } + _shopLogic = shopLogic; + _documentStorage = documentStorage; + _shopStorage = shopStorage; + } public List? ReadList(OrderSearchModel? model) { @@ -51,8 +52,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic model.Status = OrderStatus.Принят; if (_orderStorage.Insert(model) == null) { - model.Status = OrderStatus.Неизвестен; - _logger.LogWarning("Insert operation failed"); + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); return false; } return true; @@ -60,8 +61,8 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic public bool ChangeStatus(OrderBindingModel model, OrderStatus status) { - CheckModel(model, false); - var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); if (element == null) { _logger.LogWarning("Read operation failed"); @@ -72,31 +73,32 @@ namespace AbstractLawFirmBusinessLogic.BusinessLogic _logger.LogWarning("Status change operation failed"); throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); } - if (status == OrderStatus.Готов) - { - var document = _documentStorage.GetElement(new DocumentSearchModel() { Id = model.DocumentId }); - if (document == null) - { - _logger.LogWarning("Status change operation failed. Car not found."); - return false; - } + if (status == OrderStatus.Готов) + { + var document = _documentStorage.GetElement(new DocumentSearchModel() { Id = model.DocumentId }); + if (document == null) + { + _logger.LogWarning("Status change operation failed. Car not found."); + return false; + } - if (!CheckThenSupplyMany(document, model.Count)) - { - _logger.LogWarning("Status change operation failed. Shop supply error."); - return false; - } - } - model.Status = status; + if (!CheckThenSupplyMany(document, model.Count)) + { + _logger.LogWarning("Status change operation failed. Shop supply error."); + return false; + } + } + + model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; _orderStorage.Update(model); - if (_orderStorage.Update(model) == null) - { - model.Status--; - _logger.LogWarning("Update operation failed"); - return false; - } - return true; + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; } public bool TakeOrderInWork(OrderBindingModel model) @@ -125,11 +127,11 @@ true) { return; } - if (model.DocumentId < 0) - { - throw new ArgumentNullException("Некорректный идентификатор документа", nameof(model.DocumentId)); - } - if (model.Sum <= 0) + if (model.DocumentId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор документа", nameof(model.DocumentId)); + } + if (model.Sum <= 0) { throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); } @@ -138,67 +140,68 @@ true) throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); } _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); - public bool CheckThenSupplyMany(IDocumentModel document, int count) - { - if (count <= 0) - { - _logger.LogWarning("Check then supply operation error. Car count < 0."); - return false; - } + } + public bool CheckThenSupplyMany(IDocumentModel document, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Car count < 0."); + return false; + } - int freeSpace = 0; - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace += shop.MaxCountDocuments; - foreach (var c in shop.ShopDocuments) - { - freeSpace -= c.Value.Item2; - } - } + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountDocuments; + foreach (var c in shop.ShopDocuments) + { + freeSpace -= c.Value.Item2; + } + } - if (freeSpace < count) - { - _logger.LogWarning("Check then supply operation error. There's no place for new cars in shops."); - return false; - } + if (freeSpace < count) + { + _logger.LogWarning("Check then supply operation error. There's no place for new cars in shops."); + return false; + } - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace = shop.MaxCountDocuments; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountDocuments; - foreach (var c in shop.ShopDocuments) - freeSpace -= c.Value.Item2; + foreach (var c in shop.ShopDocuments) + freeSpace -= c.Value.Item2; - if (freeSpace <= 0) - continue; + if (freeSpace <= 0) + continue; + + if (freeSpace >= count) + { + if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, count)) + count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace < count) + { + if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, freeSpace)) + count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } - if (freeSpace >= count) - { - if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, count)) - count = 0; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (freeSpace < count) - { - if (_shopLogic.SupplyDocuments(new ShopSearchModel() { Id = shop.Id }, document, freeSpace)) - count -= freeSpace; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - if (count <= 0) - { - return true; - } - } - return false; - } - } } -} +} \ No newline at end of file -- 2.25.1 From 57852c70118ccf02dfb9c88c5e7866da04516924 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Fri, 21 Jun 2024 19:29:54 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=A7=D1=82=D0=BE-=D1=82=D0=BE=20=D1=81?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 1 + .../Models/Shop.cs | 197 +++++++++--------- .../Implements/ShopStorage.cs | 6 +- .../Models/Shop.cs | 1 + LawFirm/LawFirmView/FormMain.cs | 11 +- LawFirm/LawFirmView/FormMain.designer.cs | 14 ++ .../LawFirmView/FormSellDocuments.Designer.cs | 120 +++++++++++ LawFirm/LawFirmView/FormSellDocuments.cs | 94 +++++++++ LawFirm/LawFirmView/FormSellDocuments.resx | 120 +++++++++++ LawFirm/LawFirmView/FormShop.Designer.cs | 37 +++- LawFirm/LawFirmView/FormShop.cs | 7 + LawFirm/LawFirmView/Program.cs | 1 + 12 files changed, 501 insertions(+), 108 deletions(-) create mode 100644 LawFirm/LawFirmView/FormSellDocuments.Designer.cs create mode 100644 LawFirm/LawFirmView/FormSellDocuments.cs create mode 100644 LawFirm/LawFirmView/FormSellDocuments.resx diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs index 1038019..bb6969c 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs @@ -3,6 +3,7 @@ using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.StoragesContracts; using AbstractLawFirmContracts.ViewModels; using AbstractLawFirmDataModels.Models; +using AbstractLawFirmFileImplement.Models; using AbstractLawFirmFileImpliment; using System; using System.Collections.Generic; diff --git a/LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs b/LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs index 8ffb924..6b2a370 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Models/Shop.cs @@ -11,105 +11,104 @@ using System.Xml.Linq; namespace AbstractLawFirmFileImplement.Models { - public class Shop : IShopModel - { - public int Id { get; private set; } - public string ShopName { get; private set; } = string.Empty; - public string Address { get; private set; } = string.Empty; - public int MaxCountDocuments { get; private set; } - public DateTime OpeningDate { get; private set; } - public Dictionary Documents { get; private set; } = new(); - private Dictionary? _shopDocuments = null; - public Dictionary ShopDocuments - { - get - { - if (_shopDocuments == null) - { - var source = DataFileSingleton.GetInstance(); - _shopDocuments = Documents.ToDictionary( - x => x.Key, - y => ((source.Documents.FirstOrDefault(z => z.Id == y.Key) as IDocumentModel)!, y.Value) - ); - } - return _shopDocuments; - } - } - public static Shop? Create(ShopBindingModel? model) - { - if (model == null) - { - return null; - } - return new Shop() - { - Id = model.Id, - ShopName = model.ShopName, - Address = model.Address, - MaxCountDocuments = model.MaxCountDocuments, - OpeningDate = model.OpeningDate, - Documents = model.ShopDocuments.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, - Address = element.Element("Address")!.Value, - MaxCountDocuments = Convert.ToInt32(element.Element("MaxCountDocuments")!.Value), - OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), - Documents = element.Element("ShopDocuments")!.Elements("ShopDocument").ToDictionary( - x => Convert.ToInt32(x.Element("Key")?.Value), - x => Convert.ToInt32(x.Element("Value")?.Value) - ) - }; - } + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + public int MaxCountDocuments { get; private set; } + public DateTime OpeningDate { get; private set; } + public Dictionary Documents { get; private set; } = new(); + private Dictionary? _shopDocuments = null; + public Dictionary ShopDocuments + { + get + { + if (_shopDocuments == null) + { + var source = DataFileSingleton.GetInstance(); + _shopDocuments = Documents.ToDictionary( + x => x.Key, + y => ((source.Documents.FirstOrDefault(z => z.Id == y.Key) as IDocumentModel)!, y.Value) + ); + } + return _shopDocuments; + } + } + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + MaxCountDocuments = model.MaxCountDocuments, + OpeningDate = model.OpeningDate, + Documents = model.ShopDocuments.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, + Address = element.Element("Address")!.Value, + MaxCountDocuments = Convert.ToInt32(element.Element("MaxCountDocuments")!.Value), + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + Documents = element.Element("ShopDocuments")!.Elements("ShopDocument").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; - Address = model.Address; - MaxCountDocuments = model.MaxCountDocuments; - OpeningDate = model.OpeningDate; - if (model.ShopDocuments.Count > 0) - { - Documents = model.ShopDocuments.ToDictionary(x => x.Key, x => x.Value.Item2); - _shopDocuments = null; - } - } - public ShopViewModel GetViewModel => new() - { - Id = Id, - ShopName = ShopName, - Address = Address, - MaxCountDocuments = MaxCountDocuments, - OpeningDate = OpeningDate, - ShopDocuments = ShopDocuments, - }; + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + MaxCountDocuments = model.MaxCountDocuments; + OpeningDate = model.OpeningDate; + if (model.ShopDocuments.Count > 0) + { + Documents = model.ShopDocuments.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopDocuments = null; + } + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + MaxCountDocuments = MaxCountDocuments, + OpeningDate = OpeningDate, + ShopDocuments = ShopDocuments, + }; - public XElement GetXElement => new( - "Shop", - new XAttribute("Id", Id), - new XElement("ShopName", ShopName), - new XElement("Address", Address), - new XElement("MaxCountDocuments", MaxCountDocuments), - new XElement("OpeningDate", OpeningDate.ToString()), - new XElement("ShopDocuments", Documents.Select(x => - new XElement("ShopDocument", - new XElement("Key", x.Key), - new XElement("Value", x.Value))) - .ToArray())); - } -} -} + public XElement GetXElement => new( + "Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("MaxCountDocuments", MaxCountDocuments), + new XElement("OpeningDate", OpeningDate.ToString()), + new XElement("ShopDocuments", Documents.Select(x => + new XElement("ShopDocument", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} \ No newline at end of file diff --git a/LawFirm/AbstractLawFirmListImplement/Implements/ShopStorage.cs b/LawFirm/AbstractLawFirmListImplement/Implements/ShopStorage.cs index aa88ff1..8a94b2d 100644 --- a/LawFirm/AbstractLawFirmListImplement/Implements/ShopStorage.cs +++ b/LawFirm/AbstractLawFirmListImplement/Implements/ShopStorage.cs @@ -3,6 +3,7 @@ using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.StoragesContracts; using AbstractLawFirmContracts.ViewModels; using AbstractLawFirmListImplement.Models; +using AbstractLawFirmDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -19,7 +20,10 @@ namespace AbstractLawFirmListImplement.Implements { _source = DataListSingleton.GetInstance(); } - + public bool SellDocument(IDocumentModel document, int count) + { + throw new NotImplementedException(); + } public ShopViewModel? GetElement(ShopSearchModel model) { if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) diff --git a/LawFirm/AbstractLawFirmListImplement/Models/Shop.cs b/LawFirm/AbstractLawFirmListImplement/Models/Shop.cs index 144fea1..92430c3 100644 --- a/LawFirm/AbstractLawFirmListImplement/Models/Shop.cs +++ b/LawFirm/AbstractLawFirmListImplement/Models/Shop.cs @@ -14,6 +14,7 @@ namespace AbstractLawFirmListImplement.Models public int Id { get; private set; } public string ShopName { get; private set; } = string.Empty; public string Address { get; private set; } = string.Empty; + public int MaxCountDocuments { get; private set; } public DateTime OpeningDate { get; private set; } public Dictionary ShopDocuments { diff --git a/LawFirm/LawFirmView/FormMain.cs b/LawFirm/LawFirmView/FormMain.cs index 0391af9..49a0a6d 100644 --- a/LawFirm/LawFirmView/FormMain.cs +++ b/LawFirm/LawFirmView/FormMain.cs @@ -126,7 +126,7 @@ namespace LawFirmView catch (Exception ex) { _logger.LogError(ex, "Ошибка отметки о готовности заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -193,5 +193,14 @@ namespace LawFirmView form.ShowDialog(); } } + + private void buttonSellDocs_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellDocuments)); + if (service is FormSellDocuments form) + { + form.ShowDialog(); + } + } } } diff --git a/LawFirm/LawFirmView/FormMain.designer.cs b/LawFirm/LawFirmView/FormMain.designer.cs index b8e47a7..eb318fb 100644 --- a/LawFirm/LawFirmView/FormMain.designer.cs +++ b/LawFirm/LawFirmView/FormMain.designer.cs @@ -40,6 +40,7 @@ buttonIssuedOrder = new Button(); buttonRef = new Button(); buttonSupplyShop = new Button(); + buttonSellDocs = new Button(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -160,11 +161,23 @@ buttonSupplyShop.UseVisualStyleBackColor = true; buttonSupplyShop.Click += buttonSupplyShop_Click; // + // buttonSellDocs + // + buttonSellDocs.Location = new Point(850, 289); + buttonSellDocs.Margin = new Padding(3, 4, 3, 4); + buttonSellDocs.Name = "buttonSellDocs"; + buttonSellDocs.Size = new Size(178, 31); + buttonSellDocs.TabIndex = 8; + buttonSellDocs.Text = "Продать документы"; + buttonSellDocs.UseVisualStyleBackColor = true; + buttonSellDocs.Click += buttonSellDocs_Click; + // // FormMain // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1040, 636); + Controls.Add(buttonSellDocs); Controls.Add(buttonSupplyShop); Controls.Add(buttonRef); Controls.Add(buttonIssuedOrder); @@ -199,5 +212,6 @@ private Button buttonRef; private ToolStripMenuItem магазиныToolStripMenuItem; private Button buttonSupplyShop; + private Button buttonSellDocs; } } \ No newline at end of file diff --git a/LawFirm/LawFirmView/FormSellDocuments.Designer.cs b/LawFirm/LawFirmView/FormSellDocuments.Designer.cs new file mode 100644 index 0000000..07cbb98 --- /dev/null +++ b/LawFirm/LawFirmView/FormSellDocuments.Designer.cs @@ -0,0 +1,120 @@ +namespace LawFirmView +{ + partial class FormSellDocuments + { + /// + /// 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.comboBoxDoc = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // comboBoxDoc + // + this.comboBoxDoc.FormattingEnabled = true; + this.comboBoxDoc.Location = new System.Drawing.Point(149, 12); + this.comboBoxDoc.Name = "comboBoxDoc"; + this.comboBoxDoc.Size = new System.Drawing.Size(218, 23); + this.comboBoxDoc.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(24, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(110, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Пакет документов:"; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(149, 41); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(218, 23); + this.textBoxCount.TabIndex = 2; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(59, 49); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(75, 15); + this.label2.TabIndex = 4; + this.label2.Text = "Количество:"; + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(149, 92); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(75, 23); + this.buttonSell.TabIndex = 5; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.buttonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(259, 92); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(75, 23); + this.buttonCancel.TabIndex = 6; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormSellDocuments + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(394, 137); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.label2); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.label1); + this.Controls.Add(this.comboBoxDoc); + this.Name = "FormSellDocuments"; + this.Text = "FormSellDocuments"; + this.Load += new System.EventHandler(this.FormSellDocuments_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxDoc; + private Label label1; + private TextBox textBoxCount; + private Label label2; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmView/FormSellDocuments.cs b/LawFirm/LawFirmView/FormSellDocuments.cs new file mode 100644 index 0000000..8ca3cae --- /dev/null +++ b/LawFirm/LawFirmView/FormSellDocuments.cs @@ -0,0 +1,94 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.BusinessLogicsContracts; +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 LawFirmView +{ + public partial class FormSellDocuments : Form + { + private readonly ILogger _logger; + private readonly IDocumentLogic _logicDocument; + private readonly IShopLogic _logicShop; + public FormSellDocuments(ILogger logger, IDocumentLogic logicDocument, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicDocument = logicDocument; + _logicShop = logicShop; + } + + private void FormSellDocuments_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка документов для продажи"); + try + { + var list = _logicDocument.ReadList(null); + if (list != null) + { + comboBoxDoc.DisplayMember = "DocumentName"; + comboBoxDoc.ValueMember = "Id"; + comboBoxDoc.DataSource = list; + comboBoxDoc.SelectedItem = null; + } + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка документов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxDoc.SelectedValue == null) + { + MessageBox.Show("Выберите пакет документов", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание продажи"); + try + { + var operationResult = _logicShop.SellDocument( + new DocumentBindingModel + { + Id = Convert.ToInt32(comboBoxDoc.SelectedValue) + }, + Convert.ToInt32(textBoxCount.Text) + ); + if (!operationResult) + { + throw new Exception("Ошибка при создании продажи. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания продажи"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmView/FormSellDocuments.resx b/LawFirm/LawFirmView/FormSellDocuments.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/LawFirm/LawFirmView/FormSellDocuments.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/LawFirm/LawFirmView/FormShop.Designer.cs b/LawFirm/LawFirmView/FormShop.Designer.cs index ac8233e..60240b8 100644 --- a/LawFirm/LawFirmView/FormShop.Designer.cs +++ b/LawFirm/LawFirmView/FormShop.Designer.cs @@ -40,12 +40,14 @@ Column2 = new DataGridViewTextBoxColumn(); Column1 = new DataGridViewTextBoxColumn(); dataGridView = new DataGridView(); + label4 = new Label(); + textBoxMaxCountDoc = new TextBox(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // textBoxName // - textBoxName.Location = new Point(149, 20); + textBoxName.Location = new Point(204, 6); textBoxName.Margin = new Padding(3, 4, 3, 4); textBoxName.Name = "textBoxName"; textBoxName.Size = new Size(228, 27); @@ -53,7 +55,7 @@ // // textBoxAddress // - textBoxAddress.Location = new Point(149, 56); + textBoxAddress.Location = new Point(204, 41); textBoxAddress.Margin = new Padding(3, 4, 3, 4); textBoxAddress.Name = "textBoxAddress"; textBoxAddress.Size = new Size(228, 27); @@ -61,7 +63,7 @@ // // dateTimePicker // - dateTimePicker.Location = new Point(149, 94); + dateTimePicker.Location = new Point(204, 76); dateTimePicker.Margin = new Padding(3, 4, 3, 4); dateTimePicker.Name = "dateTimePicker"; dateTimePicker.Size = new Size(228, 27); @@ -92,7 +94,7 @@ // label1 // label1.AutoSize = true; - label1.Location = new Point(48, 20); + label1.Location = new Point(30, 13); label1.Name = "label1"; label1.Size = new Size(80, 20); label1.TabIndex = 6; @@ -101,7 +103,7 @@ // label2 // label2.AutoSize = true; - label2.Location = new Point(59, 59); + label2.Location = new Point(30, 48); label2.Name = "label2"; label2.Size = new Size(54, 20); label2.TabIndex = 7; @@ -110,7 +112,7 @@ // label3 // label3.AutoSize = true; - label3.Location = new Point(30, 101); + label3.Location = new Point(30, 79); label3.Name = "label3"; label3.Size = new Size(113, 20); label3.TabIndex = 8; @@ -140,7 +142,7 @@ dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.Columns.AddRange(new DataGridViewColumn[] { Column1, Column2, Column3 }); - dataGridView.Location = new Point(14, 132); + dataGridView.Location = new Point(12, 145); dataGridView.Margin = new Padding(3, 4, 3, 4); dataGridView.Name = "dataGridView"; dataGridView.RowHeadersWidth = 51; @@ -148,11 +150,30 @@ dataGridView.Size = new Size(613, 327); dataGridView.TabIndex = 3; // + // label4 + // + label4.AutoSize = true; + label4.Location = new Point(30, 108); + label4.Name = "label4"; + label4.Size = new Size(168, 20); + label4.TabIndex = 9; + label4.Text = "Максимальное кол-во:"; + // + // textBoxMaxCountDoc + // + textBoxMaxCountDoc.Location = new Point(204, 101); + textBoxMaxCountDoc.Margin = new Padding(3, 4, 3, 4); + textBoxMaxCountDoc.Name = "textBoxMaxCountDoc"; + textBoxMaxCountDoc.Size = new Size(228, 27); + textBoxMaxCountDoc.TabIndex = 10; + // // FormShop // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(640, 557); + Controls.Add(textBoxMaxCountDoc); + Controls.Add(label4); Controls.Add(label3); Controls.Add(label2); Controls.Add(label1); @@ -185,5 +206,7 @@ private DataGridViewTextBoxColumn Column2; private DataGridViewTextBoxColumn Column1; private DataGridView dataGridView; + private Label label4; + private TextBox textBoxMaxCountDoc; } } \ No newline at end of file diff --git a/LawFirm/LawFirmView/FormShop.cs b/LawFirm/LawFirmView/FormShop.cs index cf7d42b..ae11116 100644 --- a/LawFirm/LawFirmView/FormShop.cs +++ b/LawFirm/LawFirmView/FormShop.cs @@ -45,6 +45,7 @@ namespace LawFirmView { textBoxName.Text = view.ShopName; textBoxAddress.Text = view.Address; + textBoxMaxCountDoc.Text = view.MaxCountDocuments.ToString(); dateTimePicker.Value = view.OpeningDate; _shopDocuments = view.ShopDocuments ?? new Dictionary(); LoadData(); @@ -98,6 +99,11 @@ namespace LawFirmView MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (string.IsNullOrEmpty(textBoxMaxCountDoc.Text)) + { + MessageBox.Show("Заполните макс. количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Сохранение магазина"); try { @@ -106,6 +112,7 @@ namespace LawFirmView Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, + MaxCountDocuments = Convert.ToInt32(textBoxMaxCountDoc.Text), OpeningDate = dateTimePicker.Value.Date, ShopDocuments = _shopDocuments }; diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index 603ee59..05c61ea 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -53,6 +53,7 @@ namespace LawFirmView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } -- 2.25.1 From a956ac08f59499214f1039589838c6ae9c2fad65 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sat, 22 Jun 2024 10:50:29 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D0=A4=D0=B8=D0=BD=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractLawFirmFileImpliment/Implements/ShopStorage.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs b/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs index bb6969c..914ceb8 100644 --- a/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs +++ b/LawFirm/AbstractLawFirmFileImpliment/Implements/ShopStorage.cs @@ -94,12 +94,9 @@ namespace AbstractLawFirmFileImplement.Implements var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(c => c.Value.Item1.Id == document.Id)); - int countStore = 0; + int countStore = shopDocuments.Sum(it => it.Value.Item2); - foreach (var it in shopDocuments) - countStore += it.Value.Item2; - - if (count > countStore) + if (count > countStore) return false; foreach (var shop in source.Shops) -- 2.25.1