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; } [Required] public int UserID { get; set; } [Required] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; [Required] public PaymeantOption PaymeantOption { get; set; } = PaymeantOption.Неизвестно; [Required] public DateTime DateCreate { get; set; } = DateTime.Now; [Required] public DateTime? DateImplement { get; set; } [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, UserID=model.UserID, Sum = model.Sum, Status = model.Status, PaymeantOption = model.PaymeantOption, DateCreate = model.DateCreate, Products = model.ProductList.Select(x => new OrderProducts { _product = context.Products.First(y => y.ID == x.Key), Count = x.Value.Item2 }).ToList() }; } public void Update(OrderBindingModel model) { if (model == null) { return; } ID = model.ID; UserID = model.UserID; Sum = model.Sum; PaymeantOption = model.PaymeantOption; Status = model.Status; DateCreate = model.DateCreate; if (model.DateImplement != null) { DateImplement = model.DateImplement; } //todo ProductList } public OrderViewModel GetViewModel => new() { ID = ID, UserID = UserID, Sum = Sum, PaymeantOption = PaymeantOption, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, ProductList = ProductList }; } }