using BankContracts.BindingModels; using BankContracts.ViewModels; using BankDataModels.Models; 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 { public class CurrencyPurchase : ICurrencyPurchaseModel { public int Id { get; set; } [Required] public float Amount { get; set; } [Required] public DateTime PurchaseDate { get; set; } = DateTime.Now; [Required] public int BankOperatorId { get; set; } public virtual BankOperator BankOperator { get; set; } = new(); public int CurrencyId { get; set; } public virtual Currency Currency { get; set; } = new(); public static CurrencyPurchase? Create(BankDatabase context, CurrencyPurchaseBindingModel model) { if (model == null) { return null; } return new CurrencyPurchase() { Id = model.Id, Amount = model.Amount, PurchaseDate = model.PurchaseDate, BankOperatorId = model.BankOperatorId, BankOperator = context.BankOperators.First(x => x.Id == model.BankOperatorId), CurrencyId = model.CurrencyId, Currency = context.Currencies.First(x => x.Id == model.CurrencyId), }; } public static CurrencyPurchase? Create(BankDatabase context, CurrencyPurchaseViewModel model) { return new CurrencyPurchase() { Id = model.Id, Amount = model.Amount, PurchaseDate = model.PurchaseDate, BankOperatorId = model.BankOperatorId, BankOperator = context.BankOperators.First(x => x.Id == model.BankOperatorId), CurrencyId = model.CurrencyId, Currency = context.Currencies.First(x => x.Id == model.CurrencyId), }; } public void Update(CurrencyPurchaseBindingModel? model) { if (model == null) { return; } PurchaseDate = model.PurchaseDate; } public CurrencyPurchaseViewModel GetViewModel => new() { Id = Id, Amount = Amount, PurchaseDate = PurchaseDate, BankOperatorId = BankOperatorId, BankOperatorName = BankOperator.LastName + " " + BankOperator.FirstName + " " + BankOperator.MiddleName, CurrencyId = CurrencyId, CurrencyName = Currency.Name, }; } }