CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs
2024-05-25 17:47:14 +04:00

98 lines
3.0 KiB
C#

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataModels.Enums;
using ElectronicsShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Models
{
public class Order : IOrderModel
{
public int ID { get; set; }
[Required]
public double Sum { get; set; }
[ForeignKey("ClientID")]
public int ClientID { get; set; }
[ForeignKey("EmployeeID")]
public int? EmployeeID { get; set; }
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public Dictionary<int, (IProductModel, int)>? _productList = null;
[NotMapped]
public Dictionary<int, (IProductModel, int)> ProductList
{
get {
if (_productList == null) {
_productList = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC._product as IProductModel, recPC.Count));
}
return _productList;
}
}
public virtual List<OrderProduct> Products { get; set; } = new();
public static Order? Create(Database context ,OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
ID = model.ID,
ClientID=model.ClientID,
Sum = model.Sum,
EmployeeID=model.EmployeeID,
DateCreate = model.DateCreate,
Products = model.ProductList.Select(x => new OrderProduct
{
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(Database context,OrderBindingModel model)
{
if (model == null)
{
return;
}
ClientID = model.ClientID;
Sum = model.Sum;
EmployeeID = model.EmployeeID;
DateCreate = model.DateCreate;
Products = model.ProductList.Select(x => new OrderProduct
{
_product = context.Products.First(y => y.ID == x.Key),
Count = x.Value.Item2
}).ToList();
//todo ProductList
}
public OrderViewModel GetViewModel => new()
{
ID = ID,
ClientID = ClientID,
Sum = Sum,
EmployeeID = EmployeeID,
DateCreate = DateCreate,
/*
ProductList = ProductList.Select(x => new OrderProducts
{
/_product = context.Products.First(y => y.ID == x.Key)
Count = x.Value.Item2
}).ToList()
*/
};
}
}