public void Handle(DebitAccountCommand command)
        {
            var account = _repository.GetById <Account>(command.AccountId);

            account.Debit(command.Amount);
            _repository.Save(account);
        }
        public static Transaction MapToTransaction(DebitAccountCommand command)
        {
            var transaction = MapToTransaction((TransactionCommand)command);

            transaction.Amount  = -command.DebitAmount;
            transaction.IsDebit = true;
            return(transaction);
        }
Beispiel #3
0
        private static TransactionCommand ParseLineToCommand(ILogger log, string filename, int lineNumber, string line)
        {
            // id,acc_number,date_time,amount,merchant,authorization
            const int IdField            = 0;
            const int AccountNumberField = 1;
            const int DateTimeField      = 2;
            const int AmountField        = 3;
            const int MerchantField      = 4;
            const int AuthorizationField = 5;

            string[] fields = line.Split(',');

            if (!decimal.TryParse(fields[AmountField].Replace("$", ""), out decimal amount))
            {
                string errorMessage = $"Could not parse amount to decimal: line #{lineNumber} field #{AmountField} \"{fields[AmountField]}\"";
                log.LogError(errorMessage);
                throw new InvalidOperationException(errorMessage);
            }

            if (!DateTime.TryParse(fields[DateTimeField], out DateTime dateTime))
            {
                string errorMessage = $"Could not parse date_time to DateTime: line #{lineNumber} field #{DateTimeField} \"{fields[DateTimeField]}\"";
                log.LogError(errorMessage);
                throw new InvalidOperationException(errorMessage);
            }

            TransactionCommand command;

            if (amount >= 0)
            {
                // Credit
                command = new CreditAccountCommand
                {
                    CreditAmount = amount
                };
            }
            else
            {
                // Debit
                command = new DebitAccountCommand
                {
                    DebitAmount = Math.Abs(amount)
                };
            }

            command.Id                  = Guid.NewGuid();
            command.Filename            = filename;
            command.AccountNumber       = fields[AccountNumberField];
            command.AuthorizationCode   = fields[AuthorizationField];
            command.MerchantId          = fields[MerchantField];
            command.TransactionDateTime = dateTime;
            command.Amount              = amount;
            command.TransactionId       = fields[IdField];

            return(command);
        }
        public ICommandResult Handle(CommandContext context, DebitAccountCommand command)
        {
            var account = context.GetById <BankAccount>(command.Id);

            account.DebitAccount(command.Amount);

            context.Finalize(account);

            return(CommandResult.Successful);
        }
        public ActionResult Debit(DebitAccountCommand command)
        {
            try
            {
                Configuration.Instance().Bus.Handle(command);
            }
            catch (AccountLockedException)
            {
                return(RedirectToAction("Locked", new { accountId = command.AccountId }));
            }

            return(RedirectToAction("Debited", new { accountId = command.AccountId }));
        }
        public void Handle(DebitAccountCommand message)
        {
            Console.WriteLine("Debiting account message received");

            Host.GetCommandBus().Send(message);
        }