public void PayBill(Money m, Guid billId) { //TODO: replace with Apply logic reuse ? need to peek the changes in apply ? GuardNegativeMoney(m, "Cant withdrawal negative amount of money."); GuardNegativeMoney(Amount - m, "Dont have enought money to pay for bill"); var balanceWithdrawalEvent = new PayedForBillEvent(Id, m, billId); RaiseEvent(balanceWithdrawalEvent); }
public void Handle(PayedForBillEvent msg) { _modelBuilder.Add(new TransactionHistory { BalanceId = msg.BalanceId, EventType = typeof(PayedForBillEvent).Name, Event = msg.ToPropsString(), Id = Guid.NewGuid(), TransactionAmount = msg.Amount, Time = DateTimeFacade.Now }); }
private void Apply(PayedForBillEvent e) { _log.Trace("Balance {Id} with amount {Amount} decreased from event by {EventAmount}", Id, Amount, e.Amount); Amount -= e.Amount; }
public void Handle(PayedForBillEvent msg) { _modelBuilder.Modify(msg.BalanceId, b => b.Amount -= msg.Amount.Amount); _publisher.Publish(new BalanceChangeProjectedNotification(msg.BalanceId)); }
public void Given_positive_flow_events_saga_completes() { var sagaStateAggregate = new BuySubscriptionSagaStateAggregate (Guid.NewGuid(), BuySubscriptionSaga.State.SubscriptionSet); var saga = new BuySubscriptionSaga(sagaStateAggregate); var data = new Fixture(); var subscriptionOrderedEvent = data.Build <SubscriptionOrderedEvent>() .With(e => e.OfferId, WellKnownOffers.Catalog.Keys.RandomElement()) .Create(); var subscriptionId = subscriptionOrderedEvent.SuibscriptionId; var offerId = subscriptionOrderedEvent.OfferId; var accountId = subscriptionOrderedEvent.AccountId; var businessId = subscriptionOrderedEvent.BusinessId; saga.Transit(subscriptionOrderedEvent); var createSubscription = ExpectCommand <CreateSubscriptionCommand>(saga); Assert.AreEqual(subscriptionId, createSubscription.SubscriptionId); Assert.AreEqual(offerId, createSubscription.Offer); Assert.AreEqual(BuySubscriptionSaga.State.SubscriptionCreating, saga.DomainState); var offer = WellKnownOffers.Catalog[offerId]; var subscriptionCreatedEvent = new SubscriptionCreatedEvent(subscriptionId, offer); saga.Transit(subscriptionCreatedEvent); var createBillCommand = ExpectCommand <ChargeSubscriptionCommand>(saga); Assert.AreEqual(subscriptionId, createBillCommand.SubscriptionId); var chargeId = createBillCommand.ChargeId; var subscriptionChargedEvent = new SubscriptionChargedEvent(subscriptionId, chargeId, offer.Price); saga.Transit(subscriptionChargedEvent); var charge = new Charge(subscriptionChargedEvent.ChargeId, subscriptionChargedEvent.Price); var billCreateCommand = ExpectCommand <CreateBillCommand>(saga); Assert.AreEqual(new[] { charge }, billCreateCommand.Charges); var billId = billCreateCommand.BillId; var billCreatedEvent = new BillCreatedEvent(billId, new[] { charge }, charge.Amount); saga.Transit(billCreatedEvent); var payBillCommand = ExpectCommand <PayForBillCommand>(saga); Assert.AreEqual(billId, payBillCommand.BillId); Assert.AreEqual(offer.Price, payBillCommand.Amount); Assert.AreEqual(accountId, payBillCommand.AccountId); var billPaidEvent = new PayedForBillEvent(accountId, payBillCommand.Amount, billId); saga.Transit(billPaidEvent); var subscriptionChangeCommand = ExpectCommand <CompleteBusinessSubscriptionOrderCommand>(saga); Assert.AreEqual(subscriptionId, subscriptionChangeCommand.SubscriptionId); Assert.AreEqual(businessId, subscriptionChangeCommand.BusinessId); var orderCompletedEvent = new SubscriptionOrderCompletedEvent(businessId, subscriptionId); saga.Transit(orderCompletedEvent); Assert.AreEqual(BuySubscriptionSaga.State.SubscriptionSet, saga.DomainState); }