using BankYouBankruptContracts.BindingModels; using BankYouBankruptContracts.ViewModels; using BankYouBankruptDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BankYouBankruptDatabaseImplement.Models { public class MoneyTransfer : IMoneyTransferModel { public int Id { get; set; } [Required] public int Sum { get; set; } public int? AccountSenderId { get; set; } [Required] public int AccountPayeeId { get; set; } [Required] public DateTime DateOperation { get; set; } public int? CreditingId { get; set; } public int CashierId { get; set; } public virtual Cashier Cashier { get; set; } public virtual Account AccountSender { get; set; } public virtual Account AccountPayeer { get; set; } public static MoneyTransfer Create(BankYouBancruptDatabase context, MoneyTransferBindingModel model) { return new MoneyTransfer() { Id = model.Id, Sum = model.Sum, Cashier = context.Cashiers.First(x => x.Id == model.CashierId), AccountSender = model.AccountSenderId == null ? null : context.Accounts.First(x => x.Id == model.AccountSenderId), AccountPayeer = context.Accounts.First(x => x.Id == model.AccountPayeeId), AccountSenderId = model.AccountSenderId, AccountPayeeId = model.AccountPayeeId, DateOperation = model.DateOperation, CreditingId = model.CreditingId, CashierId = model.CashierId }; } public void Update(MoneyTransferBindingModel model) { Id = model.Id; DateOperation = model.DateOperation; } public MoneyTransferViewModel GetViewModel => new() { Id = Id, AccountPayeeId = AccountPayeeId, AccountSenderId = AccountSenderId, AccountPayeeNumber = AccountPayeer.AccountNumber, AccountSenderNumber = AccountSenderId == null ? null : AccountSender.AccountNumber, DateOperation = DateOperation, Sum = Sum, CreditingId = CreditingId, CashierId = CashierId, CashierSurname = Cashier.Surname }; } }