90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using ComputerHardwareStoreContracts.BindingModels;
|
|
using ComputerHardwareStoreContracts.ViewModels;
|
|
using ComputerHardwareStoreDataModels.Models;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
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; }
|
|
|
|
public int VendorId { get; set; }
|
|
[NotMapped]
|
|
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 virtual Vendor Vendor { get; private set; } = new();
|
|
|
|
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,
|
|
Components = context.Components
|
|
.Where(c => model.BuildComponents.ContainsKey(c.Id))
|
|
.Select(c => new BuildComponent()
|
|
{
|
|
BuildId = model.Id,
|
|
ComponentId = c.Id,
|
|
Component = c,
|
|
Count = model.BuildComponents[c.Id].Item2
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public void Update(BuildBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
|
|
Price = model.Price;
|
|
}
|
|
|
|
public BuildViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
Name = Name,
|
|
Price = Price,
|
|
VendorId = VendorId,
|
|
Comments = Comments.Select(c => c as ICommentModel).ToList(),
|
|
BuildComponents = BuildComponents
|
|
};
|
|
}
|
|
}
|