PIbd-23_Elatomtsev_L.K._Cou.../Bank/BankDatabaseImplement/Models/Client.cs

106 lines
3.6 KiB
C#

using BankDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using BankContracts.BindingModels;
using BankContracts.ViewModels;
namespace BankDatabaseImplement.Models
{
public class Client : IClientModel
{
public string Snils { get; set; } = string.Empty;
[Required]
public string ClientSurname { get; set; } = string.Empty;
[Required]
public string ClientName { get; set; } = string.Empty;
[Required]
public string ClientPatronymic { get; set; } = string.Empty;
[Required]
public string Phone { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
[Required]
public string PasswordHash { get; set; } = string.Empty;
[Required]
public int WorkerId { get; set; }
public virtual Worker? Worker { get; set; }
[ForeignKey("ClientId")]
public virtual List<ClientProgram> Programs { get; set; } = new();
private Dictionary<int, IProgramModel>?
_clientPrograms { get; set; } = null;
[ForeignKey("ClientId")]
public virtual List<ClientDeposit> Deposits { get; set; } = new();
private Dictionary<int, IDepositModel>?
_clientDeposits { get; set; } = null;
[NotMapped]
public Dictionary<int, IProgramModel> ClientPrograms
{
get
{
if (_clientPrograms == null)
{
_clientPrograms = Programs.ToDictionary(
x => x.ProgramId,
x => (x.Program as IProgramModel)
);
}
return _clientPrograms;
}
}
[NotMapped]
public Dictionary<int, IDepositModel> ClientDeposits
{
get
{
if (_clientDeposits == null)
{
_clientDeposits = Deposits.ToDictionary(
x => x.DepositId,
x => (x.Deposit as IDepositModel)
);
}
return _clientDeposits;
}
}
public static Client? Create(ClientBindingModel model)
{
if (model == null)
return null;
return new Client
{
Snils = model.Snils,
ClientSurname = model.ClientSurname,
ClientName = model.ClientName,
ClientPatronymic = model.ClientPatronymic,
Phone = model.Phone,
Email = model.Email ?? string.Empty,
PasswordHash = model.PasswordHash,
WorkerId = model.WorkerId,
};
}
public void Update(ClientBindingModel model)
{
if (model == null)
return;
Snils = model.Snils;
ClientSurname = model.ClientSurname;
ClientName = model.ClientName;
ClientPatronymic = model.ClientPatronymic;
Phone = model.Phone;
Email = model.Email ?? string.Empty;
PasswordHash = model.PasswordHash;
WorkerId = model.WorkerId;
}
public ClientViewModel GetViewModel => new()
{
Snils = Snils,
ClientSurname = ClientSurname,
ClientName = ClientName,
ClientPatronymic = ClientPatronymic,
Phone = Phone,
Email = Email ?? string.Empty,
PasswordHash = PasswordHash,
WorkerId = WorkerId,
};
}
}