93 lines
3.0 KiB
C#
Raw Normal View History

using BankContracts.BindingModels.Cashier;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models.ClientModels;
using BankDataModels.Enums;
using BankDataModels.Models.Cashier;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Models.CashierModels
{
// Класс, реализующий интерфейс модели счёта
public class Account : IAccountModel
{
public int Id { get; set; }
[Required]
public string AccountNumber { get; set; } = string.Empty;
[Required]
public int CashierId { get; set; }
[Required]
public int ClientId { get; set; }
// Для передачи ФИО клиента
public virtual Client Client { get; set; }
[Required]
public double Balance { get; set; }
[Required]
public DateTime DateOpen { get; set; } = DateTime.Now;
[Required]
public StatusAccount StatusAccount { get; set; } = StatusAccount.Открыт;
// Для реализации связи один-ко-многим со Снятием наличных
[ForeignKey("AccountId")]
public virtual List<CashWithdrawal> CashWithdrawals { get; set; } = new();
// Для реализации связи один-ко-многим с Переводом денег
[NotMapped]
[ForeignKey("AccountSenderId")]
public virtual List<MoneyTransfer> MoneyTransferSenders { get; set; } = new();
[NotMapped]
[ForeignKey("AccountPayeeId")]
public virtual List<MoneyTransfer> MoneyTransferPayees { get; set; } = new();
// Для реализации связи один-ко-многим с Картами
[ForeignKey("AccountId")]
public virtual List<Card> Cards { get; set; } = new();
public static Account Create(BankDatabase context, AccountBindingModel model)
{
return new Account()
{
Id = model.Id,
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
Balance = model.Balance,
DateOpen = model.DateOpen,
CashierId = model.CashierId,
AccountNumber = model.AccountNumber,
StatusAccount = model.StatusAccount
};
}
public void Update(AccountBindingModel model)
{
Balance = model.Balance;
}
public AccountViewModel GetViewModel => new()
{
Id = Id,
CashierId = CashierId,
ClientId = ClientId,
Name = Client.Name,
Patronymic = Client.Patronymic,
AccountNumber = AccountNumber,
Balance = Balance,
DateOpen = DateOpen,
StatusAccount = StatusAccount
};
}
}