SUBD_Gerimovich_Ilya_PIbd-22/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Users.cs
platoff aeeee 4b0700dee1 20
2024-05-24 14:23:37 +04:00

107 lines
3.2 KiB
C#

using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FurnitureAssemblyFileImplement.Models
{
public class Users : IUsersModel
{
public int Id { get; private set; }
public string UsersName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Komments { get; private set; } = new();
private Dictionary<int, (IKommentModel, int)>? _usersKomments = null;
public Dictionary<int, (IKommentModel, int)> UsersKomments
{
get
{
if (_usersKomments == null)
{
var source = DataFileSingleton.GetInstance();
_usersKomments = Komments.ToDictionary(x => x.Key,
y => ((source.Komments.FirstOrDefault(z => z.Id == y.Key) as IKommentModel)!, y.Value));
}
return _usersKomments;
}
}
public static Users? Create(UsersBindingModel model)
{
if (model == null)
{
return null;
}
return new Users()
{
Id = model.Id,
UsersName = model.UsersName,
Price = model.Price,
Komments = model.UsersKomments.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Users? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Users()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
UsersName = element.Element("UsersName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Komments = element.Element("FurnitureWorkPieces")!.Elements("FurnitureWorkPieces").ToDictionary(
x => Convert.ToInt32(x.Element("Key")?.Value),
y => Convert.ToInt32(y.Element("Value")?.Value))
};
}
public void Update(UsersBindingModel model)
{
if (model == null)
{
return;
}
UsersName = model.UsersName;
Price = model.Price;
Komments = model.UsersKomments.ToDictionary(x => x.Key, x => x.Value.Item2);
_usersKomments = null;
}
public UsersViewModel GetViewModel => new()
{
Id = Id,
UsersName = UsersName,
Price = Price,
UsersKomments = UsersKomments
};
public XElement GetXElement => new("User",
new XAttribute("Id", Id),
new XElement("UsersName", UsersName),
new XElement("Price", Price.ToString()),
new XElement("UsersKomments", Komments.Select(
x => new XElement("UsersKomments",
new XElement("Key", x.Key),
new XElement("Value", x.Value))
).ToArray()));
}
}