102 lines
3.2 KiB
C#
102 lines
3.2 KiB
C#
|
using GiftShopContracts.BindingModels;
|
|||
|
using GiftShopContracts.ViewModels;
|
|||
|
using GiftShopDataModels.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Xml.Linq;
|
|||
|
|
|||
|
namespace GiftShopFileImplement.Models
|
|||
|
{
|
|||
|
public class Gift : IGiftModel
|
|||
|
{
|
|||
|
public int Id { get; private set; }
|
|||
|
public string GiftName { get; private set; } = string.Empty;
|
|||
|
public double Price { get; private set; }
|
|||
|
public Dictionary<int, int> Components { get; private set; } = new();
|
|||
|
private Dictionary<int, (IComponentModel, int)>? _productComponents =
|
|||
|
null;
|
|||
|
public Dictionary<int, (IComponentModel, int)> GiftComponents
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_productComponents == null)
|
|||
|
{
|
|||
|
var source = DataFileSingleton.GetInstance();
|
|||
|
_productComponents = Components.ToDictionary(x => x.Key, y =>
|
|||
|
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
|
|||
|
y.Value));
|
|||
|
}
|
|||
|
return _productComponents;
|
|||
|
}
|
|||
|
}
|
|||
|
public static Gift? Create(GiftBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Gift()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
GiftName = model.GiftName,
|
|||
|
Price = model.Price,
|
|||
|
Components = model.GiftComponents.ToDictionary(x => x.Key, x
|
|||
|
=> x.Value.Item2)
|
|||
|
};
|
|||
|
}
|
|||
|
public static Gift? Create(XElement element)
|
|||
|
{
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Gift()
|
|||
|
{
|
|||
|
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
|||
|
GiftName = element.Element("GiftName")!.Value,
|
|||
|
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
|||
|
Components =
|
|||
|
element.Element("GiftComponents")!.Elements("GiftComponent")
|
|||
|
.ToDictionary(x =>
|
|||
|
Convert.ToInt32(x.Element("Key")?.Value), x =>
|
|||
|
Convert.ToInt32(x.Element("Value")?.Value))
|
|||
|
};
|
|||
|
}
|
|||
|
public void Update(GiftBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
GiftName = model.GiftName;
|
|||
|
Price = model.Price;
|
|||
|
Components = model.GiftComponents.ToDictionary(x => x.Key, x =>
|
|||
|
x.Value.Item2);
|
|||
|
_productComponents = null;
|
|||
|
}
|
|||
|
public GiftViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
GiftName = GiftName,
|
|||
|
Price = Price,
|
|||
|
GiftComponents = GiftComponents
|
|||
|
};
|
|||
|
public XElement GetXElement => new("Gift",
|
|||
|
new XAttribute("Id", Id),
|
|||
|
new XElement("GiftName", GiftName),
|
|||
|
new XElement("Price", Price.ToString()),
|
|||
|
new XElement("GiftComponents", Components.Select(x =>
|
|||
|
new XElement("GiftComponent",
|
|||
|
|
|||
|
new XElement("Key", x.Key),
|
|||
|
|
|||
|
new XElement("Value", x.Value)))
|
|||
|
|
|||
|
.ToArray()));
|
|||
|
}
|
|||
|
|
|||
|
}
|