105 lines
3.6 KiB
C#
105 lines
3.6 KiB
C#
using GiftShopContracts.BindingModels;
|
|
using GiftShopContracts.ViewModels;
|
|
using GiftShopDataModels.Models;
|
|
using System.Xml.Linq;
|
|
|
|
namespace GiftShopFileImplement.Models
|
|
{
|
|
public class Shop : IShopModel
|
|
{
|
|
public int Id { get; set; }
|
|
public string ShopName { get; private set; }
|
|
public string ShopAdress { get; private set; }
|
|
public DateTime OpeningDate { get; private set; }
|
|
public Dictionary<int, int> Gifts { get; private set; } = new();
|
|
|
|
private Dictionary<int, (IGiftModel, int)>? _shopGifts = null;
|
|
public Dictionary<int, (IGiftModel, int)> ShopGifts
|
|
{
|
|
get
|
|
{
|
|
if (_shopGifts == null)
|
|
{
|
|
var source = DataFileSingleton.GetInstance();
|
|
_shopGifts = Gifts.ToDictionary(x => x.Key, y =>
|
|
((source.Gifts.FirstOrDefault(z => z.Id == y.Key) as IGiftModel)!,
|
|
y.Value));
|
|
}
|
|
return _shopGifts;
|
|
}
|
|
}
|
|
|
|
public int MaxCapacity { get; private set; }
|
|
|
|
public static Shop? Create(ShopBindingModel model)
|
|
{
|
|
if (model == null)
|
|
return null;
|
|
return new Shop()
|
|
{
|
|
Id = model.Id,
|
|
ShopName = model.ShopName,
|
|
ShopAdress = model.ShopAdress,
|
|
OpeningDate = model.OpeningDate,
|
|
MaxCapacity = model.MaxCapacity,
|
|
Gifts = model.ShopGifts.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,
|
|
ShopAdress = element.Element("ShopAdress")!.Value,
|
|
MaxCapacity = Convert.ToInt32(element.Element("MaxCapacity")!.Value),
|
|
OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
|
|
Gifts = element.Element("ShopGifts")!.Elements("ShopGifts").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;
|
|
ShopAdress = model.ShopAdress;
|
|
OpeningDate = model.OpeningDate;
|
|
MaxCapacity = model.MaxCapacity;
|
|
if (model.ShopGifts.Count > 0)
|
|
{
|
|
Gifts = model.ShopGifts.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
_shopGifts = null;
|
|
}
|
|
}
|
|
|
|
public ShopViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ShopName = ShopName,
|
|
ShopAdress = ShopAdress,
|
|
OpeningDate = OpeningDate,
|
|
MaxCapacity = MaxCapacity,
|
|
ShopGifts = ShopGifts
|
|
};
|
|
|
|
public XElement GetXElement => new("Shop",
|
|
new XAttribute("Id", Id),
|
|
new XElement("ShopName", ShopName),
|
|
new XElement("ShopAdress", ShopAdress),
|
|
new XElement("OpeningDate", OpeningDate),
|
|
new XElement("MaxCapacity", MaxCapacity),
|
|
new XElement("Gifts", Gifts.Select(x => new XElement("Gifts",
|
|
new XElement("Key", x.Key),
|
|
new XElement("Value", x.Value))).ToArray()));
|
|
}
|
|
}
|