PIbd-23_Leonteva_V._A._LawFirm/LawFirm/LawFirmFileImplement/Models/Document.cs

96 lines
3.2 KiB
C#
Raw Normal View History

2024-04-22 22:48:19 +04:00
using LawFirmContracts.BindingModels;
using LawFirmContracts.ViewModels;
using LawFirmDataModels.Models;
using System.Xml.Linq;
2024-06-19 23:52:53 +04:00
using System.Runtime.Serialization;
2024-04-22 22:48:19 +04:00
namespace LawFirmFileImplement.Models
{
2024-06-19 23:52:53 +04:00
[DataContract]
public class Document : IDocumentModel
2024-04-22 22:48:19 +04:00
{
2024-06-19 23:52:53 +04:00
[DataMember]
public int Id { get; private set; }
[DataMember]
public string DocumentName { get; private set; } = string.Empty;
[DataMember]
public double Price { get; private set; }
2024-04-22 22:48:19 +04:00
public Dictionary<int, int> Blanks { get; private set; } = new();
private Dictionary<int, (IBlankModel, int)>? _documentBlanks = null;
2024-06-19 23:52:53 +04:00
[DataMember]
public Dictionary<int, (IBlankModel, int)> DocumentBlanks
2024-04-22 22:48:19 +04:00
{
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()));
}
}