103 lines
3.6 KiB
C#
103 lines
3.6 KiB
C#
using ShipyardContracts.BindingModels;
|
|
using ShipyardContracts.ViewModels;
|
|
using ShipyardDataModels.Models;
|
|
using System.Xml.Linq;
|
|
|
|
namespace ShipyardFileImplement.Models
|
|
{
|
|
public class Shop : IShopModel
|
|
{
|
|
public int Id { get; private set; }
|
|
public string ShopName { get; private set; } = string.Empty;
|
|
public string Address { get; private set; } = string.Empty;
|
|
public DateTime DateOpen { get; private set; }
|
|
public int Capacity { get; private set; }
|
|
public Dictionary<int, int> ShipsCount = new();
|
|
public Dictionary<int, (IShipModel, int)>? _ships = null;
|
|
public Dictionary<int, (IShipModel, int)> ShopShips
|
|
{
|
|
get
|
|
{
|
|
if (_ships == null)
|
|
{
|
|
var source = DataFileSingleton.GetInstance();
|
|
_ships = ShipsCount.ToDictionary(
|
|
x => x.Key,
|
|
y => ((source.Ships.FirstOrDefault(z => z.Id == y.Key) as IShipModel)!,
|
|
y.Value)
|
|
);
|
|
}
|
|
return _ships;
|
|
}
|
|
}
|
|
public static Shop? Create(ShopBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Shop()
|
|
{
|
|
Id = model.Id,
|
|
ShopName = model.ShopName,
|
|
Address = model.Address,
|
|
DateOpen = model.DateOpen,
|
|
Capacity = model.Capacity,
|
|
ShipsCount = model.ShopShips.ToDictionary(x => x.Key, x => x.Value.Item2)
|
|
};
|
|
}
|
|
public static Shop? Create(XElement element)
|
|
{
|
|
if (element == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Shop()
|
|
{
|
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|
ShopName = element.Element("ShopName")!.Value,
|
|
Address = element.Element("Address")!.Value,
|
|
DateOpen = Convert.ToDateTime(element.Element("DateOpening")!.Value),
|
|
Capacity = Convert.ToInt32(element.Element("Capacity")!.Value),
|
|
ShipsCount = element.Element("Ships")!.Elements("Ship")
|
|
.ToDictionary(
|
|
x => Convert.ToInt32(x.Element("Key")?.Value),
|
|
x => Convert.ToInt32(x.Element("Value")?.Value))
|
|
};
|
|
}
|
|
public void Update(ShopBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
ShopName = model.ShopName;
|
|
Address = model.Address;
|
|
DateOpen = model.DateOpen;
|
|
Capacity = model.Capacity;
|
|
ShipsCount = model.ShopShips.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
_ships = null;
|
|
|
|
}
|
|
public ShopViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ShopName = ShopName,
|
|
Address = Address,
|
|
DateOpen = DateOpen,
|
|
Capacity = Capacity,
|
|
ShopShips = ShopShips
|
|
};
|
|
public XElement GetXElement => new("Shop",
|
|
new XAttribute("Id", Id),
|
|
new XElement("ShopName", ShopName),
|
|
new XElement("Address", Address),
|
|
new XElement("DateOpening", DateOpen.ToString()),
|
|
new XElement("Capacity", Capacity.ToString()),
|
|
new XElement("Ships", ShipsCount.Select(x =>
|
|
new XElement("Ship",
|
|
new XElement("Key", x.Key),
|
|
new XElement("Value", x.Value)))
|
|
.ToArray()));
|
|
}
|
|
} |