ComputerHardwareStore_YouAr.../ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs

84 lines
2.8 KiB
C#
Raw Normal View History

2024-04-29 21:02:20 +04:00
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Enums;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
2024-04-30 17:05:42 +04:00
using System.ComponentModel.DataAnnotations.Schema;
2024-04-29 21:02:20 +04:00
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }
2024-04-30 17:05:42 +04:00
private Dictionary<int, (IProductModel, int)>? _orderProducts = null;
[NotMapped]
public Dictionary<int, (IProductModel, int)> OrderProducts
{
get
{
if (_orderProducts == null)
{
_orderProducts = Products
.ToDictionary(
op => op.ProductId,
op => (op.Product as IProductModel, op.Count));
}
return _orderProducts;
}
}
[ForeignKey("OrderId")]
public virtual List<OrderProduct> Products { get; set; } = new();
2024-04-29 21:02:20 +04:00
public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
2024-04-30 17:05:42 +04:00
Products = model.OrderProducts
.Select(op => new OrderProduct()
{
OrderId = model.Id,
ProductId = op.Key,
Count = op.Value.Item2
})
.ToList()
2024-04-29 21:02:20 +04:00
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
2024-04-30 15:57:30 +04:00
public Dictionary<int, (IProductModel, int)> OrderProduct => throw new NotImplementedException();
2024-04-29 21:02:20 +04:00
}
2024-04-30 17:05:42 +04:00
}