From 1779a803a989ad9e58e1a47874aceebedd113cb3 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:44:19 +0400 Subject: [PATCH 01/10] fix --- .../LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs | 2 +- LawFirm/LawFirmFileImplement/DataFileSingleton.cs | 9 ++++++--- .../LawFirmFileImplement/Implements/DocumentStorage.cs | 8 ++++---- LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs | 6 +++--- LawFirm/LawFirmFileImplement/Models/Document.cs | 3 +-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index a87b215..07d30c1 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -52,7 +52,7 @@ namespace LawFirmBusinessLogic.BusinessLogics { model.DateImplement = viewModel.DateImplement; } - CheckModel(model); + CheckModel(model, false); if (_orderStorage.Update(model) == null) { model.Status--; diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index dba4b10..a4880e5 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -27,15 +27,18 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = new List(); + Orders = LoadData(DocumentFileName, "Order", x => Document.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 XDocument.Load(filename)?. + Root?. + Elements(xmlNodeName)?. + Select(selectFunction)?. + ToList(); } return new List(); } diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 559f389..7a0d8da 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -50,14 +50,14 @@ namespace LawFirmFileImplement.Implements } public DocumentViewModel? Update(DocumentBindingModel model) { - var ship = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (ship == null) + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) { return null; } - ship.Update(model); + document.Update(model); source.SaveDocuments(); - return ship.GetViewModel; + return document.GetViewModel; } public DocumentViewModel? Delete(DocumentBindingModel model) { diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 783ef1a..0a958dc 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -38,10 +38,10 @@ namespace LawFirmFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var ship = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (ship != null) + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) { - viewModel.DocumentName = ship.DocumentName; + viewModel.DocumentName = document.DocumentName; } return viewModel; } diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 9c2eca2..071a25d 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,8 +11,7 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = - null; + private Dictionary? _productBlanks = null; public Dictionary DocumentBlanks { get From 2419d6a9f764f25c461282663b5f6abe8e2a4085 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:51:31 +0400 Subject: [PATCH 02/10] fix --- .../BusinessLogics/OrderLogic.cs | 2 +- .../Models/Document.cs | 22 +++++++++---------- .../Implements/DocumentStorage.cs | 8 +++---- .../Implements/OrderStorage.cs | 6 ++--- .../LawFirmFileImplement/Models/Document.cs | 10 ++++----- LawFirm/LawFirmView/FormCreateOrder.cs | 4 ++-- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index 6a9989b..60efcf0 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -52,7 +52,7 @@ namespace LawFirmBusinessLogic.BusinessLogics { model.DateImplement = viewModel.DateImplement; } - CheckModel(model); + CheckModel(model, false); if (_orderStorage.Update(model) == null) { model.Status--; diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs index 5e3db24..166110d 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -13,19 +13,19 @@ namespace LawFirmDatabaseImplement.Models public string DocumentName { get; set; } = string.Empty; [Required] public double Price { get; set; } - private Dictionary? _productBlanks = null; + private Dictionary? _documentBlanks = null; [NotMapped] public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { - _productBlanks = Blanks + _documentBlanks = Blanks .ToDictionary(recPC => recPC.BlankId, recPC => (recPC.Blank as IBlankModel, recPC.Count)); } - return _productBlanks; + return _documentBlanks; } } [ForeignKey("DocumentId")] @@ -61,15 +61,15 @@ namespace LawFirmDatabaseImplement.Models public void UpdateBlanks(LawFirmDatabase context, DocumentBindingModel model) { - var productBlanks = context.DocumentBlanks.Where(rec => + var documentBlanks = context.DocumentBlanks.Where(rec => rec.DocumentId == model.Id).ToList(); - if (productBlanks != null && productBlanks.Count > 0) + if (documentBlanks != null && documentBlanks.Count > 0) { // удалили те, которых нет в модели - context.DocumentBlanks.RemoveRange(productBlanks.Where(rec + context.DocumentBlanks.RemoveRange(documentBlanks.Where(rec => !model.DocumentBlanks.ContainsKey(rec.BlankId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateBlank in productBlanks) + foreach (var updateBlank in documentBlanks) { updateBlank.Count = model.DocumentBlanks[updateBlank.BlankId].Item2; @@ -77,18 +77,18 @@ namespace LawFirmDatabaseImplement.Models } context.SaveChanges(); } - var product = context.Documents.First(x => x.Id == Id); + var document = context.Documents.First(x => x.Id == Id); foreach (var pc in model.DocumentBlanks) { context.DocumentBlanks.Add(new DocumentBlank { - Document = product, + Document = document, Blank = context.Blanks.First(x => x.Id == pc.Key), Count = pc.Value.Item2 }); context.SaveChanges(); } - _productBlanks = null; + _documentBlanks = null; } } diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 559f389..7a0d8da 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -50,14 +50,14 @@ namespace LawFirmFileImplement.Implements } public DocumentViewModel? Update(DocumentBindingModel model) { - var ship = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (ship == null) + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) { return null; } - ship.Update(model); + document.Update(model); source.SaveDocuments(); - return ship.GetViewModel; + return document.GetViewModel; } public DocumentViewModel? Delete(DocumentBindingModel model) { diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 783ef1a..0a958dc 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -38,10 +38,10 @@ namespace LawFirmFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var ship = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (ship != null) + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) { - viewModel.DocumentName = ship.DocumentName; + viewModel.DocumentName = document.DocumentName; } return viewModel; } diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 9c2eca2..83afe20 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,20 +11,20 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = + private Dictionary? _documentBlanks = null; public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { var source = DataFileSingleton.GetInstance(); - _productBlanks = Blanks.ToDictionary(x => x.Key, y => + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, y.Value)); } - return _productBlanks; + return _documentBlanks; } } public static Document? Create(DocumentBindingModel model) @@ -70,7 +70,7 @@ namespace LawFirmFileImplement.Models Price = model.Price; Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => x.Value.Item2); - _productBlanks = null; + _documentBlanks = null; } public DocumentViewModel GetViewModel => new() { diff --git a/LawFirm/LawFirmView/FormCreateOrder.cs b/LawFirm/LawFirmView/FormCreateOrder.cs index e971622..f6097f4 100644 --- a/LawFirm/LawFirmView/FormCreateOrder.cs +++ b/LawFirm/LawFirmView/FormCreateOrder.cs @@ -45,12 +45,12 @@ namespace LawFirmView try { int id = Convert.ToInt32(comboBoxDocument.SelectedValue); - var product = _logicD.ReadElement(new DocumentSearchModel + var document = _logicD.ReadElement(new DocumentSearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); + textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } catch (Exception ex) From bca8b0373a9c933238c5ac63552b7e9476229b3d Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:55:02 +0400 Subject: [PATCH 03/10] fix --- LawFirm/LawFirmFileImplement/Models/Document.cs | 10 +++++----- LawFirm/LawFirmView/FormCreateOrder.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 071a25d..117f779 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,19 +11,19 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = null; + private Dictionary? _documentBlanks = null; public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { var source = DataFileSingleton.GetInstance(); - _productBlanks = Blanks.ToDictionary(x => x.Key, y => + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, y.Value)); } - return _productBlanks; + return _documentBlanks; } } public static Document? Create(DocumentBindingModel model) @@ -69,7 +69,7 @@ namespace LawFirmFileImplement.Models Price = model.Price; Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => x.Value.Item2); - _productBlanks = null; + _documentBlanks = null; } public DocumentViewModel GetViewModel => new() { diff --git a/LawFirm/LawFirmView/FormCreateOrder.cs b/LawFirm/LawFirmView/FormCreateOrder.cs index e971622..f6097f4 100644 --- a/LawFirm/LawFirmView/FormCreateOrder.cs +++ b/LawFirm/LawFirmView/FormCreateOrder.cs @@ -45,12 +45,12 @@ namespace LawFirmView try { int id = Convert.ToInt32(comboBoxDocument.SelectedValue); - var product = _logicD.ReadElement(new DocumentSearchModel + var document = _logicD.ReadElement(new DocumentSearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); + textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } catch (Exception ex) From cb8fb410b36454c46d60ae34d8ec842b3ace2ac6 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:56:13 +0400 Subject: [PATCH 04/10] fix --- LawFirm/LawFirmFileImplement/DataFileSingleton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index dba4b10..6c0c8fd 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -27,7 +27,7 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = new List(); + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) From e7a14dbda638a21d2e4b33c9f4fbab5a3bc922f5 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:56:36 +0400 Subject: [PATCH 05/10] fix --- LawFirm/LawFirmFileImplement/DataFileSingleton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index a4880e5..b4acfca 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -27,7 +27,7 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = LoadData(DocumentFileName, "Order", x => Document.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) From 2e62277509a74b2e90fbf717a6ae07f7c968bee9 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 14:19:31 +0400 Subject: [PATCH 06/10] fix --- .../BusinessLogics/OrderLogic.cs | 2 +- .../BusinessLogics/ReportLogic.cs | 3 +-- .../OfficePackage/AbstractSaveToPdf.cs | 6 ++--- .../Implements/DocumentStorage.cs | 10 ++++----- .../Models/Document.cs | 22 +++++++++---------- .../LawFirmFileImplement/DataFileSingleton.cs | 4 ++-- .../Implements/DocumentStorage.cs | 8 +++---- .../Implements/OrderStorage.cs | 6 ++--- .../LawFirmFileImplement/Models/Document.cs | 10 ++++----- LawFirm/LawFirmView/FormCreateOrder.cs | 4 ++-- LawFirm/LawFirmView/FormMain.Designer.cs | 2 +- LawFirm/LawFirmView/FormMain.cs | 2 +- .../LawFirmView/FormReportDocumentBlanks.cs | 6 ++--- LawFirm/LawFirmView/LawFirmView.csproj | 6 +++++ 14 files changed, 47 insertions(+), 44 deletions(-) diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index 59e1caa..94bb693 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -52,7 +52,7 @@ namespace LawFirmBusinessLogic.BusinessLogics { model.DateImplement = viewModel.DateImplement; } - CheckModel(model); + CheckModel(model, false); if (_orderStorage.Update(model) == null) { model.Status--; diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ReportLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ReportLogic.cs index def32b9..772e9b1 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ReportLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ReportLogic.cs @@ -50,8 +50,7 @@ namespace LawFirmBusinessLogic.BusinessLogics if (document.DocumentBlanks.ContainsKey(blank.Id)) { record.Blanks.Add(new Tuple(blank.BlankName, document.DocumentBlanks[blank.Id].Item2)); - record.TotalCount += - document.DocumentBlanks[blank.Id].Item2; + record.TotalCount += document.DocumentBlanks[blank.Id].Item2; } } list.Add(record); diff --git a/LawFirm/LawFirmBusinessLogic/OfficePackage/AbstractSaveToPdf.cs b/LawFirm/LawFirmBusinessLogic/OfficePackage/AbstractSaveToPdf.cs index a315e88..fe9c684 100644 --- a/LawFirm/LawFirmBusinessLogic/OfficePackage/AbstractSaveToPdf.cs +++ b/LawFirm/LawFirmBusinessLogic/OfficePackage/AbstractSaveToPdf.cs @@ -23,10 +23,10 @@ namespace LawFirmBusinessLogic.OfficePackage = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Center }); - CreateTable(new List { "2cm", "3cm", "6cm", "3cm" }); + CreateTable(new List { "2cm", "3cm", "6cm", "3cm", "3cm" }); CreateRow(new PdfRowParameters { - Texts = new List { "Номер", "Дата заказа", "Изделие", "Сумма" }, + Texts = new List { "Номер", "Дата заказа", "Изделие", "Статус", "Сумма" }, Style = "NormalTitle", ParagraphAlignment = PdfParagraphAlignmentType.Center }); @@ -34,7 +34,7 @@ namespace LawFirmBusinessLogic.OfficePackage { CreateRow(new PdfRowParameters { - Texts = new List { order.Id.ToString(), order.DateCreate.ToShortDateString(), order.DocumentName, order.Sum.ToString() }, + Texts = new List { order.Id.ToString(), order.DateCreate.ToShortDateString(), order.DocumentName, order.OrderStatus, order.Sum.ToString() }, Style = "Normal", ParagraphAlignment = PdfParagraphAlignmentType.Left }); diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs index 9b1ad9a..b4f94ff 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -70,17 +70,17 @@ namespace LawFirmDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var product = context.Documents.FirstOrDefault(rec => + var document = context.Documents.FirstOrDefault(rec => rec.Id == model.Id); - if (product == null) + if (document == null) { return null; } - product.Update(model); + document.Update(model); context.SaveChanges(); - product.UpdateBlanks(context, model); + document.UpdateBlanks(context, model); transaction.Commit(); - return product.GetViewModel; + return document.GetViewModel; } catch { diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs index 5e3db24..166110d 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -13,19 +13,19 @@ namespace LawFirmDatabaseImplement.Models public string DocumentName { get; set; } = string.Empty; [Required] public double Price { get; set; } - private Dictionary? _productBlanks = null; + private Dictionary? _documentBlanks = null; [NotMapped] public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { - _productBlanks = Blanks + _documentBlanks = Blanks .ToDictionary(recPC => recPC.BlankId, recPC => (recPC.Blank as IBlankModel, recPC.Count)); } - return _productBlanks; + return _documentBlanks; } } [ForeignKey("DocumentId")] @@ -61,15 +61,15 @@ namespace LawFirmDatabaseImplement.Models public void UpdateBlanks(LawFirmDatabase context, DocumentBindingModel model) { - var productBlanks = context.DocumentBlanks.Where(rec => + var documentBlanks = context.DocumentBlanks.Where(rec => rec.DocumentId == model.Id).ToList(); - if (productBlanks != null && productBlanks.Count > 0) + if (documentBlanks != null && documentBlanks.Count > 0) { // удалили те, которых нет в модели - context.DocumentBlanks.RemoveRange(productBlanks.Where(rec + context.DocumentBlanks.RemoveRange(documentBlanks.Where(rec => !model.DocumentBlanks.ContainsKey(rec.BlankId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateBlank in productBlanks) + foreach (var updateBlank in documentBlanks) { updateBlank.Count = model.DocumentBlanks[updateBlank.BlankId].Item2; @@ -77,18 +77,18 @@ namespace LawFirmDatabaseImplement.Models } context.SaveChanges(); } - var product = context.Documents.First(x => x.Id == Id); + var document = context.Documents.First(x => x.Id == Id); foreach (var pc in model.DocumentBlanks) { context.DocumentBlanks.Add(new DocumentBlank { - Document = product, + Document = document, Blank = context.Blanks.First(x => x.Id == pc.Key), Count = pc.Value.Item2 }); context.SaveChanges(); } - _productBlanks = null; + _documentBlanks = null; } } diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index dba4b10..1e6cf08 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -27,8 +27,8 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = new List(); - } + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 559f389..7a0d8da 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -50,14 +50,14 @@ namespace LawFirmFileImplement.Implements } public DocumentViewModel? Update(DocumentBindingModel model) { - var ship = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (ship == null) + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) { return null; } - ship.Update(model); + document.Update(model); source.SaveDocuments(); - return ship.GetViewModel; + return document.GetViewModel; } public DocumentViewModel? Delete(DocumentBindingModel model) { diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index aa6c9dd..5b5049f 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -38,10 +38,10 @@ namespace LawFirmFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var ship = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (ship != null) + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) { - viewModel.DocumentName = ship.DocumentName; + viewModel.DocumentName = document.DocumentName; } return viewModel; } diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 9c2eca2..83afe20 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,20 +11,20 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = + private Dictionary? _documentBlanks = null; public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { var source = DataFileSingleton.GetInstance(); - _productBlanks = Blanks.ToDictionary(x => x.Key, y => + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, y.Value)); } - return _productBlanks; + return _documentBlanks; } } public static Document? Create(DocumentBindingModel model) @@ -70,7 +70,7 @@ namespace LawFirmFileImplement.Models Price = model.Price; Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => x.Value.Item2); - _productBlanks = null; + _documentBlanks = null; } public DocumentViewModel GetViewModel => new() { diff --git a/LawFirm/LawFirmView/FormCreateOrder.cs b/LawFirm/LawFirmView/FormCreateOrder.cs index e971622..f6097f4 100644 --- a/LawFirm/LawFirmView/FormCreateOrder.cs +++ b/LawFirm/LawFirmView/FormCreateOrder.cs @@ -45,12 +45,12 @@ namespace LawFirmView try { int id = Convert.ToInt32(comboBoxDocument.SelectedValue); - var product = _logicD.ReadElement(new DocumentSearchModel + var document = _logicD.ReadElement(new DocumentSearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); + textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } catch (Exception ex) diff --git a/LawFirm/LawFirmView/FormMain.Designer.cs b/LawFirm/LawFirmView/FormMain.Designer.cs index 3ea6402..30ce520 100644 --- a/LawFirm/LawFirmView/FormMain.Designer.cs +++ b/LawFirm/LawFirmView/FormMain.Designer.cs @@ -95,7 +95,7 @@ this.списокКомпонентовToolStripMenuItem.Name = "списокКомпонентовToolStripMenuItem"; this.списокКомпонентовToolStripMenuItem.Size = new System.Drawing.Size(218, 22); this.списокКомпонентовToolStripMenuItem.Text = "Список компонентов"; - this.списокКомпонентовToolStripMenuItem.Click += new System.EventHandler(this.BlanksReportToolStripMenuItem_Click); + this.списокКомпонентовToolStripMenuItem.Click += new System.EventHandler(this.DocumentsReportToolStripMenuItem_Click); // // компонентыПоИзделиямToolStripMenuItem // diff --git a/LawFirm/LawFirmView/FormMain.cs b/LawFirm/LawFirmView/FormMain.cs index 28d703b..ee56f66 100644 --- a/LawFirm/LawFirmView/FormMain.cs +++ b/LawFirm/LawFirmView/FormMain.cs @@ -167,7 +167,7 @@ namespace LawFirmView } } - private void BlanksReportToolStripMenuItem_Click(object sender, EventArgs + private void DocumentsReportToolStripMenuItem_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "docx|*.docx" }; diff --git a/LawFirm/LawFirmView/FormReportDocumentBlanks.cs b/LawFirm/LawFirmView/FormReportDocumentBlanks.cs index cf09d42..b40758c 100644 --- a/LawFirm/LawFirmView/FormReportDocumentBlanks.cs +++ b/LawFirm/LawFirmView/FormReportDocumentBlanks.cs @@ -31,11 +31,9 @@ namespace LawFirmView dataGridView.Rows.Add(new object[] { elem.DocumentName, "", "" }); foreach (var listElem in elem.Blanks) { - dataGridView.Rows.Add(new object[] { "", -listElem.Item1, listElem.Item2 }); + dataGridView.Rows.Add(new object[] { "", listElem.Item1, listElem.Item2 }); } - dataGridView.Rows.Add(new object[] { "Итого", "", -elem.TotalCount }); + dataGridView.Rows.Add(new object[] { "Итого", "", elem.TotalCount }); dataGridView.Rows.Add(Array.Empty()); } } diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index 725b3ef..5904c43 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -27,4 +27,10 @@ + + + Always + + + \ No newline at end of file From 8d0be1216cb7423dead2d6966b91714fd7f79e6a Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 14:32:11 +0400 Subject: [PATCH 07/10] fix --- .../LawFirmFileImplement/DataFileSingleton.cs | 97 ++++---- .../Implements/DocumentStorage.cs | 132 +++++------ .../Implements/OrderStorage.cs | 148 ++++++------ .../LawFirmFileImplement/Models/Document.cs | 167 +++++++------ LawFirm/LawFirmView/FormCreateOrder.cs | 220 +++++++++--------- 5 files changed, 383 insertions(+), 381 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index 6c0c8fd..8ed511f 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -3,50 +3,53 @@ using System.Xml.Linq; namespace LawFirmFileImplement { - internal class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string BlankFileName = "Blank.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string DocumentFileName = "Document.xml"; - public List Blanks { 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 SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement); - public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - private DataFileSingleton() - { - Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; - Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - } - private static List? LoadData(string filename, string xmlNodeName, - Func selectFunction) - { - if (File.Exists(filename)) - { - return - XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); - } - return new List(); - } - private static void SaveData(List data, string filename, - string xmlNodeName, Func selectFunction) - { - if (data != null) - { - new XDocument(new XElement(xmlNodeName, - data.Select(selectFunction).ToArray())).Save(filename); - } - } - } -} + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string BlankFileName = "Blank.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string DocumentFileName = "Document.xml"; + public List Blanks { 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 SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement); + public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; + Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, + Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?. + Root?. + Elements(xmlNodeName)?. + Select(selectFunction)?. + ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, + string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, + data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 7a0d8da..9b8fc32 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -6,69 +6,69 @@ using LawFirmFileImplement.Models; namespace LawFirmFileImplement.Implements { - public class DocumentStorage : IDocumentStorage - { - private readonly DataFileSingleton source; - public DocumentStorage() - { - source = DataFileSingleton.GetInstance(); - } - public List GetFullList() - { - return source.Documents.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 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? Insert(DocumentBindingModel model) - { - model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; - var newDocument = Document.Create(model); - if (newDocument == null) - { - return null; - } - source.Documents.Add(newDocument); - source.SaveDocuments(); - return newDocument.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 element = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (element != null) - { - source.Documents.Remove(element); - source.SaveDocuments(); - return element.GetViewModel; - } - return null; - } - } -} + public class DocumentStorage : IDocumentStorage + { + private readonly DataFileSingleton source; + public DocumentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Documents.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 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? Insert(DocumentBindingModel model) + { + model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; + var newDocument = Document.Create(model); + if (newDocument == null) + { + return null; + } + source.Documents.Add(newDocument); + source.SaveDocuments(); + return newDocument.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 element = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Documents.Remove(element); + source.SaveDocuments(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 0a958dc..58e0231 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -6,78 +6,78 @@ using LawFirmFileImplement.Models; namespace LawFirmFileImplement.Implements { - public class OrderStorage : IOrderStorage - { - private readonly DataFileSingleton source; - public OrderStorage() - { - source = DataFileSingleton.GetInstance(); - } - public List GetFullList() - { - return source.Orders.Select(x => GetViewModel(x)).ToList(); - } - public List GetFilteredList(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList(); - } - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } - return source.Orders.FirstOrDefault(x => - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - 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; - } - 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 element = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (element != null) - { - source.Orders.Remove(element); - source.SaveOrders(); - return GetViewModel(element); - } - return null; - } - } -} + return source.Orders.FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + 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; + } + 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 element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return GetViewModel(element); + } + return null; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 83afe20..8672080 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -5,87 +5,86 @@ using System.Xml.Linq; namespace LawFirmFileImplement.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 Blanks { get; private set; } = new(); - private Dictionary? _documentBlanks = - null; - public Dictionary DocumentBlanks - { - get - { - if (_documentBlanks == null) - { - var source = DataFileSingleton.GetInstance(); - _documentBlanks = Blanks.ToDictionary(x => x.Key, y => - ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, - y.Value)); - } - return _documentBlanks; - } - } - public static Document? Create(DocumentBindingModel model) - { - if (model == null) - { - return null; - } - return new Document() - { - Id = model.Id, - DocumentName = model.DocumentName, - Price = model.Price, - Blanks = model.DocumentBlanks.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), - Blanks = - element.Element("DocumentBlanks")!.Elements("DocumentBlank") - .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; - Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => - x.Value.Item2); - _documentBlanks = null; - } - public DocumentViewModel GetViewModel => new() - { - Id = Id, - DocumentName = DocumentName, - Price = Price, - DocumentBlanks = DocumentBlanks - }; - public XElement GetXElement => new("Document", - new XAttribute("Id", Id), - new XElement("DocumentName", DocumentName), - new XElement("Price", Price.ToString()), - new XElement("DocumentBlanks", Blanks.Select(x => - new XElement("DocumentBlank", - 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 Blanks { get; private set; } = new(); + private Dictionary? _documentBlanks = null; + public Dictionary DocumentBlanks + { + get + { + if (_documentBlanks == null) + { + var source = DataFileSingleton.GetInstance(); + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => + ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, + y.Value)); + } + return _documentBlanks; + } + } + public static Document? Create(DocumentBindingModel model) + { + if (model == null) + { + return null; + } + return new Document() + { + Id = model.Id, + DocumentName = model.DocumentName, + Price = model.Price, + Blanks = model.DocumentBlanks.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), + Blanks = + element.Element("DocumentBlanks")!.Elements("DocumentBlank") + .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; + Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => + x.Value.Item2); + _documentBlanks = null; + } + public DocumentViewModel GetViewModel => new() + { + Id = Id, + DocumentName = DocumentName, + Price = Price, + DocumentBlanks = DocumentBlanks + }; + public XElement GetXElement => new("Document", + new XAttribute("Id", Id), + new XElement("DocumentName", DocumentName), + new XElement("Price", Price.ToString()), + new XElement("DocumentBlanks", Blanks.Select(x => + new XElement("DocumentBlank", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmView/FormCreateOrder.cs b/LawFirm/LawFirmView/FormCreateOrder.cs index f6097f4..d706fdb 100644 --- a/LawFirm/LawFirmView/FormCreateOrder.cs +++ b/LawFirm/LawFirmView/FormCreateOrder.cs @@ -5,113 +5,113 @@ using Microsoft.Extensions.Logging; namespace LawFirmView { - public partial class FormCreateOrder : Form - { - private readonly ILogger _logger; - private readonly IDocumentLogic _logicD; - private readonly IOrderLogic _logicO; - public FormCreateOrder(ILogger logger, IDocumentLogic logicD, IOrderLogic logicO) - { - InitializeComponent(); - _logger = logger; - _logicD = logicD; - _logicO = logicO; - } - private void FormCreateOrder_Load(object sender, EventArgs e) - { - _logger.LogInformation("Загрузка документов для заказа"); - try - { - var list = _logicD.ReadList(null); - if (list != null) - { - comboBoxDocument.DisplayMember = "DocumentName"; - comboBoxDocument.ValueMember = "Id"; - comboBoxDocument.DataSource = list; - comboBoxDocument.SelectedItem = null; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки документов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void CalcSum() - { - if (comboBoxDocument.SelectedValue != null && - !string.IsNullOrEmpty(textBoxCount.Text)) - { - try - { - int id = Convert.ToInt32(comboBoxDocument.SelectedValue); - var document = _logicD.ReadElement(new DocumentSearchModel - { - Id = id - }); - int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); - _logger.LogInformation("Расчет суммы заказа"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка расчета суммы заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } - } - private void TextBoxCount_TextChanged(object sender, EventArgs e) - { - CalcSum(); - } - private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e) - { - CalcSum(); - } - private void ButtonSave_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxCount.Text)) - { - MessageBox.Show("Заполните поле Количество", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (comboBoxDocument.SelectedValue == null) - { - MessageBox.Show("Выберите документ", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - _logger.LogInformation("Создание заказа"); - try - { - var operationResult = _logicO.CreateOrder(new OrderBindingModel - { - DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue), - Count = Convert.ToInt32(textBoxCount.Text), - Sum = Convert.ToDouble(textBoxSum.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(); - } - } -} + public partial class FormCreateOrder : Form + { + private readonly ILogger _logger; + private readonly IDocumentLogic _logicD; + private readonly IOrderLogic _logicO; + public FormCreateOrder(ILogger logger, IDocumentLogic logicD, IOrderLogic logicO) + { + InitializeComponent(); + _logger = logger; + _logicD = logicD; + _logicO = logicO; + } + private void FormCreateOrder_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка документов для заказа"); + try + { + var list = _logicD.ReadList(null); + if (list != null) + { + comboBoxDocument.DisplayMember = "DocumentName"; + comboBoxDocument.ValueMember = "Id"; + comboBoxDocument.DataSource = list; + comboBoxDocument.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки документов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void CalcSum() + { + if (comboBoxDocument.SelectedValue != null && + !string.IsNullOrEmpty(textBoxCount.Text)) + { + try + { + int id = Convert.ToInt32(comboBoxDocument.SelectedValue); + var document = _logicD.ReadElement(new DocumentSearchModel + { + Id = id + }); + int count = Convert.ToInt32(textBoxCount.Text); + textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); + _logger.LogInformation("Расчет суммы заказа"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка расчета суммы заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void TextBoxCount_TextChanged(object sender, EventArgs e) + { + CalcSum(); + } + private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e) + { + CalcSum(); + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxDocument.SelectedValue == null) + { + MessageBox.Show("Выберите документ", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text), + Sum = Convert.ToDouble(textBoxSum.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 From e1859dc0eb1f3fd41c6ee244d7fa8fdbbdf5c948 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 14:35:46 +0400 Subject: [PATCH 08/10] fix --- LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 58e0231..89e152f 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -80,4 +80,4 @@ namespace LawFirmFileImplement.Implements return null; } } -} \ No newline at end of file +} From 453ed01e98a253650d749a01c8e20b242f9ae4de Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Tue, 2 May 2023 14:11:28 +0400 Subject: [PATCH 09/10] fix --- .../LawFirmFileImplement/Implements/DocumentStorage.cs | 8 ++++---- LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 559f389..7a0d8da 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -50,14 +50,14 @@ namespace LawFirmFileImplement.Implements } public DocumentViewModel? Update(DocumentBindingModel model) { - var ship = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (ship == null) + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) { return null; } - ship.Update(model); + document.Update(model); source.SaveDocuments(); - return ship.GetViewModel; + return document.GetViewModel; } public DocumentViewModel? Delete(DocumentBindingModel model) { diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 8d8d0d1..35a6985 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -49,10 +49,10 @@ namespace LawFirmFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var ship = _source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (ship != null) + var document = _source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) { - viewModel.DocumentName = ship.DocumentName; + viewModel.DocumentName = document.DocumentName; } return viewModel; } From 6e453da61d04402e35d1ce27c808d9254a2b1bbd Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Tue, 2 May 2023 14:16:05 +0400 Subject: [PATCH 10/10] fix --- LawFirm/LawFirmFileImplement/DataFileSingleton.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index 538e0b6..82effda 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -33,8 +33,8 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = new List(); - Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction)