Account aggregate contract definition

This commit is contained in:
Michal Zeman
2019-09-08 12:14:02 +02:00
parent 830ebfbe15
commit 1304089c66
30 changed files with 339 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
package com.mz.reactor.ddd.reactorddd.domain;
package com.mz.reactor.ddd.reactorddd.account.domain;
import com.mz.reactor.ddd.common.api.valueobject.Id;
import com.mz.reactor.ddd.common.api.valueobject.Money;

View File

@@ -1,4 +1,4 @@
package com.mz.reactor.ddd.reactorddd.domain;
package com.mz.reactor.ddd.reactorddd.account.domain;
import org.immutables.value.Value;

View File

@@ -0,0 +1,9 @@
package com.mz.reactor.ddd.reactorddd.account.domain.command;
import com.mz.reactor.ddd.common.api.command.Command;
public interface AccountCommand extends Command {
String aggregateId();
}

View File

@@ -0,0 +1,44 @@
package com.mz.reactor.ddd.reactorddd.account.domain.command;
import com.mz.reactor.ddd.common.api.command.CommandHandler;
import com.mz.reactor.ddd.common.api.command.CommandResult;
import com.mz.reactor.ddd.reactorddd.account.domain.AccountAggregate;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
public class AccountCommandHandler implements CommandHandler<AccountAggregate, AccountCommand> {
private final Map<Class, BiFunction> handlers = new HashMap<>();
public AccountCommandHandler() {
addHandler(CreateAccount.class, this::doCreateAccount);
addHandler(WithdrawMoney.class, this::doWithdrawMoney);
addHandler(DepositMoney.class, this::doDepositMoney);
}
private <K extends AccountCommand> void addHandler(
Class<K> kClass,
BiFunction<AccountAggregate, K, CommandResult> handler
) {
this.handlers.put(kClass, handler);
}
private CommandResult doWithdrawMoney(AccountAggregate aggregate, WithdrawMoney command) {
return null;
}
private CommandResult doCreateAccount(AccountAggregate aggregate, CreateAccount command) {
return null;
}
private CommandResult doDepositMoney(AccountAggregate aggregate, DepositMoney command) {
return null;
}
@Override
public CommandResult execute(AccountAggregate aggregate, AccountCommand command) {
return (CommandResult) handlers.get(command.getClass()).apply(aggregate, command);
}
}

View File

@@ -0,0 +1,15 @@
package com.mz.reactor.ddd.reactorddd.account.domain.command;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface CreateAccount extends AccountCommand {
BigDecimal balance();
static ImmutableCreateAccount.Builder builder() {
return ImmutableCreateAccount.builder();
}
}

View File

@@ -0,0 +1,14 @@
package com.mz.reactor.ddd.reactorddd.account.domain.command;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface DepositMoney extends AccountCommand {
BigDecimal amount();
static ImmutableDepositMoney.Builder builder() {
return ImmutableDepositMoney.builder();
}
}

View File

@@ -0,0 +1,15 @@
package com.mz.reactor.ddd.reactorddd.account.domain.command;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface WithdrawMoney extends AccountCommand {
BigDecimal amount();
static ImmutableWithdrawMoney.Builder builder() {
return ImmutableWithdrawMoney.builder();
}
}

View File

