PIbd-22_Chernyshev_Shabunov.../ComputerShopDatabaseImplement/Models/Assembly.cs

66 lines
1.6 KiB
C#

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<AssemblyComponent> Components { get; set; } = new();
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> 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()
}
}