示例#1
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DepositInput" /> class.
 /// </summary>
 /// <param name="accountId">AccountId.</param>
 /// <param name="amount">Positive amount to deposit.</param>
 public DepositInput(
     AccountId accountId,
     PositiveMoney amount)
 {
     this.AccountId = accountId;
     this.Amount    = amount;
 }
示例#2
0
 public WithdrawInput(
     AccountId accountId,
     PositiveMoney amount)
 {
     AccountId = accountId;
     Amount    = amount;
 }
示例#3
0
        /// <summary>
        ///     ///     Executes the Use Case.
        /// </summary>
        /// <param name="input">Input Message.</param>
        /// <returns>Task.</returns>
        public async Task Execute(DepositInput input)
        {
            if (input is null)
            {
                this._depositOutputPort
                .WriteError(Messages.InputIsNull);
                return;
            }

            IAccount account = await this._accountRepository
                               .GetAccount(input.AccountId)
                               .ConfigureAwait(false);

            if (account is null)
            {
                this._depositOutputPort
                .NotFound(Messages.AccountDoesNotExist);
                return;
            }

            PositiveMoney amountConverted = await this._currencyExchange
                                            .ConvertToUSD(input.Amount)
                                            .ConfigureAwait(false);

            ICredit credit = await this._accountService
                             .Deposit(account, amountConverted)
                             .ConfigureAwait(false);

            await this._unitOfWork
            .Save()
            .ConfigureAwait(false);

            this.BuildOutput(credit, account);
        }
示例#4
0
 public Credit(CreditId id, AccountId accountId, PositiveMoney amount, DateTime transactionDate)
 {
     this.Id              = id;
     this.AccountId       = accountId;
     this.Amount          = amount;
     this.TransactionDate = transactionDate;
 }
示例#5
0
 public RegisterInput(
     SSN ssn,
     PositiveMoney initialAmount)
 {
     SSN           = ssn;
     InitialAmount = initialAmount;
 }
        private async Task WithdrawInternal(AccountId accountId, PositiveMoney withdrawAmount)
        {
            string externalUserId = this._userService
                                    .GetCurrentUserId();

            IAccount account = await this._accountRepository
                               .Find(accountId, externalUserId)
                               .ConfigureAwait(false);

            if (account is Account withdrawAccount)
            {
                PositiveMoney localCurrencyAmount =
                    await this._currencyExchange
                    .Convert(withdrawAmount, withdrawAccount.Currency)
                    .ConfigureAwait(false);

                Debit debit = this._accountFactory
                              .NewDebit(withdrawAccount, localCurrencyAmount, DateTime.Now);

                if (withdrawAccount.GetCurrentBalance().Amount - debit.Amount.Amount < 0)
                {
                    this._outputPort?.OutOfFunds();
                    return;
                }

                await this.Withdraw(withdrawAccount, debit)
                .ConfigureAwait(false);

                this._outputPort?.Ok(debit, withdrawAccount);
                return;
            }

            this._outputPort?.NotFound();
        }
        private async Task DepositInternal(AccountId accountId, PositiveMoney amount)
        {
            IAccount account = await this._accountRepository
                               .GetAccount(accountId)
                               .ConfigureAwait(false);

            if (account is Account depositAccount)
            {
                PositiveMoney amountInAccountCurrency =
                    await this._currencyExchange
                    .Convert(amount, depositAccount.Currency)
                    .ConfigureAwait(false);

                Credit credit = this._accountFactory
                                .NewCredit(depositAccount, amountInAccountCurrency, DateTime.Now);

                await this.Deposit(depositAccount, credit)
                .ConfigureAwait(false);

                this._outputPort?.Ok(credit, depositAccount);
                return;
            }

            this._outputPort?.NotFound();
        }
示例#8
0
        public ICredit Deposit(IEntityFactory entityFactory, PositiveMoney amountToDeposit)
        {
            var credit = entityFactory.NewCredit(this, amountToDeposit, DateTime.UtcNow);

            Credits.Add(credit);
            return(credit);
        }
 public void PositiveMoney_NonPositiveAmountRejected_ErrorThrown(Decimal moneyValue)
 {
     Should.Throw <ArgumentOutOfRangeException>(() =>
     {
         PositiveMoney.Create(Money.Create(moneyValue));
     });
 }
示例#10
0
        public void PositiveMoney_CanBeCreated_IsCreated(Decimal moneyValue)
        {
            PositiveMoney money = PositiveMoney.Create(Money.Create(moneyValue));

            money.ShouldNotBeNull();
            money.Value.ShouldBe(moneyValue);
        }
        public async Task <ICredit> Deposit(IAccount account, PositiveMoney amount)
        {
            var credit = account.Deposit(_accountFactory, amount);
            await _accountRepository.Update(account, credit);

            return(credit);
        }
        public async Task <IDebit> Withdraw(IAccount account, PositiveMoney amount)
        {
            var debit = account.Withdraw(_accountFactory, amount);
            await _accountRepository.Update(account, debit);

            return(debit);
        }
        public async Task <IAccount> OpenCheckingAccount(CustomerId customerId, PositiveMoney amount)
        {
            var account = _accountFactory.NewAccount(customerId);
            var credit  = account.Deposit(_accountFactory, amount);
            await _accountRepository.Add(account, credit);

            return(account);
        }
示例#14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Transaction"/> class.
 /// </summary>
 /// <param name="description">Text description.</param>
 /// <param name="amount">Amount.</param>
 /// <param name="transactionDate">Transaction Date.</param>
 public Transaction(
     string description,
     PositiveMoney amount,
     DateTime transactionDate)
 {
     this.Description     = description;
     this.Amount          = amount;
     this.TransactionDate = transactionDate;
 }
