PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopFileImplement/Models/Order.cs

130 lines
3.5 KiB
C#
Raw Normal View History

2024-04-07 10:14:11 +04:00
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Enums;
using GiftShopDataModels.Models;
2024-05-17 13:28:57 +04:00
using System.Runtime.Serialization;
2024-04-07 10:14:11 +04:00
using System.Xml.Linq;
namespace GiftShopFileImplement.Models
{
2024-05-17 13:28:57 +04:00
[DataContract]
public class Order : IOrderModel
2024-04-07 10:14:11 +04:00
{
2024-05-17 13:28:57 +04:00
[DataMember]
public int GiftId { get; private set; }
2024-04-07 10:14:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public int? ImplementerId { get; set; }
2024-05-15 15:07:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public int ClientId { get; private set; }
2024-05-15 15:07:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public string GiftName { get; private set; } = string.Empty;
2024-05-15 15:07:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public int Count { get; private set; }
2024-04-07 10:14:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public double Sum { get; private set; }
2024-04-07 10:14:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
2024-04-07 10:14:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public DateTime DateCreate { get; private set; } = DateTime.Now;
2024-04-07 10:14:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public DateTime? DateImplement { get; private set; }
2024-04-07 10:14:11 +04:00
2024-05-17 13:28:57 +04:00
[DataMember]
public int Id { get; private set; }
2024-04-07 10:14:11 +04:00
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
GiftId = model.GiftId,
2024-05-17 13:28:57 +04:00
GiftName = model.GiftName,
Count = model.Count,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
Sum = model.Sum,
2024-04-07 10:14:11 +04:00
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
var order = new Order()
2024-05-17 13:28:57 +04:00
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
GiftId = Convert.ToInt32(element.Element("GiftId")!.Value),
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value),
DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null),
};
2024-04-07 10:14:11 +04:00
DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl);
order.DateImplement = dateImpl;
2024-05-17 13:28:57 +04:00
if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status))
{
return null;
}
order.Status = status;
return order;
2024-04-07 10:14:11 +04:00
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
2024-05-17 13:28:57 +04:00
}
Status = model.Status;
DateImplement = model.DateImplement;
}
2024-04-07 10:14:11 +04:00
public OrderViewModel GetViewModel => new()
2024-05-17 13:28:57 +04:00
{
Id = Id,
GiftId = GiftId,
ClientId = ClientId,
ImplementerId = ImplementerId,
GiftName = GiftName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
2024-04-07 10:14:11 +04:00
public XElement GetXElement => new("Order",
2024-05-17 13:28:57 +04:00
new XAttribute("Id", Id),
new XElement("GiftName", GiftName),
new XElement("GiftId", GiftId.ToString()),
new XElement("ClientId", ClientId.ToString()),
new XElement("ImplementerId", ImplementerId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString()));
}
2024-04-07 10:14:11 +04:00
}