PIbd-22-Stroev-V.M.-Plumbin.../PlumbingRepair/PlumbingRepairFileImplement/Models/Work.cs

97 lines
3.3 KiB
C#
Raw Normal View History

2024-03-12 23:36:19 +04:00
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
2024-05-21 20:02:14 +04:00
using System.Runtime.Serialization;
2024-03-12 23:36:19 +04:00
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace PlumbingRepairFileImplement.Models
{
2024-05-21 20:02:14 +04:00
[DataContract]
2024-03-12 23:36:19 +04:00
public class Work : IWorkModel
{
2024-05-21 20:02:14 +04:00
[DataMember]
2024-03-12 23:36:19 +04:00
public int Id { get; private set; }
2024-05-21 20:02:14 +04:00
[DataMember]
2024-03-12 23:36:19 +04:00
public string WorkName { get; private set; } = string.Empty;
2024-05-21 20:02:14 +04:00
[DataMember]
2024-03-12 23:36:19 +04:00
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _workComponents = null;
2024-05-21 20:02:14 +04:00
[DataMember]
2024-03-12 23:36:19 +04:00
public Dictionary<int, (IComponentModel, int)> WorkComponents
{
get
{
if(_workComponents == null)
{
2024-03-13 10:28:19 +04:00
var source = DataFileSingleton.GetInstance();
_workComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key)
as IComponentModel)!, y.Value));
2024-03-12 23:36:19 +04:00
}
return _workComponents;
}
}
public static Work? Create(WorkBindingModel? model)
{
if (model == null)
{
return null;
}
return new Work()
{
Id = model.Id,
WorkName = model.WorkName,
Price = model.Price,
Components = model.WorkComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Work? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Work()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
WorkName = element.Element("WorkName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("WorkComponents")!.Elements("WorkComponent")
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(WorkBindingModel? model)
{
if (model == null)
{
return;
}
WorkName = model.WorkName;
Price = model.Price;
Components = model.WorkComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_workComponents = null;
}
public WorkViewModel GetViewModel => new()
{
Id = Id,
WorkName = WorkName,
Price = Price,
WorkComponents = WorkComponents
};
2024-03-13 10:28:19 +04:00
public XElement GetXElement => new("Work",
2024-03-12 23:36:19 +04:00
new XAttribute("Id", Id),
new XElement("WorkName", WorkName),
new XElement("Price", Price.ToString()),
new XElement("WorkComponents", Components.Select(x =>
new XElement("WorkComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}