should fix issue #38

This commit is contained in:
dartpopikyardo
2016-09-14 23:02:15 +03:00
parent 91f6fde5cd
commit 72a4ab1ee4
21 changed files with 292 additions and 29 deletions

View File

@@ -3,23 +3,29 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.
import io.eventuate.Event;
import io.eventuate.EventUtil;
import io.eventuate.ReflectiveMutableCommandProcessingAggregate;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountCreditedEvent;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountDebitFailedDueToInsufficientFundsEvent;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountDebitedEvent;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountOpenedEvent;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class Account extends ReflectiveMutableCommandProcessingAggregate<Account, AccountCommand> {
private BigDecimal balance;
private boolean deleted;
public List<Event> process(OpenAccountCommand cmd) {
return EventUtil.events(new AccountOpenedEvent(cmd.getCustomerId(), cmd.getTitle(), cmd.getInitialBalance(), cmd.getDescription()));
}
public List<Event> process(DeleteAccountCommand cmd) {
return EventUtil.events(new AccountDeletedEvent());
}
public List<Event> process(DebitAccountCommand cmd) {
if(deleted)
return new ArrayList<>();
if (balance.compareTo(cmd.getAmount()) < 0)
return EventUtil.events(new AccountDebitFailedDueToInsufficientFundsEvent(cmd.getTransactionId()));
else
@@ -27,6 +33,9 @@ public class Account extends ReflectiveMutableCommandProcessingAggregate<Account
}
public List<Event> process(CreditAccountCommand cmd) {
if(deleted)
return new ArrayList<>();
return EventUtil.events(new AccountCreditedEvent(cmd.getAmount(), cmd.getTransactionId()));
}
@@ -34,6 +43,10 @@ public class Account extends ReflectiveMutableCommandProcessingAggregate<Account
balance = event.getInitialBalance();
}
public void apply(AccountDeletedEvent event) {
deleted = true;
}
public void apply(AccountDebitedEvent event) {
balance = balance.subtract(event.getAmount());
}

View File

@@ -5,6 +5,7 @@ import io.eventuate.EntityWithIdAndVersion;
import io.eventuate.EventHandlerContext;
import io.eventuate.EventHandlerMethod;
import io.eventuate.EventSubscriber;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerAccountDeleted;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.transactions.DebitRecordedEvent;
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.transactions.MoneyTransferCreatedEvent;
@@ -22,13 +23,7 @@ public class AccountWorkflow {
String fromAccountId = event.getDetails().getFromAccountId();
return ctx.update(Account.class, fromAccountId, new DebitAccountCommand(amount, transactionId)).handle((x, e) -> {
if (e != null) {
e.printStackTrace();
}
return x;
}
);
return ctx.update(Account.class, fromAccountId, new DebitAccountCommand(amount, transactionId));
}
@EventHandlerMethod
@@ -38,13 +33,14 @@ public class AccountWorkflow {
String fromAccountId = event.getDetails().getToAccountId();
String transactionId = ctx.getEntityId();
return ctx.update(Account.class, fromAccountId, new CreditAccountCommand(amount, transactionId)).handle((x, e) -> {
if (e != null) {
e.printStackTrace();
}
return x;
}
);
return ctx.update(Account.class, fromAccountId, new CreditAccountCommand(amount, transactionId));
}
@EventHandlerMethod
public CompletableFuture<EntityWithIdAndVersion<Account>> deleteAccount(EventHandlerContext<CustomerAccountDeleted> ctx) {
CustomerAccountDeleted event = ctx.getEvent();
String accountId = event.getAccountId();
return ctx.update(Account.class, accountId, new DeleteAccountCommand());
}
}

View File

@@ -0,0 +1,7 @@
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
/**
* Created by popikyardo on 9/14/16.
*/
public class DeleteAccountCommand implements AccountCommand {
}