PIbd-23_Nevaeva_K.A._Travel.../TravelCompany/TravelCompanyFileImplement/Models/Condition.cs

71 lines
2.0 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.ViewModels;
using TravelCompanyDataModels.Models;
namespace TravelCompanyFileImplement.Models
{
[DataContract]
public class Condition : IConditionModel
{
[DataMember]
public int Id { get; private set; }
[DataMember]
public string ConditionName { get; private set; } = string.Empty;
[DataMember]
public double Cost { get; set; }
public static Condition? Create(ConditionBindingModel model)
{
if (model == null)
{
return null;
}
return new Condition()
{
Id = model.Id,
ConditionName = model.ConditionName,
Cost = model.Cost
};
}
public static Condition? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Condition()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ConditionName = element.Element("ConditionName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(ConditionBindingModel model)
{
if (model == null)
{
return;
}
ConditionName = model.ConditionName;
Cost = model.Cost;
}
public ConditionViewModel GetViewModel => new()
{
Id = Id,
ConditionName = ConditionName,
Cost = Cost
};
public XElement GetXElement => new("Condition",
new XAttribute("Id", Id),
new XElement("ConditionName", ConditionName),
new XElement("Cost", Cost.ToString()));
}
}