public async Task HandlerAsync(FastSell command) { await _transactionService.FastSellTransactionAsync(command.FromCurrency, command.Amount, command.UserId); var price = await CryptoCompare.GetCryptoPriceInUsd(command.FromCurrency); await _historyService.AddAsync(OperationType.Sell, command.FromCurrency, command.Amount, price, command.UserId); }
public async Task FastSellTransactionAsync(string fromCurrnecy, decimal amount, Guid userId) { var wallet = await _walletRepository.GetByUserIdAsync(userId); var price = await CryptoCompare.GetCryptoPriceInUsd(fromCurrnecy); var fromWallet = wallet.Where(x => x.Currency == fromCurrnecy && (x.AmountOfMoney - amount) >= 0).SingleOrDefault(); var toWallet = wallet.Where(x => x.Currency == "USD").SingleOrDefault(); if (fromWallet == null || toWallet == null) { throw new Exception("Wallet is not exists"); } fromWallet.SetAmountOfMoney(fromWallet.AmountOfMoney - amount); toWallet.SetAmountOfMoney(toWallet.AmountOfMoney + amount * price); await _walletRepository.UpdateAsync(fromWallet); await _walletRepository.UpdateAsync(toWallet); }