PIbd-23_Borschevskaya_A.A._.../FurnitureAssembly/FurnitureAssemFileImplement/Models/MessageInfo.cs

98 lines
3.1 KiB
C#

using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Models;
using System.Xml.Linq;
namespace FurnitureAssemFileImplement.Models
{
public class MessageInfo : IMessageInfoModel
{
public string MessageId { get; private set; } = string.Empty;
public int? ClientId { get; private set; }
public string SenderName { get; private set; } = string.Empty;
public DateTime DateDelivery { get; private set; } = DateTime.Now;
public string Subject { get; private set; } = string.Empty;
public string Body { get; private set; } = string.Empty;
public string? Reply { get; private set; }
public bool IsRead { get; private set; }
public static MessageInfo? Create(MessageInfoBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
MessageId = model.MessageId,
ClientId = model.ClientId,
Body = model.Body,
Subject = model.Subject,
SenderName = model.SenderName,
DateDelivery = model.DateDelivery,
IsRead = model.IsRead,
Reply = model.Reply
};
}
public static MessageInfo? Create(XElement element)
{
if (element == null)
{
return null;
}
return new MessageInfo()
{
MessageId = element.Element("MessageId")!.Value,
ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value),
Body = element.Element("Body")!.Value,
Subject = element.Element("Subject")!.Value,
SenderName = element.Element("SenderName")!.Value,
DateDelivery = DateTime.ParseExact(element.Element("DateDelivery")!.Value, "G", null),
IsRead = Convert.ToBoolean(element.Element("IsRead")!.Value),
Reply = element.Element("Reply")!.Value,
};
}
public void Update(MessageInfoBindingModel model)
{
if (model == null)
{
return;
}
IsRead = model.IsRead;
Reply = model.Reply;
}
public MessageInfoViewModel GetViewModel => new()
{
Body = Body,
Subject = Subject,
ClientId = ClientId,
MessageId = MessageId,
SenderName = SenderName,
DateDelivery = DateDelivery,
IsRead = IsRead,
Reply = Reply
};
public XElement GetXElement => new("MessageInfo",
new XElement("SenderName", SenderName),
new XElement("ClientId", ClientId),
new XElement("DateDelivery", DateDelivery),
new XAttribute("MessageId", MessageId),
new XElement("Body", Body),
new XElement("Subject", Subject),
new XElement("IsRead", IsRead),
new XElement("Reply", Reply)
);
}
}