ComputerHardwareStore_YouAr.../ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs

57 lines
1.7 KiB
C#
Raw Normal View History

2024-04-29 21:02:20 +04:00
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
2024-04-30 15:57:30 +04:00
[Required]
2024-05-30 07:44:40 +04:00
public virtual StoreKeeper? StoreKeeper { get; set; }
2024-04-30 20:06:35 +04:00
[NotMapped]
2024-05-30 07:44:40 +04:00
public int StoreKeeperId { get; set; }
2024-04-30 15:57:30 +04:00
2024-04-29 21:02:20 +04:00
[ForeignKey("ComponentId")]
public virtual List<ProductComponent> ProductComponents { get; set; } = new();
[ForeignKey("ComponentId")]
public virtual List<BuildComponent> BuldComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
2024-04-29 21:02:20 +04:00
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
Name = model.Name,
2024-04-30 15:57:30 +04:00
Cost = model.Cost,
2024-05-30 07:44:40 +04:00
StoreKeeperId = model.StoreKeeperId
2024-04-29 21:02:20 +04:00
};
}
public void Update (ComponentBindingModel model)
{
if (model == null)
{
return;
}
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Cost = Cost
};
}
}