using ComputerShopContracts.BindingModels; using ComputerShopDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ComputerShopDatabaseImplement.Models { public class Assembly : IAssemblyModel { public int Id { get; private set; } [Required] public int UserId { get; private set; } [Required] public string AssemblyName { get; private set; } = string.Empty; [Required] public double Cost { get; private set; } [Required] public string Category { get; private set; } = string.Empty; [ForeignKey("ComponentId")] public virtual List Components { get; set; } = new(); private Dictionary? _assemblyComponents; [NotMapped] public Dictionary AssemblyComponents { get { if (_assemblyComponents == null) { _assemblyComponents = Components.ToDictionary( AsmComp => AsmComp.ComponentId, AsmComp => (AsmComp.Component as IComponentModel, AsmComp.Count) ); } return _assemblyComponents; } } public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model) { return new() { Id = Model.Id, UserId = Model.UserId, AssemblyName = Model.AssemblyName, Cost = Model.Cost, Category = Model.Category, Components = Model.AssemblyComponents.Select(x => new AssemblyComponent { Component = Context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2 }).ToList(), }; } // TODO: Update(), ViewModel, UpdateComponents() } }