Borschevskaya A.A. Lab Work 2 Hard #4

Closed
pgirl1 wants to merge 17 commits from lab2_hard into lab1_hard
5 changed files with 129 additions and 2 deletions
Showing only changes of commit d31da227a0 - Show all commits

View File

@ -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<int, (IFurnitureModel, int)>? _furnitures = null;
public Dictionary<int, (IFurnitureModel, int)> 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<int, int> 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()));
}
}

View File

@ -16,6 +16,10 @@ namespace FurnitureAssemblyContracts.BindingModels
public DateTime DateOpening { get; set; }
public Dictionary<int, (IFurnitureModel, int)> Furnitures { get; set; } = new();
public Dictionary<int, (IFurnitureModel, int)> Furnitures { get; set; } = new();
public int MaxCount { get; set; }
}
}

View File

@ -18,6 +18,8 @@ namespace FurnitureAssemblyContracts.ViewModels
[DisplayName("Дата открытия")]
public DateTime DateOpening { get; set; }
public Dictionary<int, (IFurnitureModel, int)> Furnitures { get; set; } = new();
public Dictionary<int, (IFurnitureModel, int)> Furnitures { get; set; } = new();
[DisplayName("Вместимость")]
public int MaxCount { get; set; }
}
}

View File

@ -12,5 +12,6 @@ namespace FurnitureAssemblyDataModels.Models
string Address { get; }
DateTime DateOpening { get; }
Dictionary<int, (IFurnitureModel, int)> Furnitures { get; }
int MaxCount { get; }
}
}

View File

@ -21,6 +21,8 @@ namespace FurnitureAssemblyListImplement.Models
public Dictionary<int, (IFurnitureModel, int)> 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
};