PIbd-22_Smirnov_A.A._Securi.../SecuritySystem/SecuritySystemFileImplement/Models/Sensor.cs
2024-05-07 16:18:43 +04:00

77 lines
2.0 KiB
C#

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