示例#15
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="TransferInput" /> class.
 /// </summary>
 /// <param name="originAccountId">Origin Account Id.</param>
 /// <param name="destinationAccountId">Destination Account Id.</param>
 /// <param name="amount">Positive amount.</param>
 public TransferInput(
     AccountId originAccountId,
     AccountId destinationAccountId,
     PositiveMoney amount)
 {
     this.OriginAccountId      = originAccountId;
     this.DestinationAccountId = destinationAccountId;
     this.Amount = amount;
 }
示例#16
0
 public Debit(
     IAccount account,
     PositiveMoney amountToWithdraw,
     DateTime transactionDate)
 {
     AccountId       = account.Id;
     Amount          = amountToWithdraw;
     TransactionDate = transactionDate;
 }
示例#17
0
 public Credit(
     IAccount account,
     PositiveMoney amountToDeposit,
     DateTime transactionDate)
 {
     AccountId       = account.Id;
     Amount          = amountToDeposit;
     TransactionDate = transactionDate;
 }
示例#18
0
        public static Money Neg(PositiveMoney money)
        {
            var amount = Convert.ToDecimal(money.amount);
            var temp   = -1 * amount;
            var result = new Money {
                currency = money.currency, amount = temp, amountSpecified = true
            };

            return(result);
        }
示例#19
0
        /// <summary>
        ///     Open Checking Account.
        /// </summary>
        /// <param name="customerId">Customer Id.</param>
        /// <param name="amount">Amount.</param>
        /// <returns>IAccount created.</returns>
        public async Task <IAccount> OpenCheckingAccount(CustomerId customerId, PositiveMoney amount)
        {
            IAccount account = this._accountFactory.NewAccount(customerId);
            ICredit  credit  = account.Deposit(this._accountFactory, amount);

            await this._accountRepository.Add(account, credit)
            .ConfigureAwait(false);

            return(account);
        }
示例#20
0
        public WithdrawInput(Guid accountId, PositiveMoney amount)
        {
            if (accountId == Guid.Empty)
            {
                throw new InputValidationException($"{nameof(accountId)} cannot be empty.");
            }

            AccountId = accountId;
            Amount    = amount;
        }
示例#21
0
        public PositiveMoney GetTotal()
        {
            PositiveMoney total = new PositiveMoney(0);

            foreach (ICredit credit in _credits)
            {
                total = credit.Sum(total);
            }

            return(total);
        }
示例#22
0
        public static Money GetMoney(PositiveMoney positiveMoney)
        {
            var currency = new Currency {
                Value = positiveMoney.currency.Value
            };
            var money = new Money {
                currency = currency, amount = positiveMoney.amount, amountSpecified = true
            };

            return(money);
        }
示例#23
0
        public static PositiveMoney GetPositiveMoney(Money amount)
        {
            var currency = new Currency {
                Value = amount.currency.Value
            };
            var money = new PositiveMoney {
                currency = currency, amount = System.Math.Abs(amount.amount), amountSpecified = true
            };

            return(money);
        }
示例#24
0
        public static PositiveMoney GetPositiveMoney(decimal amount, string currencyAsString)
        {
            var currency = new Currency {
                Value = currencyAsString
            };
            var money = new PositiveMoney {
                currency = currency, amount = System.Math.Abs(amount), amountSpecified = true
            };

            return(money);
        }
示例#25
0
 public Debit(
     DebitId debitId,
     AccountId accountId,
     PositiveMoney amountToWithdraw,
     DateTime transactionDate)
 {
     this.Id              = debitId;
     this.AccountId       = accountId;
     this.Amount          = amountToWithdraw;
     this.TransactionDate = transactionDate;
 }
        public PositiveMoney GetTotal()
        {
            PositiveMoney total = new PositiveMoney(0);

            foreach (IDebit debit in _debits)
            {
                total = debit.Sum(total);
            }

            return(total);
        }
示例#27
0
        /// <inheritdoc />
        public ICredit Deposit(IAccountFactory entityFactory, PositiveMoney amountToDeposit)
        {
            if (entityFactory is null)
            {
                throw new ArgumentNullException(nameof(entityFactory));
            }

            var credit = entityFactory.NewCredit(this, amountToDeposit, DateTime.UtcNow);

            this.Credits.Add(credit);
            return(credit);
        }
示例#28
0
        public IDebit Withdraw(IEntityFactory entityFactory, PositiveMoney amountToWithdraw)
        {
            if (GetCurrentBalance().LessThan(amountToWithdraw))
            {
                throw new MoneyShouldBePositiveException("Account has not enough funds.");
            }

            var debit = entityFactory.NewDebit(this, amountToWithdraw, DateTime.UtcNow);

            Debits.Add(debit);
            return(debit);
        }
        public Task <PositiveMoney> Convert(PositiveMoney originalAmount, Currency destinationCurrency)
        {
            // hardcoded rates from https://www.xe.com/currency/usd-us-dollar

            decimal usdAmount         = this._usdRates[originalAmount.Currency] / originalAmount.Amount;
            decimal destinationAmount = this._usdRates[destinationCurrency] / usdAmount;

            return(Task.FromResult(
                       new PositiveMoney(
                           destinationAmount,
                           destinationCurrency)));
        }
示例#30
0
        /// <inheritdoc />
        public IDebit NewDebit(
            IAccount account,
            PositiveMoney amountToWithdraw,
            DateTime transactionDate)
        {
            if (account is null)
            {
                throw new ArgumentNullException(nameof(account));
            }

            return(new Debit(new DebitId(Guid.NewGuid()), account.Id, amountToWithdraw, transactionDate));
        }