public async Task SendMoney(SendMoneyRequestDto sendMoneyDto)
        {
            var user = await _userRepository.GetAsync(sendMoneyDto.UserId);

            if (user == null)
            {
                throw new UserNotFoundException($"{sendMoneyDto.UserId} is not found.");
            }

            var userTo = await _userRepository.GetAsync(sendMoneyDto.UserIdTo);

            if (userTo == null)
            {
                throw new UserNotFoundException($"{sendMoneyDto.UserIdTo} is not found.");
            }

            var currency = await _currencyRepository.GetAsync(sendMoneyDto.CurrencyName);

            if (currency == null)
            {
                throw new CurrencyNotFoundException($"{sendMoneyDto.CurrencyName} is not found.");
            }

            user.SendMoney(userTo, new Money(sendMoneyDto.Amount, currency));

            await _userRepository.UpdateBalanceSync(user);

            await _userRepository.UpdateBalanceSync(userTo);
        }
示例#2
0
        public async Task When_SendMoney_And_NoCurrencyFound_Then_ShouldReturn_Exception()
        {
            var      fixture      = new Fixture();
            var      userId       = 0;
            var      sendMoneyDto = new SendMoneyRequestDto(0, null, 0, 0);
            var      user         = fixture.Build <User>().With(x => x.Balances, new List <Balance>()).Create();
            Currency currency     = null;

            try
            {
                _userRepositoryMock.Setup(x => x.GetAsync(userId)).Returns(Task.FromResult(user));
                _currencyRepository.Setup(x => x.GetAsync(sendMoneyDto.CurrencyName))
                .Returns(Task.FromResult(currency));

                var moneyExchangeService =
                    new MoneyExchangeAppService(_userRepositoryMock.Object, _currencyRepository.Object);

                await moneyExchangeService.SendMoney(sendMoneyDto);

                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.Pass();
            }
        }
        public async Task <IActionResult> ExchangeMoney1([FromBody] SendMoneyRequestDto sendMoneyRequestDto)
        {
            await _moneyExchangeAppService.SendMoney(sendMoneyRequestDto);

            return(Ok(1));
        }