2023-03-06 09:07:54 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2023-06-03 03:05:40 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2023-03-06 09:07:54 +04:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Linq;
|
2023-06-03 03:05:40 +04:00
|
|
|
|
using TravelCompanyContracts.BindingModels;
|
|
|
|
|
using TravelCompanyContracts.ViewModels;
|
|
|
|
|
using TravelCompanyDataModels.Models;
|
2023-03-06 09:07:54 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace TravelCompanyFileImplement.Models
|
|
|
|
|
{
|
2023-06-03 03:05:40 +04:00
|
|
|
|
[DataContract]
|
2023-03-06 09:07:54 +04:00
|
|
|
|
public class Travel : ITravelModel
|
|
|
|
|
{
|
2023-06-03 03:05:40 +04:00
|
|
|
|
[DataMember]
|
2023-03-06 09:07:54 +04:00
|
|
|
|
public int Id { get; private set; }
|
2023-06-03 03:05:40 +04:00
|
|
|
|
[DataMember]
|
2023-03-06 09:07:54 +04:00
|
|
|
|
public string TravelName { get; private set; } = string.Empty;
|
2023-06-03 03:05:40 +04:00
|
|
|
|
[DataMember]
|
2023-03-06 09:07:54 +04:00
|
|
|
|
public double Price { get; private set; }
|
2023-06-03 03:05:40 +04:00
|
|
|
|
[DataMember]
|
2023-03-06 09:07:54 +04:00
|
|
|
|
public Dictionary<int, int> Conditions { get; private set; } = new();
|
|
|
|
|
private Dictionary<int, (IConditionModel, int)>? _travelConditions = null;
|
|
|
|
|
public Dictionary<int, (IConditionModel, int)> TravelConditions
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_travelConditions == null)
|
|
|
|
|
{
|
|
|
|
|
var source = DataFileSingleton.GetInstance();
|
|
|
|
|
_travelConditions = Conditions.ToDictionary(x => x.Key, y => ((source.Conditions.FirstOrDefault(z => z.Id == y.Key) as IConditionModel)!,y.Value));
|
|
|
|
|
}
|
|
|
|
|
return _travelConditions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static Travel? Create(TravelBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Travel()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
TravelName = model.TravelName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Conditions = model.TravelConditions.ToDictionary(x => x.Key, x=> x.Value.Item2)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public static Travel? Create(XElement element)
|
|
|
|
|
{
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Travel()
|
|
|
|
|
{
|
|
|
|
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|
|
|
|
TravelName = element.Element("TravelName")!.Value,
|
|
|
|
|
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
|
|
|
|
Conditions = element.Element("TravelConditions")!.Elements("TravelCondition").ToDictionary(x =>Convert.ToInt32(x.Element("Key")?.Value), x =>Convert.ToInt32(x.Element("Value")?.Value))
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(TravelBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
TravelName = model.TravelName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
Conditions = model.TravelConditions.ToDictionary(x => x.Key, x =>x.Value.Item2);
|
|
|
|
|
_travelConditions = null;
|
|
|
|
|
}
|
|
|
|
|
public TravelViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
TravelName = TravelName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
TravelConditions = TravelConditions
|
|
|
|
|
};
|
|
|
|
|
public XElement GetXElement => new("Travel",
|
|
|
|
|
new XAttribute("Id", Id),
|
|
|
|
|
new XElement("TravelName", TravelName),
|
|
|
|
|
new XElement("Price", Price.ToString()),
|
|
|
|
|
new XElement("TravelConditions", Conditions.Select(x =>
|
|
|
|
|
new XElement("TravelCondition",
|
|
|
|
|
|
|
|
|
|
new XElement("Key", x.Key),
|
|
|
|
|
|
|
|
|
|
new XElement("Value", x.Value)))
|
|
|
|
|
|
|
|
|
|
.ToArray()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|