using ComputerHardwareStoreContracts.BindingModels; using ComputerHardwareStoreContracts.ViewModels; using ComputerHardwareStoreDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Runtime.InteropServices; namespace ComputerHardwareStoreDatabaseImplement.Models { public class Build : IBuildModel { public int Id { get; set; } [Required] public string Name { get; set; } = string.Empty; [Required] public double Price { get; set; } [Required] public int VendorId { get; set; } public virtual Vendor Vendor { get; private set; } = new(); private Dictionary? _buildComponents = null; [NotMapped] public Dictionary BuildComponents { get { if (_buildComponents == null) { _buildComponents = Components .ToDictionary(bc => bc.ComponentId, bc => (bc.Component as IComponentModel, bc.Count)); } return _buildComponents; } } [ForeignKey("BuildId")] public virtual List Components { get; set; } = new(); [ForeignKey("BuildId")] public virtual List Comments { get; set; } = new(); [NotMapped] List IBuildModel.Comments => Comments.Select(c => c as ICommentModel).ToList(); public static Build? Create(ComputerHardwareStoreDBContext context, BuildBindingModel model) { if (model == null) { return null; } return new() { Id = model.Id, Name = model.Name, Price = model.Price, VendorId = model.VendorId, }; } public BuildViewModel GetViewModel() => new() { Id = Id, Name = Name, Price = Price, VendorId = VendorId, Comments = Comments.Select(c => c as ICommentModel).ToList(), BuildComponents = BuildComponents }; } }