Beispiel #1
0
        public virtual async Task <TransferMoneyResultDto> SaveTransferringMoney(TransferMoneyFullDto dto)
        {
            TransferMoneyResultDto resultDto = new TransferMoneyResultDto();

            using (IDbContextTransaction transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    Statement senderStatement = await _statementRepository.Add(new Statement
                    {
                        Account       = dto.sender,
                        StatementType = StatementType.Transfer,
                        Fee           = dto.transfer_fee,
                        Amount        = -dto.amount,
                        CreateAt      = dto.transfer_date
                    });

                    Statement payeeStatement = await _statementRepository.Add(new Statement
                    {
                        Account       = dto.payee,
                        StatementType = StatementType.Deposit,
                        Fee           = dto.transfer_fee,
                        Amount        = dto.amount,
                        CreateAt      = dto.transfer_date
                    });

                    Transaction trasactionResult = await Add(new Transaction
                    {
                        PayeeStatement  = payeeStatement,
                        SenderStatement = senderStatement,
                        TransferAt      = dto.transfer_date
                    });

                    await transaction.CommitAsync();

                    resultDto.success = true;
                }
                catch (Exception ex)
                {
                    throw new TransactionFailureException("transaction error", ex);
                }
            }

            return(resultDto);
        }
Beispiel #2
0
        public async Task <TransferMoneyResultDto> TransferMoney(TransferMoneyDto dto)
        {
            ValidationHelper.Validate(_transferMoneyValidator, dto);
            Account sender = await _accountRepository.GetByIBanNumber(dto.sender_iban_number);

            if (sender == null)
            {
                throw new AccountNotFoundException("sender was not found");
            }

            Account payee = await _accountRepository.GetByIBanNumber(dto.payee_iban_number);

            if (payee == null)
            {
                throw new AccountNotFoundException("payee was not found");
            }

            var statements = await _statementRepository.GetByAccountId(sender.Id);

            var totalBalance = statements.Sum(x => StatementHelper.CalculateActualAmount(x.Amount, x.Fee));

            if (totalBalance < dto.amount)
            {
                throw new MoneyNotEnoughException("not enough money to transfer");
            }

            TransferMoneyFullDto fullDto = new TransferMoneyFullDto
            {
                amount        = dto.amount,
                sender        = sender,
                payee         = payee,
                transfer_date = DateTime.Now,
                transfer_fee  = _appSettings.TransferFee
            };

            var dtoResult = await _transactionRepository.SaveTransferringMoney(fullDto);

            return(dtoResult);
        }