ISEbd-21_Agliullov.D.A._Con.../ConfectionaryFileImplement/Order.cs

111 lines
4.0 KiB
C#

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Enums;
using ConfectioneryDataModels.Models;
using System.Xml.Linq;
namespace ConfectioneryFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int PastryId { get; private set; }
public int ClientId { get; set; }
public int? ImplementerId { get; set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; }
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
PastryId = model.PastryId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ImplementerId = model.ImplementerId,
Id = model.Id,
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
var dateImplement = element.Element("DateImplement")!.Value;
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value),
PastryId = Convert.ToInt32(element.Element("PastryId")!.Value),
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value),
DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement),
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
PastryId = model.PastryId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
Id = model.Id;
}
public OrderViewModel GetViewModel => new()
{
ImplementerFIO = DataFileSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty,
PastryName = DataFileSingleton.GetInstance().Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty,
ClientFIO = DataFileSingleton.GetInstance().Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
PastryId = PastryId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ImplementerId = ImplementerId,
Id = Id,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("PastryId", PastryId),
new XElement("ClientId", ClientId),
new XElement("Count", Count),
new XElement("Sum", Sum.ToString()),
new XElement("Status", (int)Status),
new XElement("DateCreate", DateCreate),
new XElement("DateImplement", DateImplement),
new XElement("ImplementerId", ImplementerId)
);
}
}