public void Handler(ApplyInterestCommand command)
        {
            if (command.Amount > 0)
            {
                var account  = _context.Accounts.SingleOrDefault(x => x.AccountId == command.AccountId);
                var dateDiff = Convert.ToDecimal((DateTime.Now - command.PreviousApplication).TotalDays);

                int frequency = 365;

                if (account.Frequency.ToLower() == "weekly")
                {
                    frequency = 52;
                }
                else if (account.Frequency.ToLower() == "monthly")
                {
                    frequency = 12;
                }

                var percent  = command.Amount / 100;
                var interest = Convert.ToDouble(1 + (percent / frequency));

                var balance = Math.Abs(account.Balance * Convert.ToDecimal(Math.Pow(interest, frequency)));
                _context.SaveChanges();

                var transaction = new CreateTransactionCommand {
                    Account = account, Amount = balance, Operation = "Compound interest"
                };
                var handler = new CreateTransactionHandler(transaction);
            }
            else
            {
                throw new NegativeAmountException();
            }
        }
Ejemplo n.º 2
0
        public async Task Handler(WithdrawCommand command)
        {
            var account = _context.Accounts.SingleOrDefault(x => x.AccountId == command.AccountId);

            account.Balance -= command.Amount;

            var transaction = new CreateTransactionCommand()
            {
                Operation = "Withdraw in cash",
                Account   = account,
                Amount    = command.Amount
            };

            var handler = new CreateTransactionHandler(transaction);

            await _context.SaveChangesAsync();
        }