Ejemplo n.º 1
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity <AccountData>()
            .HasOne(x => x.User)
            .WithMany(x => x.Accounts)
            .HasForeignKey(x => x.UserId);

            modelBuilder.Entity <AccountEntryData>()
            .HasOne(x => x.Account)
            .WithMany(x => x.Entries)
            .HasForeignKey(x => x.AccountId);

            modelBuilder.Entity <AccountTransactionData>()
            .HasOne <AccountEntryData>()
            .WithOne()
            .HasForeignKey <AccountTransactionData>(x => x.DebitEntryId);

            modelBuilder.Entity <AccountTransactionData>()
            .HasOne <AccountEntryData>()
            .WithOne()
            .HasForeignKey <AccountTransactionData>(x => x.CreditEntryId);

            modelBuilder.Entity <UserData>().HasData(new UserData()
            {
                Id = SystemAccount.SystemUserId, Email = "*****@*****.**", FirstName = "SYSTEM", LastName = "SYSTEM"
            });

            modelBuilder.Entity <AccountData>().HasData(
                new AccountData()
            {
                Id = SystemAccount.FromCurrency(AccountCurrency.UAH).Id, Currency = (int)AccountCurrency.UAH, UserId = SystemAccount.SystemUserId
            },
                new AccountData()
            {
                Id = SystemAccount.FromCurrency(AccountCurrency.EUR).Id, Currency = (int)AccountCurrency.EUR, UserId = SystemAccount.SystemUserId
            },
                new AccountData()
            {
                Id = SystemAccount.FromCurrency(AccountCurrency.USD).Id, Currency = (int)AccountCurrency.USD, UserId = SystemAccount.SystemUserId
            });
        }
        public async Task <BuyCurrencyResult> Handle(BuyCurrencyCommand request, CancellationToken cancellationToken)
        {
            var baseAccount = await _accountRepository.FindAsync(request.AccountId);

            var currencyAccount = await _accountRepository.FindAsync(request.CurrencyAccountId);

            var exchangeBaseAccount     = SystemAccount.FromCurrency(baseAccount.Currency);
            var exchangeCurrencyAccount = SystemAccount.FromCurrency(currencyAccount.Currency);

            var exchangeRate = await _currencyExchangeAdapter.GetExchangeRate(currencyAccount.Currency, baseAccount.Currency);

            var baseAmount = request.Amount * exchangeRate;

            var debitBaseAccount      = baseAccount.Transfer(exchangeBaseAccount, baseAmount.Value, $"Buy {request.Amount} {currencyAccount.Currency.ToString()} operation");
            var creditCurrencyAccount = exchangeCurrencyAccount.Transfer(currencyAccount, request.Amount, $"Buy {request.Amount} {currencyAccount.Currency.ToString()} operation");

            await _accountTransactionRepository.SaveAsync(debitBaseAccount);

            await _accountTransactionRepository.SaveAsync(creditCurrencyAccount);

            return(new BuyCurrencyResult());
        }