コード例 #1
0
        public void Handle(DebitAccountCommand command)
        {
            var account = _repository.GetById <Account>(command.AccountId);

            account.Debit(command.Amount);
            _repository.Save(account);
        }
コード例 #2
0
        public static Transaction MapToTransaction(DebitAccountCommand command)
        {
            var transaction = MapToTransaction((TransactionCommand)command);

            transaction.Amount  = -command.DebitAmount;
            transaction.IsDebit = true;
            return(transaction);
        }
コード例 #3
0
ファイル: NewFile.cs プロジェクト: naidu-kjml/Messaging
        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);
        }
コード例 #4
0
        public ICommandResult Handle(CommandContext context, DebitAccountCommand command)
        {
            var account = context.GetById <BankAccount>(command.Id);

            account.DebitAccount(command.Amount);

            context.Finalize(account);

            return(CommandResult.Successful);
        }
コード例 #5
0
        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 }));
        }
コード例 #6
0
        public void Handle(DebitAccountCommand message)
        {
            Console.WriteLine("Debiting account message received");

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