diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Models/Shop.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Shop.cs new file mode 100644 index 0000000..aeb1cc6 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Models/Shop.cs @@ -0,0 +1,115 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyDataModels.Models; +using FurnitureAssemblyContracts.ViewModels; +using System.Xml.Linq; +using System.Diagnostics; + +namespace FurnitureAssemFileImplement.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; } + + private Dictionary? _furnitures = null; + public Dictionary Furnitures + { + get + { + if (_furnitures == null) + { + var source = DataFileSingleton.GetInstance(); + _furnitures = CurrentCount.ToDictionary( + x => x.Key, + y => (source.Furnitures.FirstOrDefault(z => z.Id == y.Key)! as IFurnitureModel, y.Value)); + + } + return _furnitures; + } + private set { } + } + public int MaxCount { get; private set; } + + public Dictionary CurrentCount { get; private set; } = new(); + + 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, + MaxCount = model.MaxCount, + CurrentCount = model.Furnitures.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + MaxCount = Convert.ToInt32(element.Element("MaxCount")!.Value), + CurrentCount = element.Element("CurrentCount")!.Elements("CurrentCountFurniture") + .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; + MaxCount = model.MaxCount; + DateOpening = model.DateOpening; + if (model.Furnitures.Count > 0) + { + CurrentCount = model.Furnitures.ToDictionary(x => x.Key, x => x.Value.Item2); + _furnitures = null; + } + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCount = MaxCount, + Furnitures = Furnitures + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening), + new XElement("MaxCount", MaxCount), + new XElement("CurrentCount", + CurrentCount.Select( + x => new XElement("CurrentCountFurniture", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs index 379850c..58f3782 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs @@ -16,6 +16,10 @@ namespace FurnitureAssemblyContracts.BindingModels public DateTime DateOpening { get; set; } - public Dictionary Furnitures { get; set; } = new(); + public Dictionary Furnitures { get; set; } = new(); + + public int MaxCount { get; set; } + + } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs index 0a738fc..37982f1 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs @@ -18,6 +18,8 @@ namespace FurnitureAssemblyContracts.ViewModels [DisplayName("Дата открытия")] public DateTime DateOpening { get; set; } - public Dictionary Furnitures { get; set; } = new(); + public Dictionary Furnitures { get; set; } = new(); + [DisplayName("Вместимость")] + public int MaxCount { get; set; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs index bd5efbf..b413694 100644 --- a/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs @@ -12,5 +12,6 @@ namespace FurnitureAssemblyDataModels.Models string Address { get; } DateTime DateOpening { get; } Dictionary Furnitures { get; } + int MaxCount { get; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs index 4b64a67..1a73183 100644 --- a/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs @@ -21,6 +21,8 @@ namespace FurnitureAssemblyListImplement.Models public Dictionary Furnitures { get; private set; } = new(); + public int MaxCount { get; private set; } + public static Shop? Create(ShopBindingModel? model) { if (model == null) @@ -32,6 +34,7 @@ namespace FurnitureAssemblyListImplement.Models Id = model.Id, ShopName = model.ShopName, Address = model.Address, + MaxCount = model.MaxCount, DateOpening = model.DateOpening, Furnitures = model.Furnitures }; @@ -45,6 +48,7 @@ namespace FurnitureAssemblyListImplement.Models } ShopName = model.ShopName; Address = model.Address; + MaxCount = model.MaxCount; DateOpening = model.DateOpening; Furnitures = model.Furnitures; } @@ -54,6 +58,7 @@ namespace FurnitureAssemblyListImplement.Models Id = Id, ShopName = ShopName, Address = Address, + MaxCount = MaxCount, DateOpening = DateOpening, Furnitures = Furnitures };