// The buyer deposited the coin into the coin acceptor (deposit) public async Task AddAmountDepositAsync(CoinDTO coin, Guid userId) { if (coin == null) { throw new NullReferenceException("Coin is null"); } using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { // Increase the amount of the user's deposit await _userDepositRepository.AddAmountDepositAsync((int)coin.TypeCoin, userId); // we take a coin from the user await _purseRepository.RemoveCoinAsync(userId, coin.TypeCoin); // TODO: To verify a transaction, you need to uncomment this line -> // throw new Exception("To verify a transaction, you need to uncomment this line."); // give a coin to VM await _vendingMachineService.AddCoinAsync(coin.TypeCoin); // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails scope.Complete(); } catch (System.Exception) { // TODO: Handle failure throw new ApplicationException("The problem of adding a user deposit amount!"); } }; }