Пример #1
0
        private int Create(string accountName, string currency, User userEntity)
        {
            accountName.EnsureNotNullOrWhiteSpace(nameof(accountName));
            currency.EnsureNotNullOrWhiteSpace(nameof(currency));
            userEntity.EnsureNotNull("User");

            //TODO: should validate in Entity class, not this.
            accountName = accountName.Trim();
            if (accountName.Length < 10 && accountName.Length > 20)
            {
                throw new BusinessException("Account name is not valid.");
            }

            if (BankAccountRepository.ByAccountName(accountName) != null)
            {
                throw new BusinessException("Bank account name is taken.");
            }

            var currencyEntity = CurrencyService.Get(currency);

            currencyEntity.EnsureNotNull("Currency");

            var entity = new BankAccount
            {
                AccountName = accountName,
                Currency    = currencyEntity,
                User        = userEntity
            };

            BankAccountRepository.Create(entity);
            UnitOfWork.SaveChanges();
            return(entity.Id);
        }
Пример #2
0
            ) Transfer(string fromAccount, string toAccount, string currency, decimal money, string message)
        {
            if (money < 0)
            {
                throw new BusinessException("Money should be greater than 0.");
            }

            var nowUtc = DateTime.Now.ToUniversalTime();
            var sourceAccountEntity = BankAccountService.Get(fromAccount);
            var targetAccountEntity = BankAccountService.Get(toAccount);
            var currencyEntity      = CurrencyService.Get(currency);

            var exchangeRateFromSourceAccount = ExchangeRateHistoryService.GetExchangeRate(sourceAccountEntity.Currency, currencyEntity, nowUtc);
            var exchangeRateToTargetAccount   = ExchangeRateHistoryService.GetExchangeRate(currencyEntity, targetAccountEntity.Currency, nowUtc);
            var deltaFromSourceAccount        = money * exchangeRateFromSourceAccount;
            var deltaToTargetAccount          = money * exchangeRateToTargetAccount;

            if (deltaFromSourceAccount > sourceAccountEntity.Balance)
            {
                throw new BusinessException("Source account balance is not sufficient");
            }
            else if (deltaFromSourceAccount <= 0 || deltaToTargetAccount <= 0)
            {
                throw new BusinessException("Transfer delta is not valid.");
            }

            sourceAccountEntity.Balance -= deltaFromSourceAccount;
            targetAccountEntity.Balance += deltaToTargetAccount;

            var entity = new TransferMoneyTransaction
            {
                DateTimeUtc   = nowUtc,
                SourceAccount = sourceAccountEntity,
                Account       = targetAccountEntity,
                Currency      = currencyEntity,
                Money         = money,
                Message       = message
            };

            TransactionRepository.Create(entity);
            BankAccountRepository.Update(sourceAccountEntity);
            BankAccountRepository.Update(targetAccountEntity);

            UnitOfWork.SaveChanges();
            return(
                entity.Id,
                exchangeRateFromSourceAccount, deltaFromSourceAccount, sourceAccountEntity.Balance,
                exchangeRateToTargetAccount, deltaToTargetAccount, targetAccountEntity.Balance
                );
        }
Пример #3
0
        public (int transactionId, decimal exchangeRate, decimal delta, decimal newBalance) Withdraw(string accountName, string currency, decimal money, string message)
        {
            if (money < 0)
            {
                throw new BusinessException("Money should be greater than 0.");
            }

            var nowUtc         = DateTime.Now.ToUniversalTime();
            var accountEntity  = BankAccountService.Get(accountName);
            var currencyEntity = CurrencyService.Get(currency);

            var exchangeRate = ExchangeRateHistoryService.GetExchangeRate(currencyEntity, accountEntity.Currency, nowUtc);
            var delta        = money * exchangeRate;

            if (delta > accountEntity.Balance)
            {
                throw new BusinessException("Account balance is not sufficient");
            }
            else if (delta <= 0)
            {
                throw new BusinessException("Withdraw delta is not valid.");
            }

            accountEntity.Balance -= delta;

            var entity = new WithdrawMoneyTransaction
            {
                DateTimeUtc = nowUtc,
                Account     = accountEntity,
                Currency    = currencyEntity,
                Money       = money,
                Message     = message
            };

            TransactionRepository.Create(entity);
            BankAccountRepository.Update(accountEntity);

            UnitOfWork.SaveChanges();
            return(entity.Id, exchangeRate, delta, accountEntity.Balance);
        }
Пример #4
0
        public (int transactionId, decimal exchangeRate, decimal delta, decimal newBalance) Deposit(User bankOfficer, string account, string currency, decimal money, string message)
        {
            if (money < 0)
            {
                throw new BusinessException("Money should be greater than 0.");
            }

            var nowUtc         = DateTime.Now.ToUniversalTime();
            var accountEntity  = BankAccountService.Get(account);
            var currencyEntity = CurrencyService.Get(currency);

            var exchangeRate = ExchangeRateHistoryService.GetExchangeRate(currencyEntity, accountEntity.Currency, nowUtc);
            var delta        = money * exchangeRate;

            if (delta <= 0)
            {
                throw new BusinessException("Deposit delta is not valid.");
            }

            accountEntity.Balance += delta;

            UnitOfWork.AttachEntity(bankOfficer).State = EntityState.Unchanged;

            var entity = new DepositMoneyTransaction
            {
                DateTimeUtc = nowUtc,
                Account     = accountEntity,
                Currency    = currencyEntity,
                Money       = money,
                Message     = message,
                BankOfficer = bankOfficer
            };

            TransactionRepository.Create(entity);
            BankAccountRepository.Update(accountEntity);

            UnitOfWork.SaveChanges();
            return(entity.Id, exchangeRate, delta, accountEntity.Balance);
        }
Пример #5
0
 public decimal GetExchangeRate(string fromCurrency, string toCurrency, DateTime time)
 => fromCurrency.IsOrdinalEqual(toCurrency, true) ? 1m : GetExchangeRate(CurrencyService.Get(fromCurrency), CurrencyService.Get(toCurrency), time);