69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
|
|
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<int, (IComponentModel, int)>? _buildComponents = null;
|
|
[NotMapped]
|
|
public Dictionary<int, (IComponentModel, int)> 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<BuildComponent> Components { get; set; } = new();
|
|
[ForeignKey("BuildId")]
|
|
public virtual List<Comment> Comments { get; set; } = new();
|
|
[NotMapped]
|
|
List<ICommentModel> 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
|
|
};
|
|
}
|
|
}
|