109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
using ComputersShopContracts.BindingModels;
|
|
using ComputersShopContracts.ViewModels;
|
|
using ComputersShopDataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace ComputersShopFileImplement.Models
|
|
{
|
|
public class Shop : IShopModel
|
|
{
|
|
public int Id { get; private set; }
|
|
public string ShopName { get; private set; } = string.Empty;
|
|
public string ShopAddress { get; private set; } = string.Empty;
|
|
public DateTime DateOpening { get; private set; }
|
|
public int Capacity { get; private set; }
|
|
public Dictionary<int, int> ComputersCount = new();
|
|
public Dictionary<int, (IComputerModel, int)>? _computers = null;
|
|
public Dictionary<int, (IComputerModel, int)> Computers
|
|
{
|
|
get
|
|
{
|
|
if (_computers == null)
|
|
{
|
|
var source = DataFileSingleton.GetInstance();
|
|
_computers = ComputersCount.ToDictionary(
|
|
x => x.Key,
|
|
y => ((source.Computers.FirstOrDefault(z => z.Id == y.Key) as IComputerModel)!,
|
|
y.Value)
|
|
);
|
|
}
|
|
return _computers;
|
|
}
|
|
}
|
|
public static Shop? Create(ShopBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Shop()
|
|
{
|
|
Id = model.Id,
|
|
ShopName = model.ShopName,
|
|
ShopAddress = model.ShopAddress,
|
|
DateOpening = model.DateOpening,
|
|
Capacity = model.Capacity,
|
|
ComputersCount = model.Computers.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,
|
|
ShopAddress = element.Element("ShopAddress")!.Value,
|
|
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
|
|
Capacity = Convert.ToInt32(element.Element("Capacity")!.Value),
|
|
ComputersCount = element.Element("Computers")!.Elements("Computer")
|
|
.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;
|
|
ShopAddress = model.ShopAddress;
|
|
DateOpening = model.DateOpening;
|
|
Capacity = model.Capacity;
|
|
ComputersCount = model.Computers.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
_computers = null;
|
|
|
|
}
|
|
public ShopViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ShopName = ShopName,
|
|
ShopAddress = ShopAddress,
|
|
DateOpening = DateOpening,
|
|
Capacity = Capacity,
|
|
Computers = Computers
|
|
};
|
|
public XElement GetXElement => new("Shop",
|
|
new XAttribute("Id", Id),
|
|
new XElement("ShopName", ShopName),
|
|
new XElement("ShopAddress", ShopAddress),
|
|
new XElement("DateOpening", DateOpening.ToString()),
|
|
new XElement("Capacity", Capacity.ToString()),
|
|
new XElement("Computers", ComputersCount.Select(x =>
|
|
new XElement("Computer",
|
|
new XElement("Key", x.Key),
|
|
new XElement("Value", x.Value)))
|
|
.ToArray()));
|
|
}
|
|
}
|