82 lines
1.6 KiB
C#
Raw Permalink Normal View History

2024-05-07 16:18:43 +04:00
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
2024-06-22 10:36:54 +04:00
using System.Runtime.Serialization;
2024-05-07 16:18:43 +04:00
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace SecuritySystemFileImplement.Models
{
2024-06-22 10:36:54 +04:00
//реализация интерфейса модели заготовки
[DataContract]
public class Sensor : ISensorModel
{
[DataMember]
public int Id { get; private set; }
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
[DataMember]
public string SensorName { get; private set; } = string.Empty;
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
[DataMember]
public double Cost { get; set; }
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
public static Sensor? Create(SensorBindingModel model)
{
if (model == null)
{
return null;
}
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
return new Sensor()
{
Id = model.Id,
SensorName = model.SensorName,
Cost = model.Cost
};
}
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
public static Sensor? Create(XElement element)
{
if (element == null)
{
return null;
}
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
return new Sensor()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SensorName = element.Element("SensorName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
public void Update(SensorBindingModel model)
{
if (model == null)
{
return;
}
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
SensorName = model.SensorName;
Cost = model.Cost;
}
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
public SensorViewModel GetViewModel => new()
{
Id = Id,
SensorName = SensorName,
Cost = Cost
};
2024-05-07 16:18:43 +04:00
2024-06-22 10:36:54 +04:00
public XElement GetXElement => new("Sensor",
new XAttribute("Id", Id),
new XElement("SensorName", SensorName),
new XElement("Cost", Cost.ToString()));
}
2024-05-07 16:18:43 +04:00
}