Exemplo n.º 1
0
        public void DespositMoney(WalletId walletId, Money amount)
        {
            var wallet = _wallets.SingleOrDefault(w => w.WalletId == walletId);

            if (wallet == null)
            {
                throw new WalletNotFoundException($"Wallet {walletId} was not found.");
            }

            var e = wallet.Deposit(amount);

            RaiseEvent(e);
        }
Exemplo n.º 2
0
        public void WithdrawMoney(WalletId walletId, Money amount)
        {
            var wallet = _wallets.SingleOrDefault(w => w.WalletId == walletId);

            if (wallet == null)
            {
                throw new WalletNotFoundException($"Wallet {walletId} was not found.");
            }

            if (amount > wallet.Balance)
            {
                throw new InsufficientFundsException($"Insufficient funds to withdraw {amount}.");
            }

            var e = wallet.Withdraw(amount);

            RaiseEvent(e);
        }
Exemplo n.º 3
0
        public void TransferToWallet(WalletId sourceId, WalletId destinationId, Money amount)
        {
            var source      = _wallets.SingleOrDefault(w => w.WalletId == sourceId);
            var destination = _wallets.SingleOrDefault(w => w.WalletId == destinationId);

            if (source == null || destination == null)
            {
                throw new WalletNotFoundException("Could not find one of the specified wallets.");
            }

            if (source.Balance < amount)
            {
                throw new InsufficientFundsException("The source wallet does not have enough funds to transfer to the destination.");
            }

            // This *could* just call WithdrawnMoney and DepositMoney, but I want to separate them
            // in order to create an atomic transaction. If it errors before raising the events,
            // then the events will not persist.
            var withdrawn = source.Withdraw(amount);
            var deposited = destination.Deposit(amount);

            RaiseEvent(withdrawn);
            RaiseEvent(deposited);
        }