@@ -1,10 +1,13 @@
package com.mz.reactor.ddd.reactorddd.domain.event;
package com.mz.reactor.ddd.reactorddd.account.domain.event;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface AccountCreated extends AccountEvent {
BigDecimal balance();
static ImmutableAccountCreated.Builder builder() {
return ImmutableAccountCreated.builder();

View File

@@ -1,7 +1,6 @@
package com.mz.reactor.ddd.reactorddd.domain.event;
package com.mz.reactor.ddd.reactorddd.account.domain.event;
import com.mz.reactor.ddd.common.api.event.DomainEvent;
import org.immutables.value.Value;
public interface AccountEvent extends DomainEvent {

View File

@@ -1,7 +1,7 @@
package com.mz.reactor.ddd.reactorddd.domain.event;
package com.mz.reactor.ddd.reactorddd.account.domain.event;
import com.mz.reactor.ddd.common.api.event.EventApplier;
import com.mz.reactor.ddd.reactorddd.domain.AccountAggregate;
import com.mz.reactor.ddd.reactorddd.account.domain.AccountAggregate;
import java.util.HashMap;
import java.util.Map;

View File

@@ -0,0 +1,16 @@
package com.mz.reactor.ddd.reactorddd.account.domain.event;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface DepositMoneyFailed extends AccountEvent {
BigDecimal amount();
static ImmutableDepositMoneyFailed.Builder builder() {
return ImmutableDepositMoneyFailed.builder();
}
}

View File

@@ -0,0 +1,14 @@
package com.mz.reactor.ddd.reactorddd.account.domain.event;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface MoneyDeposited extends AccountEvent {
BigDecimal amount();
static ImmutableMoneyDeposited.Builder builder() {
return ImmutableMoneyDeposited.builder();
}
}

View File

@@ -0,0 +1,14 @@
package com.mz.reactor.ddd.reactorddd.account.domain.event;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface MoneyWithdrawn extends AccountEvent {
BigDecimal amount();
static ImmutableMoneyWithdrawn.Builder builder() {
return ImmutableMoneyWithdrawn.builder();
}
}

View File

@@ -0,0 +1,16 @@
package com.mz.reactor.ddd.reactorddd.account.domain.event;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface WithdrawMoneyFailed extends AccountEvent {
BigDecimal amount();
static ImmutableWithdrawMoneyFailed.Builder builder() {
return ImmutableWithdrawMoneyFailed.builder();
}
}

View File

@@ -1,4 +0,0 @@
package com.mz.reactor.ddd.reactorddd.domain.event;
public interface DepositMoneyFailed extends AccountEvent {
}

View File

@@ -1,4 +0,0 @@
package com.mz.reactor.ddd.reactorddd.domain.event;
public interface MoneyDeposited extends AccountEvent {
}

View File

@@ -1,4 +0,0 @@
package com.mz.reactor.ddd.reactorddd.domain.event;
public interface MoneyWithdrawn extends AccountEvent {
}

View File

@@ -1,4 +0,0 @@
package com.mz.reactor.ddd.reactorddd.domain.event;
public interface WithdrawMoneyFailed extends AccountEvent {
}

View File

@@ -0,0 +1,26 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain;
import com.mz.reactor.ddd.common.api.valueobject.Id;
import java.math.BigDecimal;
public class TransactionAggregate {
enum TransactionState {
Created,
Finished,
Failed
}
private Id aggregateId;
private Id fromAccount;
private Id toAccount;
private BigDecimal amount;
public TransactionAggregate(String aggregateId) {
this.aggregateId = new Id(aggregateId);
}
}

View File

@@ -0,0 +1,22 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface TransactionState {
String aggregateId();
String fromAccountId();
String toAccountId();
BigDecimal amount();
static ImmutableTransactionState.Builder builder() {
return ImmutableTransactionState.builder();
}
}

View File

@@ -0,0 +1,20 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain.command;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface CreateTransaction extends TransactionCommand {
String aggregateId();
String fromAccountId();
String toAccountId();
BigDecimal amount();
static ImmutableCreateTransaction.Builder builder() {
return ImmutableCreateTransaction.builder();
}
}

View File

@@ -0,0 +1,6 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain.command;
import com.mz.reactor.ddd.common.api.command.Command;
public interface TransactionCommand extends Command {
}

View File

@@ -0,0 +1,34 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain.command;
import com.mz.reactor.ddd.common.api.command.CommandHandler;
import com.mz.reactor.ddd.common.api.command.CommandResult;
import com.mz.reactor.ddd.reactorddd.transaction.domain.TransactionAggregate;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
public class TransactionCommandHandler implements CommandHandler<TransactionAggregate, TransactionCommand> {
private final Map<Class, BiFunction> handlers = new HashMap<>();
public TransactionCommandHandler() {
addHandler(CreateTransaction.class, this::doCreateTransaction);
}
private <C extends TransactionCommand> void addHandler(
Class<C> kClass,
BiFunction<TransactionAggregate, C, CommandResult> handler
) {
handlers.put(kClass, handler);
}
private CommandResult doCreateTransaction(TransactionAggregate aggregate, CreateTransaction command) {
return (CommandResult) handlers.get(command).apply(aggregate, command);
}
@Override
public CommandResult execute(TransactionAggregate aggregate, TransactionCommand command) {
return null;
}
}

View File

@@ -0,0 +1,19 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain.event;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface TransactionCreated extends TransactionEvent {
String fromAccountId();
String toAccountId();
BigDecimal amount();
static ImmutableTransactionCreated.Builder builder() {
return ImmutableTransactionCreated.builder();
}
}

View File

@@ -0,0 +1,9 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain.event;
import com.mz.reactor.ddd.common.api.event.DomainEvent;
public interface TransactionEvent extends DomainEvent {
String aggregateId();
}

View File

@@ -0,0 +1,20 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain.event;
import org.immutables.value.Value;
import java.math.BigDecimal;
@Value.Immutable
public interface TransactionFailed extends TransactionEvent {
String fromAccountId();
String toAccountId();
BigDecimal amount();
static ImmutableTransactionFailed.Builder builder() {
return ImmutableTransactionFailed.builder();
}
}

View File

@@ -0,0 +1,11 @@
package com.mz.reactor.ddd.reactorddd.transaction.domain.event;
import org.immutables.value.Value;
@Value.Immutable
public interface TransactionFinished extends TransactionEvent {
static ImmutableTransactionFinished.Builder builder() {
return ImmutableTransactionFinished.builder();
}
}

View File

@@ -68,6 +68,7 @@ project(':bank-account:bank-account-application') {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation project(':bank-account:account-domain')
implementation project(':bank-account:transaction-domain')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
@@ -79,7 +80,11 @@ project(':bank-account:account-domain') {
}
}
project(':bank-account:transaction-domain') {}
project(':bank-account:transaction-domain') {
dependencies {
api project(':common-api')
}
}
project(':shared-dependencies') {
dependencies {

View File

@@ -5,7 +5,6 @@ import org.immutables.value.Value;
import java.time.Instant;
import java.util.Optional;
@Value.Immutable
public interface Command {
Optional<String> correlationId();

Binary file not shown.