using Contracts.BindingModels; using Contracts.ViewModels; using DataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace DatabaseImplement.Models { public class Product : IProductModel { public int Id { get; set; } [Required] public string Name { get; set; } = string.Empty; [Required] public double Cost { get; set; } [Required] public int UserId { get; set; } private Dictionary? _detailProducts = null; [NotMapped] public Dictionary DetailProducts { get { if (_detailProducts == null) { _detailProducts = Details.ToDictionary(recDP => recDP.DetailId, recDP => (recDP.Detail as IDetailModel, recDP.Count)); } return _detailProducts; } } [ForeignKey("ProductId")] public virtual List Details { get; set; } = new(); public static Product? Create(ProductBindingModel model) { if (model == null) { return null; } return new Product { Id = model.Id, Name = model.Name, Cost = model.Cost, UserId = model.UserId, Details = model.DetailProducts.Select(x => new DetailProduct { //Detail = }).ToList() }; } public static Product Create(ProductViewModel model) { return new Product { Id = model.Id, Name = model.Name, Cost = model.Cost, UserId = model.UserId }; } public void Update(ProductBindingModel model) { if (model == null) return; Name = model.Name; Cost = model.Cost; } public ProductViewModel GetViewModel => new() { Id = Id, Name = Name, Cost = Cost, UserId = UserId }; } }