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()) + ); + } +}