public void Handle(DepositEvent domainEvent)
        {
            var account = dbContext.Accounts.SingleOrDefault(x => x.Id == domainEvent.AggregateRootId);

            account.Balance += domainEvent.Value.Amount;
            dbContext.SaveChanges();
        }
Ejemplo n.º 2
0
        public IEnumerator Run(
            Client client,
            GameSession session,
            string moneyNamespaceName,
            int slot,
            GetWalletEvent onGetWallet,
            DepositEvent onDeposit,
            WithdrawEvent onWithdraw,
            ErrorEvent onError
            )
        {
            if (_watching)
            {
                throw new InvalidOperationException("already started");
            }

            _watching = true;

            _client  = client;
            _session = session;

            _moneyNamespaceName = moneyNamespaceName;
            _slot        = slot;
            _onGetWallet = onGetWallet;
            _onDeposit   = onDeposit;
            _onWithdraw  = onWithdraw;
            _onError     = onError;

            _onDeposit.AddListener(DepositAction);
            _onWithdraw.AddListener(WithdrawAction);

            yield return(Refresh());
        }
Ejemplo n.º 3
0
        public JournaledAccountGrainState Apply(DepositEvent @event)
        {
            Balance += @event.Amount;

            _transactions.Add(new Transaction
            {
                Amount      = @event.Amount,
                BookingDate = DateTime.Now
            });

            return(this);
        }
        public void Event_retrieval_should_discriminate_between_timelines()
        {
            var eventDispatcherMockBuilder = new Mock <IEventDispatcher>();
            var sut = new InMemoryEventStore(eventDispatcherMockBuilder.Object);

            var sourceAccountId      = Guid.NewGuid();
            var destinationAccountId = Guid.NewGuid();

            var withdrawalEvent = new WithdrawalEvent()
            {
                CurrentAccountId = sourceAccountId, Amount = 101
            };

            sut.Save(withdrawalEvent);

            var depositEvent = new DepositEvent()
            {
                CurrentAccountId = sourceAccountId, Amount = 42
            };

            sut.Save(depositEvent);

            var moneyTransferredEvent = new MoneyTransferredEvent()
            {
                SourceAccountId = sourceAccountId, DestinationAccountId = destinationAccountId, Amount = 42, TimelineId = Guid.NewGuid()
            };

            sut.Save(moneyTransferredEvent);

            var eventDescriptors = new List <EventMapping>()
            {
                new EventMapping
                {
                    AggregateIdPropertyName = nameof(WithdrawalEvent.CurrentAccountId),
                    EventType = typeof(WithdrawalEvent)
                },
                new EventMapping
                {
                    AggregateIdPropertyName = nameof(MoneyTransferredEvent.SourceAccountId),
                    EventType = typeof(MoneyTransferredEvent)
                }
            };

            var events = sut.RetrieveEvents(sourceAccountId, DateTime.Now, eventDescriptors, null);

            Assert.Single(events);
        }
        public void Saving_two_events_should_allow_for_later_retrieval_of_one_of_them()
        {
            var withdrawalEvent = new WithdrawalEvent()
            {
                CurrentAccountId = Guid.NewGuid(), Amount = 101
            };

            EventStore.Save(withdrawalEvent);
            var depositEvent = new DepositEvent()
            {
                CurrentAccountId = Guid.NewGuid(), Amount = 42
            };

            EventStore.Save(depositEvent);

            var events = EventStore.Find <WithdrawalEvent>(e => e.CurrentAccountId == withdrawalEvent.CurrentAccountId);

            Assert.Single(events);
            Assert.Equal(withdrawalEvent.CurrentAccountId, events.First().CurrentAccountId);
            Assert.Equal(withdrawalEvent.Amount, events.First().Amount);
        }
Ejemplo n.º 6
0
        public static Transaction Create(Cashier cashier, TransactionType type, decimal transactionValue, CancellationToken cancellationToken)
        {
            if (transactionValue < 0)
            {
                throw new Exception("Valor de operação não pode ser negativo");
            }
            if (cashier.Id <= 0)
            {
                throw new Exception("Transação precisa ser associada a cashier");
            }

            Transaction returnObj;

            switch (type)
            {
            case TransactionType.Deposit:
                returnObj = new Transaction(cashier, type, transactionValue);
                var depositEvent = new DepositEvent(returnObj);
                returnObj.AddDomainEvent(depositEvent);
                break;

            case TransactionType.Withdrawal:
                if (cashier.StoredAmount < transactionValue)
                {
                    throw new Exception("Saldo insuficiente");
                }
                returnObj = new Transaction(cashier, type, transactionValue);
                var withdrawEvent = new WithdrawlEvent(returnObj);
                returnObj.AddDomainEvent(withdrawEvent);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(returnObj);
        }
Ejemplo n.º 7
0
        public static IEnumerator Deposit(
            string identifierDepositClientId,
            string identifierDepositClientSecret,
            string moneyNamespaceName,
            string userId,
            int slot,
            float price,
            int value,
            DepositEvent onDeposit,
            ErrorEvent onError
            )
        {
            // このコードは実際にアプリケーションで使用するべきではありません。
            // アプリ内から課金通貨の残高を加算できるようにすることは、事業に多大な悪い影響を及ぼす可能性があります。
            var restSession = new Gs2RestSession(
                new BasicGs2Credential(
                    identifierDepositClientId,
                    identifierDepositClientSecret
                    )
                );
            var error = false;

            yield return(restSession.Open(
                             r =>
            {
                if (r.Error != null)
                {
                    onError.Invoke(r.Error);
                    error = true;
                }
            }
                             ));

            if (error)
            {
                yield return(restSession.Close(() => { }));

                yield break;
            }

            var restClient = new Gs2MoneyRestClient(
                restSession
                );

            yield return(restClient.DepositByUserId(
                             new DepositByUserIdRequest()
                             .WithNamespaceName(moneyNamespaceName)
                             .WithUserId(userId)
                             .WithSlot(slot)
                             .WithPrice(price)
                             .WithCount(value),
                             r =>
            {
                if (r.Error != null)
                {
                    onError.Invoke(r.Error);
                    error = true;
                }
                else
                {
                    onDeposit.Invoke(new EzWallet(r.Result.item), price, value);
                }
            }
                             ));

            yield return(restSession.Close(() => { }));
        }
Ejemplo n.º 8
0
 public void OnDeposit(DepositEvent evt)
 {
     Balance = new SimpleMoney(Balance.Amount + evt.Value.Amount);
 }