PIbd-22_Petrushin_E.A._LawFirm/LawFirm/AbstractLawFirmFileImpliment/Models/Document.cs

97 lines
3.4 KiB
C#
Raw Normal View History

2024-03-12 21:53:02 +04:00
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<int, int> Components { get; private set; } = new();
2024-03-13 01:12:49 +04:00
private Dictionary<int, (IComponentModel, int)>? _documentComponents = null;
public Dictionary<int, (IComponentModel, int)> DocumentComponents
2024-03-12 21:53:02 +04:00
{
get
{
2024-03-13 01:12:49 +04:00
if (_documentComponents == null)
2024-03-12 21:53:02 +04:00
{
var source = DataFileSingleton.GetInstance();
2024-03-13 01:12:49 +04:00
_documentComponents = Components.ToDictionary(x => x.Key, y =>
2024-03-12 21:53:02 +04:00
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
y.Value));
}
2024-03-13 01:12:49 +04:00
return _documentComponents;
2024-03-12 21:53:02 +04:00
}
}
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),
2024-03-13 01:12:49 +04:00
DocumentName = element.Element("DocumentName")!.Value,
2024-03-12 21:53:02 +04:00
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components =
2024-03-13 01:12:49 +04:00
element.Element("DocumentComponents")!.Elements("DocumentComponent")
2024-03-12 21:53:02 +04:00
.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);
2024-03-13 01:12:49 +04:00
_documentComponents = null;
2024-03-12 21:53:02 +04:00
}
public DocumentViewModel GetViewModel => new()
{
Id = Id,
DocumentName = DocumentName,
Price = Price,
2024-03-13 01:12:49 +04:00
DocumentComponents = DocumentComponents
2024-03-12 21:53:02 +04:00
};
2024-03-13 01:12:49 +04:00
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()));
2024-03-12 21:53:02 +04:00
}
}