Exemplo n.º 1
0
        public async Task <OperationResult> Convert(int userId, decimal amount, string fromCurrency,
                                                    string toCurrency)
        {
            var fromWallet = await walletManager.GetUserWallet(userId, fromCurrency);

            if (fromWallet == null)
            {
                return(new OperationResult(false, Constants.WalletNotExistsMsg));
            }

            if (fromWallet.Amount < amount)
            {
                return(new OperationResult(false, Constants.WalletHasNotEnoughMoneyMsg));
            }

            var toWallet = await walletManager.GetUserWallet(userId, toCurrency)
                           ?? await walletManager.CreateUserWallet(userId, toCurrency, 0);

            var currencyRate = await currencyManager.GetCurrencyRate(fromCurrency, toCurrency);

            if (!currencyRate.HasValue)
            {
                return(new OperationResult(false, Constants.CurrencyNotSupported));
            }

            fromWallet.Amount -= amount;
            toWallet.Amount   += Decimal.Round(amount / currencyRate.Value, 2, MidpointRounding.ToNegativeInfinity);

            await walletManager.UpdateUserWallets(fromWallet, toWallet);

            return(new OperationResult(true, String.Empty));
        }