PIbd-21_Ihonkina_E.S._Preca.../PrecastConcreteFileImplement/Models/Shop.cs
2023-04-21 04:01:40 +03:00

110 lines
3.9 KiB
C#

using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement.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 DateOpening { get; private set; }
public int Capacity { get; private set; }
public Dictionary<int, int> ReinforcedsCount = new();
public Dictionary<int, (IReinforcedModel, int)>? _reinforceds = null;
public Dictionary<int, (IReinforcedModel, int)> Reinforceds
{
get
{
if (_reinforceds == null)
{
var source = DataFileSingleton.GetInstance();
_reinforceds = ReinforcedsCount.ToDictionary(
x => x.Key,
y => ((source.Reinforceds.FirstOrDefault(z => z.Id == y.Key) as IReinforcedModel)!,
y.Value)
);
}
return _reinforceds;
}
}
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
Capacity = model.Capacity,
ReinforcedsCount = model.Reinforceds.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,
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
Capacity = Convert.ToInt32(element.Element("Capacity")!.Value),
ReinforcedsCount = element.Element("Reinforceds")!.Elements("Reinforced")
.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;
DateOpening = model.DateOpening;
Capacity = model.Capacity;
ReinforcedsCount = model.Reinforceds.ToDictionary(x => x.Key, x => x.Value.Item2);
_reinforceds = null;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
Capacity = Capacity,
Reinforceds = Reinforceds
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("DateOpening", DateOpening.ToString()),
new XElement("Capacity", Capacity.ToString()),
new XElement("Reinforceds", ReinforcedsCount.Select(x =>
new XElement("Reinforced",
new XElement("Key", x.Key),
new XElement("Value", x.Value)))
.ToArray()));
}
}