ComputerHardwareStore_YouAr.../ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs

58 lines
1.7 KiB
C#

using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Vendor : IVendorModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string Login { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
[ForeignKey("VendorId")]
public virtual List<Purchase> Purchases { get; set; } = new();
[ForeignKey("VendorId")]
public virtual List<Build> Builds { get; set; } = new();
public static Vendor? Create(ComputerHardwareStoreDBContext context, VendorBindingModel model)
{
if (model == null)
{
return null;
}
return new Vendor()
{
Id = model.Id,
Name = model.Name,
Login = model.Login,
Password = model.Password,
};
}
public void Update(VendorBindingModel model)
{
if (model == null)
{
return;
}
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password;
}
public VendorViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Login = Login,
Password = Password
};
}
}