Exemplo n.º 1
0
        private void Handle(CommitTransaction cmd)
        {
            var sender = Sender;

            if (cmd.AccountNumber != _state.AccountNumber)
            {
                throw new NotFoundException("Invalid Account Number");
            }

            if (!_state.OutstandingTransaction.ContainsKey(cmd.TransactionId))
            {
                // command idempotent, just confirm that it is done
                Respond(sender, "Commited");
                return;
            }

            var amount = _state.OutstandingTransaction[cmd.TransactionId];
            var commit = new AccountDebitCommited
            {
                TransactionId = cmd.TransactionId,
                Amount        = amount,
                EndedDate     = DateTime.UtcNow
            };

            Persist(commit, e =>
            {
                ApplyEvent(e);
                Respond(sender, "Commited", new[] { commit });
            });
        }
Exemplo n.º 2
0
 public Task <IHttpActionResult> CommitTransaction(string transactionId, [FromBody] CommitTransaction commit) =>
 Result(async() =>
 {
     var transaction = transactions[transactionId];
     var sha         = await transaction.Commit(commit.Message, commit.Author);
     transactions.Remove(transactionId);
     return(sha);
 });
Exemplo n.º 3
0
 private void Handle(ICommandContext commandContext, CommitTransaction command)
 {
     if (AccountNumber != command.AccountNumber)
     {
         throw new Exception("Wrong account number");
     }
     commandContext.Emit(
         new TransactionCommitted
     {
         AccountNumber = command.AccountNumber,
         CausationId   = command.CausationId,
         TransactionId = command.TransactionId
     });
 }
Exemplo n.º 4
0
        public TransactionSaga(IActorRef accountsRef, string transactionId)
        {
            _accountsRef = accountsRef;

            Receive <AccountDebited>(msg =>
            {
                _log.Info("----------DEBITED---------");
                _fromAccount   = msg.From;
                _toAcount      = msg.To;
                _amount        = msg.Amount;
                _transactionId = msg.TransactionId;

                var cmd = new Deposit
                {
                    Amount        = msg.Amount,
                    From          = msg.From,
                    To            = msg.To,
                    TransactionId = msg.TransactionId
                };
                _accountsRef.Tell(new CommandEnvelope(msg.To, cmd));
            });

            Receive <AccountCredited>(msg =>
            {
                _log.Info("----------CREDITED---------");
                var cmd = new CommitTransaction
                {
                    AccountNumber = msg.From,
                    TransactionId = msg.TransactionId
                };
                _accountsRef.Tell(new CommandEnvelope(cmd.AccountNumber, cmd));
            });

            Receive <AccountDebitCommited>(msg =>
            {
                _log.Info("----------COMMITED---------");
                _log.Info($"Transaction {msg.TransactionId} successfully finished.");
                Self.Tell(PoisonPill.Instance);
            });

            Receive <AccountDebitRolledBack>(msg =>
            {
                _log.Info("----------ROLLBACK---------");
                _log.Info($"Transaction {msg.TransactionId} rolled back");
                Self.Tell(PoisonPill.Instance);
            });
        }
Exemplo n.º 5
0
        public void Commit()
        {
            CommitTransaction ct = new CommitTransaction(_records.Values.ToList(), _connection.Database);

            ct.UseTransactionLog = UseTransactionLog;
            var result = _connection.ExecuteOperation(ct);
            Dictionary <ORID, ORID> mapping = result.GetField <Dictionary <ORID, ORID> >("CreatedRecordMapping");

            var survivingRecords = _records.Values.Where(x => x.RecordType != RecordType.Delete).ToList();

            foreach (var kvp in mapping)
            {
                var record = _records[kvp.Key];
                record.ORID = kvp.Value;
                _records.Add(record.ORID, record);
                if (record.Document != null)
                {
                    _connection.Database.ClientCache.Add(kvp.Value, record.Document);
                }
            }

            var versions = result.GetField <Dictionary <ORID, int> >("UpdatedRecordVersions");

            foreach (var kvp in versions)
            {
                var record = _records[kvp.Key];
                record.Version = kvp.Value;
            }

            foreach (var record in survivingRecords)
            {
                if (record.Object != null)
                {
                    ORIDUpdaterBase.GetInstanceFor(record.Object.GetType()).UpdateORIDs(record.Object, mapping);
                }
                else
                {
                    ORIDUpdaterBase.GetInstanceFor(record.Document.GetType()).UpdateORIDs(record.Document, mapping);
                }
            }

            Reset();
        }
Exemplo n.º 6
0
        public TransactionSaga(IActorRef accountsRef, string transactionId)
        {
            _accountsRef = accountsRef;

            Receive <AccountDebited>(msg =>
            {
                _log.Info("----------DEBITED---------");
                _fromAccount   = msg.AccuontNumber;
                _toAcount      = msg.ToAccount;
                _amount        = msg.Amount;
                _transactionId = msg.TransactionId;

                var cmd = new Deposit
                {
                    Amount        = msg.Amount,
                    FromAccount   = msg.AccuontNumber,
                    ToAccount     = msg.ToAccount,
                    TransactionId = msg.TransactionId
                };
                _accountsRef.Tell(cmd);
            });

            Receive <AccountCredited>(msg =>
            {
                _log.Info("----------CREDITED---------");
                var cmd = new CommitTransaction
                {
                    AccountNumber = msg.FromAccount,
                    TransactionId = msg.TransactionId
                };
                _accountsRef.Tell(cmd);
            });

            Receive <TransactionCommitted>(msg =>
            {
                _log.Info("----------COMMITED---------");
                _log.Info($"Transaction {msg.TransactionId} successfully finished.");
                Self.Tell(PoisonPill.Instance);
            });
        }
Exemplo n.º 7
0
 /// <summary>
 /// Метод осуществляет перевод денег му счетами
 /// </summary>
 /// <param name="accountFrom"></param>
 /// <param name="accountTo"></param>
 /// <param name="sum"></param>
 public void BalanceTransferTo(Account accountTo, double sum)
 {
     this.Balance      -= sum;
     accountTo.Balance += sum;
     CommitTransaction?.Invoke(this, accountTo, sum);
 }
Exemplo n.º 8
0
 internal static void OnCommitTransaction(object sender, EventArgs e)
 {
     CommitTransaction?.Invoke(sender, e);
 }