PIbd-21_MasenkinMS_Aircraft.../AircraftPlant/AircraftPlantFileImplement/Models/Component.cs

107 lines
3.0 KiB
C#
Raw Normal View History

2024-02-25 01:22:22 +04:00
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AircraftPlantFileImplement.Models
{
/// <summary>
/// Сущность "Компонент"
/// </summary>
public class Component : IComponentModel
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; private set; }
/// <summary>
/// Название компонента
/// </summary>
public string ComponentName { get; private set; } = string.Empty;
/// <summary>
/// Стоимость компонента
/// </summary>
public double Cost { get; set; }
/// <summary>
/// Создание модели компонента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static Component? Create(ComponentBindingModel? model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
/// <summary>
/// Создание модели компонента из данных файла
/// </summary>
/// <param name="element"></param>
/// <returns></returns>
public static Component? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Component()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
/// <summary>
/// Изменение модели компонента
/// </summary>
/// <param name="model"></param>
public void Update(ComponentBindingModel? model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
/// <summary>
/// Получение модели компонента
/// </summary>
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
/// <summary>
/// Запись данных о модели компонента в файл
/// </summary>
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
}
}