using ComputerShopContracts.BindingModels; using ComputerShopContracts.ViewModels; 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 string AssemblyName { get; private set; } = string.Empty; [Required] public double Cost { get; private set; } [Required] public int UserId { get; private set; } [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, AssemblyName = Model.AssemblyName, Cost = Model.Cost, UserId = Model.UserId, Components = Model.AssemblyComponents.Select(x => new AssemblyComponent { Component = Context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2 }).ToList(), }; } public void Update(AssemblyBindingModel Model) { if (!string.IsNullOrEmpty(Model.AssemblyName)) { AssemblyName = Model.AssemblyName; } Cost = Model.Cost; } public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model) { var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList(); if (AssemblyComponents != null && AssemblyComponents.Count > 0) { Context.AssemblyComponents .RemoveRange(AssemblyComponents .Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId))); Context.SaveChanges(); foreach (var ComponentToUpdate in AssemblyComponents) { ComponentToUpdate.Count = Model.AssemblyComponents[ComponentToUpdate.ComponentId].Item2; Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId); } Context.SaveChanges(); } var CurrentAssembly = Context.Assemblies.First(x => x.Id == Id); foreach (var AssemblyComponent in Model.AssemblyComponents) { Context.AssemblyComponents.Add(new AssemblyComponent { Assembly = CurrentAssembly, Component = Context.Components.First(x => x.Id == AssemblyComponent.Key), Count = AssemblyComponent.Value.Item2 }); Context.SaveChanges(); } _assemblyComponents = null; } public AssemblyViewModel ViewModel => new() { Id = Id, AssemblyName = AssemblyName, Cost = Cost, UserId = UserId, AssemblyComponents = AssemblyComponents, }; } }