107 lines
3.2 KiB
C#
Raw Normal View History

2024-05-24 12:23:50 +04:00
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
{
2024-05-24 13:05:47 +04:00
public class Users : IUsersModel
2024-05-24 12:23:50 +04:00
{
public int Id { get; private set; }
2024-05-24 13:05:47 +04:00
public string UsersName { get; private set; } = string.Empty;
2024-05-24 12:23:50 +04:00
public double Price { get; private set; }
2024-05-24 14:23:37 +04:00
public Dictionary<int, int> Komments { get; private set; } = new();
2024-05-24 12:23:50 +04:00
2024-05-24 14:23:37 +04:00
private Dictionary<int, (IKommentModel, int)>? _usersKomments = null;
2024-05-24 12:23:50 +04:00
2024-05-24 14:23:37 +04:00
public Dictionary<int, (IKommentModel, int)> UsersKomments
2024-05-24 12:23:50 +04:00
{
get
{
2024-05-24 14:23:37 +04:00
if (_usersKomments == null)
2024-05-24 12:23:50 +04:00
{
var source = DataFileSingleton.GetInstance();
2024-05-24 14:23:37 +04:00
_usersKomments = Komments.ToDictionary(x => x.Key,
y => ((source.Komments.FirstOrDefault(z => z.Id == y.Key) as IKommentModel)!, y.Value));
2024-05-24 12:23:50 +04:00
}
2024-05-24 14:23:37 +04:00
return _usersKomments;
2024-05-24 12:23:50 +04:00
}
}
2024-05-24 13:05:47 +04:00
public static Users? Create(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
if (model == null)
{
return null;
}
2024-05-24 13:05:47 +04:00
return new Users()
2024-05-24 12:23:50 +04:00
{
Id = model.Id,
2024-05-24 13:05:47 +04:00
UsersName = model.UsersName,
2024-05-24 12:23:50 +04:00
Price = model.Price,
2024-05-24 14:23:37 +04:00
Komments = model.UsersKomments.ToDictionary(x => x.Key, x => x.Value.Item2)
2024-05-24 12:23:50 +04:00
};
}
2024-05-24 13:05:47 +04:00
public static Users? Create(XElement element)
2024-05-24 12:23:50 +04:00
{
if (element == null)
{
return null;
}
2024-05-24 13:05:47 +04:00
return new Users()
2024-05-24 12:23:50 +04:00
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
2024-05-24 13:05:47 +04:00
UsersName = element.Element("UsersName")!.Value,
2024-05-24 12:23:50 +04:00
Price = Convert.ToDouble(element.Element("Price")!.Value),
2024-05-24 14:23:37 +04:00
Komments = element.Element("FurnitureWorkPieces")!.Elements("FurnitureWorkPieces").ToDictionary(
2024-05-24 12:23:50 +04:00
x => Convert.ToInt32(x.Element("Key")?.Value),
y => Convert.ToInt32(y.Element("Value")?.Value))
};
}
2024-05-24 13:05:47 +04:00
public void Update(UsersBindingModel model)
2024-05-24 12:23:50 +04:00
{
if (model == null)
{
return;
}
2024-05-24 13:05:47 +04:00
UsersName = model.UsersName;
2024-05-24 12:23:50 +04:00
Price = model.Price;
2024-05-24 14:23:37 +04:00
Komments = model.UsersKomments.ToDictionary(x => x.Key, x => x.Value.Item2);
_usersKomments = null;
2024-05-24 12:23:50 +04:00
}
2024-05-24 14:23:37 +04:00
public UsersViewModel GetViewModel => new()
2024-05-24 12:23:50 +04:00
{
Id = Id,
2024-05-24 13:05:47 +04:00
UsersName = UsersName,
2024-05-24 12:23:50 +04:00
Price = Price,
2024-05-24 14:23:37 +04:00
UsersKomments = UsersKomments
2024-05-24 12:23:50 +04:00
};
2024-05-24 14:23:37 +04:00
public XElement GetXElement => new("User",
2024-05-24 12:23:50 +04:00
new XAttribute("Id", Id),
2024-05-24 13:05:47 +04:00
new XElement("UsersName", UsersName),
2024-05-24 12:23:50 +04:00
new XElement("Price", Price.ToString()),
2024-05-24 14:23:37 +04:00
new XElement("UsersKomments", Komments.Select(
x => new XElement("UsersKomments",
2024-05-24 12:23:50 +04:00
new XElement("Key", x.Key),
new XElement("Value", x.Value))
).ToArray()));
}
}