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; [Required] public Dictionary ProductList { get; set; } = new(); [NotMapped] public Dictionary _productList { get { if (ProductList == null) { ProductList = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC._product as IProductModel, recPC.Count)); } return _productList; } } public virtual List 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; } 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(); //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() */ }; } }