Improved module name <functional-area>-<Command|Query>....
Standalone services now use the Event Store Server (many tests still use the embedded server)
This commit is contained in:
19
java-spring/accounts-command-side-backend/build.gradle
Normal file
19
java-spring/accounts-command-side-backend/build.gradle
Normal file
@@ -0,0 +1,19 @@
|
||||
apply plugin: 'java'
|
||||
|
||||
dependencies {
|
||||
|
||||
compile project(":common-backend")
|
||||
compile "net.chrisrichardson.eventstore.client:eventstore-java-client:$eventStoreClientVersion"
|
||||
compile "org.springframework.boot:spring-boot-starter-data-mongodb:$springBootVersion"
|
||||
|
||||
compile 'com.fasterxml.jackson.core:jackson-core:2.4.3'
|
||||
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.3'
|
||||
compile 'com.fasterxml.jackson.module:jackson-module-scala_2.10:2.4.3'
|
||||
|
||||
testCompile project(":testutil")
|
||||
testCompile "junit:junit:4.11"
|
||||
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
|
||||
testCompile "net.chrisrichardson.eventstore.client:eventstore-jdbc:$eventStoreClientVersion"
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.Event;
|
||||
import net.chrisrichardson.eventstore.EventUtil;
|
||||
import net.chrisrichardson.eventstore.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 java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
public class Account extends ReflectiveMutableCommandProcessingAggregate<Account, AccountCommand> {
|
||||
|
||||
private BigDecimal balance;
|
||||
|
||||
public List<Event> process(OpenAccountCommand cmd) {
|
||||
return EventUtil.events(new AccountOpenedEvent(cmd.getInitialBalance()));
|
||||
}
|
||||
|
||||
public List<Event> process(DebitAccountCommand cmd) {
|
||||
if (balance.compareTo(cmd.getAmount()) < 0)
|
||||
return EventUtil.events(new AccountDebitFailedDueToInsufficientFundsEvent(cmd.getTransactionId()));
|
||||
else
|
||||
return EventUtil.events(new AccountDebitedEvent(cmd.getAmount(), cmd.getTransactionId()));
|
||||
}
|
||||
|
||||
public List<Event> process(CreditAccountCommand cmd) {
|
||||
return EventUtil.events(new AccountCreditedEvent(cmd.getAmount(), cmd.getTransactionId()));
|
||||
}
|
||||
|
||||
public void apply(AccountOpenedEvent event) {
|
||||
balance = event.getInitialBalance();
|
||||
}
|
||||
|
||||
public void apply(AccountDebitedEvent event) {
|
||||
balance = balance.subtract(event.getAmount());
|
||||
}
|
||||
|
||||
public void apply(AccountDebitFailedDueToInsufficientFundsEvent event) {
|
||||
}
|
||||
|
||||
public void apply(AccountCreditedEvent event) {
|
||||
balance = balance.add(event.getAmount());
|
||||
}
|
||||
|
||||
public BigDecimal getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.Command;
|
||||
|
||||
interface AccountCommand extends Command {
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.EventStore;
|
||||
import net.chrisrichardson.eventstore.javaapi.consumer.EnableJavaEventHandlers;
|
||||
import net.chrisrichardson.eventstore.repository.AggregateRepository;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableJavaEventHandlers
|
||||
public class AccountConfiguration {
|
||||
|
||||
@Bean
|
||||
public AccountWorkflow accountWorkflow() {
|
||||
return new AccountWorkflow();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public AccountService accountService(AggregateRepository<Account, AccountCommand> accountRepository) {
|
||||
return new AccountService(accountRepository);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AggregateRepository<Account, AccountCommand> accountRepository(EventStore eventStore) {
|
||||
return new AggregateRepository<Account, AccountCommand>(Account.class, eventStore);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
|
||||
import net.chrisrichardson.eventstore.EntityWithIdAndVersion;
|
||||
import net.chrisrichardson.eventstore.repository.AggregateRepository;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class AccountService {
|
||||
|
||||
private final AggregateRepository<Account, AccountCommand> accountRepository;
|
||||
|
||||
public AccountService(AggregateRepository<Account, AccountCommand> accountRepository) {
|
||||
this.accountRepository = accountRepository;
|
||||
}
|
||||
|
||||
public rx.Observable<EntityWithIdAndVersion<Account>> openAccount(BigDecimal initialBalance) {
|
||||
return accountRepository.save(new OpenAccountCommand(initialBalance));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.EntityIdentifier;
|
||||
import net.chrisrichardson.eventstore.javaapi.consumer.EventHandlerContext;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.transactions.DebitRecordedEvent;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.transactions.MoneyTransferCreatedEvent;
|
||||
import net.chrisrichardson.eventstore.subscriptions.*;
|
||||
import rx.Observable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@EventSubscriber(id="accountEventHandlers")
|
||||
public class AccountWorkflow implements CompoundEventHandler {
|
||||
|
||||
@EventHandlerMethod
|
||||
public Observable<?> debitAccount(EventHandlerContext<MoneyTransferCreatedEvent> ctx) {
|
||||
MoneyTransferCreatedEvent event = ctx.getEvent();
|
||||
BigDecimal amount = event.getDetails().getAmount();
|
||||
EntityIdentifier transactionId = ctx.getEntityIdentifier();
|
||||
|
||||
EntityIdentifier fromAccountId = event.getDetails().getFromAccountId();
|
||||
|
||||
return ctx.update(Account.class, fromAccountId, new DebitAccountCommand(amount, transactionId));
|
||||
}
|
||||
|
||||
@EventHandlerMethod
|
||||
public Observable<?> creditAccount(EventHandlerContext<DebitRecordedEvent> ctx) {
|
||||
DebitRecordedEvent event = ctx.getEvent();
|
||||
BigDecimal amount = event.getDetails().getAmount();
|
||||
EntityIdentifier fromAccountId = event.getDetails().getToAccountId();
|
||||
EntityIdentifier transactionId = ctx.getEntityIdentifier();
|
||||
|
||||
return ctx.update(Account.class, fromAccountId, new CreditAccountCommand(amount, transactionId));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.Aggregate;
|
||||
import net.chrisrichardson.eventstore.EntityIdentifier;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class CreditAccountCommand implements AccountCommand {
|
||||
private final BigDecimal amount;
|
||||
private final EntityIdentifier transactionId;
|
||||
|
||||
public CreditAccountCommand(BigDecimal amount, EntityIdentifier transactionId) {
|
||||
|
||||
this.amount = amount;
|
||||
this.transactionId = transactionId;
|
||||
}
|
||||
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public EntityIdentifier getTransactionId() {
|
||||
return transactionId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.Aggregate;
|
||||
import net.chrisrichardson.eventstore.EntityIdentifier;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class DebitAccountCommand implements AccountCommand {
|
||||
private final BigDecimal amount;
|
||||
private final EntityIdentifier transactionId;
|
||||
|
||||
public DebitAccountCommand(BigDecimal amount, EntityIdentifier transactionId) {
|
||||
|
||||
this.amount = amount;
|
||||
this.transactionId = transactionId;
|
||||
}
|
||||
|
||||
public BigDecimal getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public EntityIdentifier getTransactionId() {
|
||||
return transactionId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class OpenAccountCommand implements AccountCommand {
|
||||
|
||||
private BigDecimal initialBalance;
|
||||
|
||||
public OpenAccountCommand(BigDecimal initialBalance) {
|
||||
this.initialBalance = initialBalance;
|
||||
}
|
||||
|
||||
public BigDecimal getInitialBalance() {
|
||||
return initialBalance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
|
||||
import net.chrisrichardson.eventstorestore.javaexamples.testutil.AbstractEntityEventTest;
|
||||
|
||||
public class AccountEventTest extends AbstractEntityEventTest {
|
||||
|
||||
@Override
|
||||
protected Class<Account> entityClass() {
|
||||
return Account.class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts;
|
||||
|
||||
import net.chrisrichardson.eventstore.CommandProcessingAggregates;
|
||||
import net.chrisrichardson.eventstore.Event;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountOpenedEvent;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
public class AccountTest {
|
||||
|
||||
@Test
|
||||
public void testSomething() {
|
||||
Account account = new Account();
|
||||
BigDecimal initialBalance = new BigDecimal(512);
|
||||
List<Event> events = CommandProcessingAggregates.processToList(account, (AccountCommand)new OpenAccountCommand(initialBalance));
|
||||
|
||||
Assert.assertEquals(1, events.size());
|
||||
Assert.assertEquals(AccountOpenedEvent.class, events.get(0).getClass());
|
||||
|
||||
account.applyEvent(events.get(0));
|
||||
|
||||
Assert.assertEquals(initialBalance, account.getBalance());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user