PIbd-23_Ivanov_V.N._Pizzeria/Pizzeria/PizzeriaFileImplement/Models/Order.cs

116 lines
3.8 KiB
C#
Raw Normal View History

2024-02-17 17:52:28 +04:00
using PizzeriaDataModels.Enums;
using PizzeriaDataModels.Models;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using System.Xml.Linq;
2024-04-03 17:52:29 +04:00
using System.Runtime.Serialization;
2024-02-17 17:52:28 +04:00
namespace PizzeriaFileImplement.Models
{
2024-04-03 17:52:29 +04:00
[DataContract]
2024-02-17 17:52:28 +04:00
public class Order : IOrderModel
{
2024-04-03 17:52:29 +04:00
[DataMember]
2024-02-17 17:52:28 +04:00
public int Id { get; private set; }
2024-04-03 17:52:29 +04:00
[DataMember]
2024-03-22 10:09:56 +04:00
public int ClientId { get; private set; }
2024-04-03 17:52:29 +04:00
[DataMember]
2024-04-02 17:02:41 +04:00
public int? ImplementerId { get; set; }
2024-04-03 17:52:29 +04:00
[DataMember]
2024-03-22 10:09:56 +04:00
public int PizzaId { get; private set; }
2024-04-03 17:52:29 +04:00
[DataMember]
2024-02-17 17:52:28 +04:00
public int Count { get; private set; }
2024-04-03 17:52:29 +04:00
[DataMember]
2024-02-17 17:52:28 +04:00
public double Sum { get; private set; }
2024-04-03 17:52:29 +04:00
[DataMember]
2024-02-17 17:52:28 +04:00
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
2024-04-03 17:52:29 +04:00
[DataMember]
2024-02-17 17:52:28 +04:00
public DateTime DateCreate { get; private set; } = DateTime.Now;
2024-04-03 17:52:29 +04:00
[DataMember]
2024-02-17 17:52:28 +04:00
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
PizzaId = model.PizzaId,
2024-03-22 10:09:56 +04:00
ClientId = model.ClientId,
2024-04-02 17:02:41 +04:00
ImplementerId = model.ImplementerId,
2024-03-22 10:09:56 +04:00
Count = model.Count,
2024-02-17 17:52:28 +04:00
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
string dateImplement = element.Element("DateImplement")!.Value;
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value),
2024-03-22 10:09:56 +04:00
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
2024-04-02 17:02:41 +04:00
ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value),
2024-03-22 10:09:56 +04:00
Count = Convert.ToInt32(element.Element("Count")!.Value),
2024-02-17 17:52:28 +04:00
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = (dateImplement == "" || dateImplement is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateImplement)
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
PizzaId = PizzaId,
2024-03-22 10:09:56 +04:00
ClientId = ClientId,
2024-04-02 17:02:41 +04:00
ImplementerId = ImplementerId,
2024-03-22 10:09:56 +04:00
Count = Count,
2024-02-17 17:52:28 +04:00
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("PizzaId", PizzaId.ToString()),
2024-03-22 10:09:56 +04:00
new XElement("ClientId", ClientId.ToString()),
2024-04-02 17:02:41 +04:00
new XElement("ImplementerId", ImplementerId),
2024-03-22 10:09:56 +04:00
new XElement("Count", Count.ToString()),
2024-02-17 17:52:28 +04:00
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString()));
}
}