117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using FoodOrdersContracts.BindingModels;
|
|
using FoodOrdersContracts.ViewModels;
|
|
using FoodOrdersDataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace FoodOrdersFileImplement.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 int MaxCountDishs { get; private set; }
|
|
public DateTime DateOpening { get; private set; }
|
|
public Dictionary<int, int> Dishs { get; private set; } = new();
|
|
private Dictionary<int, (IDishModel, int)>? _shopDishs = null;
|
|
|
|
public Dictionary<int, (IDishModel, int)> ShopDishs
|
|
{
|
|
get
|
|
{
|
|
if (_shopDishs == null)
|
|
{
|
|
var source = DataFileSingleton.GetInstance();
|
|
_shopDishs = Dishs.ToDictionary(
|
|
x => x.Key,
|
|
y => ((source.Dishs.FirstOrDefault(z => z.Id == y.Key) as IDishModel)!, y.Value)
|
|
);
|
|
}
|
|
return _shopDishs;
|
|
}
|
|
}
|
|
|
|
public static Shop? Create(ShopBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Shop()
|
|
{
|
|
Id = model.Id,
|
|
ShopName = model.ShopName,
|
|
Address = model.Address,
|
|
MaxCountDishs = model.MaxCountDishs,
|
|
DateOpening = model.DateOpening,
|
|
Dishs = model.ShopDishs.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,
|
|
MaxCountDishs = Convert.ToInt32(element.Element("MaxCountDishs")!.Value),
|
|
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
|
|
Dishs = element.Element("ShopDishs")!.Elements("ShopDish").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;
|
|
MaxCountDishs = model.MaxCountDishs;
|
|
DateOpening = model.DateOpening;
|
|
if (model.ShopDishs.Count > 0)
|
|
{
|
|
Dishs = model.ShopDishs.ToDictionary(x => x.Key, x => x.Value.Item2);
|
|
_shopDishs = null;
|
|
}
|
|
}
|
|
public ShopViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ShopName = ShopName,
|
|
Address = Address,
|
|
MaxCountDishs = MaxCountDishs,
|
|
DateOpening = DateOpening,
|
|
ShopDishs = ShopDishs,
|
|
};
|
|
|
|
public XElement GetXElement => new(
|
|
"Shop",
|
|
new XAttribute("Id", Id),
|
|
new XElement("ShopName", ShopName),
|
|
new XElement("Address", Address),
|
|
new XElement("MaxCountDishs", MaxCountDishs),
|
|
new XElement("DateOpening", DateOpening.ToString()),
|
|
new XElement("ShopDishs", Dishs.Select(x =>
|
|
new XElement("ShopDish",
|
|
new XElement("Key", x.Key),
|
|
new XElement("Value", x.Value)))
|
|
.ToArray()));
|
|
}
|
|
}
|