예제 #1
0
        public async Task <IActionResult> Transfer(
            Guid userId,
            [FromBody][Required] WalletTransferDto wallet)
        {
            await _walletService.Transfer(userId, wallet);

            return(NoContent());
        }
예제 #2
0
        public async Task Transfer(Guid userId, WalletTransferDto walletDto)
        {
            await CheckUserExists(userId);

            Wallet walletFrom = await CreateWalletIfNotExist(userId, walletDto.CurrencyFrom);

            if (!TrySubstractAmount(walletFrom.Total, walletDto.Amount, out decimal newTotalFrom, out string error))
            {
                throw new InvalidRequestException(error);
            }

            walletFrom.Total = newTotalFrom;

            var converter = new CurrencyConverter(_logger, _currencyRatesProvider);

            if (!converter.TryConvert(
                    walletDto.CurrencyFrom,
                    walletDto.CurrencyTo,
                    walletDto.Amount,
                    out decimal amountTo,
                    out error))
            {
                throw new InvalidRequestException(error);
            }

            Wallet walletTo = await CreateWalletIfNotExist(userId, walletDto.CurrencyTo);

            if (!TryAddAmount(walletTo.Total, amountTo, out decimal newTotalTo, out error))
            {
                throw new InvalidRequestException(error);
            }

            walletTo.Total = newTotalTo;

            await _db.SaveChangesAsync();
        }