diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index 49deb79..08bae40 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -5,13 +5,15 @@ VisualStudioVersion = 17.7.34221.43 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmView", "LawFirm\LawFirmView.csproj", "{003F906D-2F68-43B6-9668-BF3CE918739B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmDataModels", "LawFirmDataModels\LawFirmDataModels.csproj", "{89AE92AD-3D6A-4C2F-AA8A-E36D895A55C8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmDataModels", "LawFirmDataModels\LawFirmDataModels.csproj", "{89AE92AD-3D6A-4C2F-AA8A-E36D895A55C8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmContracts", "LawFirmContracts\LawFirmContracts.csproj", "{6EB55DC4-8665-49CB-A0E6-DAF6E3CE58D3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmContracts", "LawFirmContracts\LawFirmContracts.csproj", "{6EB55DC4-8665-49CB-A0E6-DAF6E3CE58D3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmListImplement", "LawFirmListImplement\LawFirmListImplement.csproj", "{8B5C5B0D-7F02-4178-A047-13FE2D659A8A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmListImplement", "LawFirmListImplement\LawFirmListImplement.csproj", "{8B5C5B0D-7F02-4178-A047-13FE2D659A8A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmBusinessLogic", "LawFirmBusinessLogic\LawFirmBusinessLogic.csproj", "{0EB8CCEC-83C2-4425-A963-7979DB147D82}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmBusinessLogic", "LawFirmBusinessLogic\LawFirmBusinessLogic.csproj", "{0EB8CCEC-83C2-4425-A963-7979DB147D82}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmFileImplement", "LawFirmFileImplement\LawFirmFileImplement.csproj", "{5CAEEF2D-864E-4EA6-9051-AD57284623E1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {0EB8CCEC-83C2-4425-A963-7979DB147D82}.Debug|Any CPU.Build.0 = Debug|Any CPU {0EB8CCEC-83C2-4425-A963-7979DB147D82}.Release|Any CPU.ActiveCfg = Release|Any CPU {0EB8CCEC-83C2-4425-A963-7979DB147D82}.Release|Any CPU.Build.0 = Release|Any CPU + {5CAEEF2D-864E-4EA6-9051-AD57284623E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CAEEF2D-864E-4EA6-9051-AD57284623E1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CAEEF2D-864E-4EA6-9051-AD57284623E1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CAEEF2D-864E-4EA6-9051-AD57284623E1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirm/FormDocument.Designer.cs b/LawFirm/LawFirm/FormDocument.Designer.cs index e5eb270..85343f6 100644 --- a/LawFirm/LawFirm/FormDocument.Designer.cs +++ b/LawFirm/LawFirm/FormDocument.Designer.cs @@ -33,16 +33,16 @@ textBoxName = new TextBox(); textBoxPrice = new TextBox(); groupBox = new GroupBox(); - buttonSave = new Button(); - buttonCancel = new Button(); - buttonAdd = new Button(); - buttonChange = new Button(); - buttonDelete = new Button(); - buttonUpdate = new Button(); dataGridView = new DataGridView(); BlankId = new DataGridViewTextBoxColumn(); Blank = new DataGridViewTextBoxColumn(); Count = new DataGridViewTextBoxColumn(); + buttonUpdate = new Button(); + buttonDelete = new Button(); + buttonChange = new Button(); + buttonAdd = new Button(); + buttonSave = new Button(); + buttonCancel = new Button(); groupBox.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -76,6 +76,7 @@ // textBoxPrice.Location = new Point(124, 60); textBoxPrice.Name = "textBoxPrice"; + textBoxPrice.ReadOnly = true; textBoxPrice.Size = new Size(229, 27); textBoxPrice.TabIndex = 3; // @@ -93,66 +94,6 @@ groupBox.TabStop = false; groupBox.Text = "Бланки"; // - // buttonSave - // - buttonSave.Location = new Point(502, 592); - buttonSave.Name = "buttonSave"; - buttonSave.Size = new Size(94, 29); - buttonSave.TabIndex = 5; - buttonSave.Text = "Сохранить"; - buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; - // - // buttonCancel - // - buttonCancel.Location = new Point(625, 592); - buttonCancel.Name = "buttonCancel"; - buttonCancel.Size = new Size(94, 29); - buttonCancel.TabIndex = 6; - buttonCancel.Text = "Отмена"; - buttonCancel.UseVisualStyleBackColor = true; - buttonCancel.Click += buttonCancel_Click; - // - // buttonAdd - // - buttonAdd.Location = new Point(573, 41); - buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(94, 29); - buttonAdd.TabIndex = 7; - buttonAdd.Text = "Добавить"; - buttonAdd.UseVisualStyleBackColor = true; - buttonAdd.Click += buttonAdd_Click; - // - // buttonChange - // - buttonChange.Location = new Point(573, 85); - buttonChange.Name = "buttonChange"; - buttonChange.Size = new Size(94, 29); - buttonChange.TabIndex = 8; - buttonChange.Text = "Изменить"; - buttonChange.UseVisualStyleBackColor = true; - buttonChange.Click += buttonChange_Click; - // - // buttonDelete - // - buttonDelete.Location = new Point(573, 130); - buttonDelete.Name = "buttonDelete"; - buttonDelete.Size = new Size(94, 29); - buttonDelete.TabIndex = 9; - buttonDelete.Text = "Удалить"; - buttonDelete.UseVisualStyleBackColor = true; - buttonDelete.Click += buttonDelete_Click; - // - // buttonUpdate - // - buttonUpdate.Location = new Point(573, 176); - buttonUpdate.Name = "buttonUpdate"; - buttonUpdate.Size = new Size(94, 29); - buttonUpdate.TabIndex = 10; - buttonUpdate.Text = "Обновить"; - buttonUpdate.UseVisualStyleBackColor = true; - buttonUpdate.Click += buttonUpdate_Click; - // // dataGridView // dataGridView.AllowUserToAddRows = false; @@ -194,6 +135,66 @@ Count.Name = "Count"; Count.ReadOnly = true; // + // buttonUpdate + // + buttonUpdate.Location = new Point(573, 176); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(94, 29); + buttonUpdate.TabIndex = 10; + buttonUpdate.Text = "Обновить"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += buttonUpdate_Click; + // + // buttonDelete + // + buttonDelete.Location = new Point(573, 130); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(94, 29); + buttonDelete.TabIndex = 9; + buttonDelete.Text = "Удалить"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += buttonDelete_Click; + // + // buttonChange + // + buttonChange.Location = new Point(573, 85); + buttonChange.Name = "buttonChange"; + buttonChange.Size = new Size(94, 29); + buttonChange.TabIndex = 8; + buttonChange.Text = "Изменить"; + buttonChange.UseVisualStyleBackColor = true; + buttonChange.Click += buttonChange_Click; + // + // buttonAdd + // + buttonAdd.Location = new Point(573, 41); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(94, 29); + buttonAdd.TabIndex = 7; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // buttonSave + // + buttonSave.Location = new Point(502, 592); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(94, 29); + buttonSave.TabIndex = 5; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(625, 592); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 6; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // // FormDocument // AutoScaleDimensions = new SizeF(8F, 20F); diff --git a/LawFirm/LawFirm/FormDocument.resx b/LawFirm/LawFirm/FormDocument.resx index 8ea99a0..ddf6ea6 100644 --- a/LawFirm/LawFirm/FormDocument.resx +++ b/LawFirm/LawFirm/FormDocument.resx @@ -126,13 +126,4 @@ True - - True - - - True - - - True - \ No newline at end of file diff --git a/LawFirm/LawFirm/LawFirmView.csproj b/LawFirm/LawFirm/LawFirmView.csproj index 3c403c3..a266b32 100644 --- a/LawFirm/LawFirm/LawFirmView.csproj +++ b/LawFirm/LawFirm/LawFirmView.csproj @@ -20,6 +20,7 @@ + diff --git a/LawFirm/LawFirm/Program.cs b/LawFirm/LawFirm/Program.cs index 79a64e7..90de36e 100644 --- a/LawFirm/LawFirm/Program.cs +++ b/LawFirm/LawFirm/Program.cs @@ -1,7 +1,7 @@ using LawFirmBusinessLogic.BusinessLogics; using LawFirmContracts.BusinessLogicsContracts; using LawFirmContracts.StoragesContracts; -using LawFirmListImplement.Implements; +using LawFirmFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..8c60cc4 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -0,0 +1,55 @@ +using LawFirmFileImplement.Models; +using System.Xml.Linq; + +namespace LawFirmFileImplement +{ + public 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/BlankStorage.cs b/LawFirm/LawFirmFileImplement/Implements/BlankStorage.cs new file mode 100644 index 0000000..55e451a --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Implements/BlankStorage.cs @@ -0,0 +1,79 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmFileImplement.Models; + +namespace LawFirmFileImplement.Implements +{ + public class BlankStorage : IBlankStorage + { + private readonly DataFileSingleton source; + public BlankStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Blanks.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(BlankSearchModel model) + { + if (string.IsNullOrEmpty(model.BlankName)) + { + return new(); + } + return source.Blanks + .Where(x => x.BlankName.Contains(model.BlankName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public BlankViewModel? GetElement(BlankSearchModel model) + { + if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue) + { + return null; + } + return source.Blanks + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.BlankName) && + x.BlankName == model.BlankName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public BlankViewModel? Insert(BlankBindingModel model) + { + model.Id = source.Blanks.Count > 0 ? source.Blanks.Max(x => x.Id) + 1 : 1; + var newBlank = Blank.Create(model); + if (newBlank == null) + { + return null; + } + source.Blanks.Add(newBlank); + source.SaveBlanks(); + return newBlank.GetViewModel; + } + public BlankViewModel? Update(BlankBindingModel model) + { + var component = source.Blanks.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveBlanks(); + return component.GetViewModel; + } + public BlankViewModel? Delete(BlankBindingModel model) + { + var element = source.Blanks.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Blanks.Remove(element); + source.SaveBlanks(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs new file mode 100644 index 0000000..492156b --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -0,0 +1,74 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +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; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..6b5cf10 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +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; + } + + 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/LawFirmFileImplement.csproj b/LawFirm/LawFirmFileImplement/LawFirmFileImplement.csproj new file mode 100644 index 0000000..77fe5b1 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/LawFirmFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/LawFirm/LawFirmFileImplement/Models/Blank.cs b/LawFirm/LawFirmFileImplement/Models/Blank.cs new file mode 100644 index 0000000..c02a5b7 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Models/Blank.cs @@ -0,0 +1,59 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System.Xml.Linq; + +namespace LawFirmFileImplement.Models +{ + public class Blank : IBlankModel + { + public int Id { get; private set; } + public string BlankName { get; private set; } = string.Empty; + public double Price { get; set; } + public static Blank? Create(BlankBindingModel model) + { + if (model == null) + { + return null; + } + return new Blank() + { + Id = model.Id, + BlankName = model.BlankName, + Price = model.Price + }; + } + public static Blank? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Blank() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + BlankName = element.Element("BlankName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value) + }; + } + public void Update(BlankBindingModel model) + { + if (model == null) + { + return; + } + BlankName = model.BlankName; + Price = model.Price; + } + public BlankViewModel GetViewModel => new() + { + Id = Id, + BlankName = BlankName, + Price = Price + }; + public XElement GetXElement => new("Blank", + new XAttribute("Id", Id), + new XElement("BlankName", BlankName), + new XElement("Price", Price.ToString())); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs new file mode 100644 index 0000000..2647bf9 --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -0,0 +1,90 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +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())); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Models/Order.cs b/LawFirm/LawFirmFileImplement/Models/Order.cs new file mode 100644 index 0000000..549a84a --- /dev/null +++ b/LawFirm/LawFirmFileImplement/Models/Order.cs @@ -0,0 +1,87 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Enums; +using LawFirmDataModels.Models; +using System.Xml.Linq; + +namespace LawFirmFileImplement.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; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + 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; + } + var order = new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null), + }; + DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); + order.DateImplement = dateImpl; + + if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status)) + { + return null; + } + order.Status = status; + return order; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + 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), + 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())); + } +} \ No newline at end of file