-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathAccount.java
80 lines (61 loc) · 2.31 KB
/
Account.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package net.chrisrichardson.ftgo.accountingservice.domain;
import io.eventuate.Event;
import io.eventuate.ReflectiveMutableCommandProcessingAggregate;
import io.eventuate.tram.sagas.eventsourcingsupport.SagaReplyRequestedEvent;
import net.chrisrichardson.ftgo.common.Money;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static io.eventuate.EventUtil.events;
public class Account extends ReflectiveMutableCommandProcessingAggregate<Account, AccountCommand> {
public Money getBalance() {
return balance;
}
public boolean accountLimitSufficient(Money money){
return balance.isGreaterThanOrEqual(money);
}
public void setBalance(Money balance) {
this.balance = balance;
}
private final Logger logger = LoggerFactory.getLogger(getClass());
private Money balance;
public List<Event> process(CreateAccountCommand command) {
logger.info(command.getCustomerId().toString());
return events(new AccountCreatedEvent(command.getCustomerId(), command.getInitialBalance()));
}
public void apply(AccountCreatedEvent event) {
this.balance = event.getInitialBalance();
logger.info(this.balance.asString());
logger.info(event.getCustomerId().toString());
}
public List<Event> process(CheckAccountLimitCommandInternal command) {
if(balance.isGreaterThanOrEqual(command.getMoney())){
return events(new AccountLimitSufficientEvent());
}
return events(new AccountLimitExceededEvent());
}
public List<Event> process(AuthorizeCommandInternal command) {
return events(new AccountAuthorizedEvent(command.getOrderTotal()));
}
public List<Event> process(ReverseAuthorizationCommandInternal command) {
return Collections.emptyList();
}
public List<Event> process(ReviseAuthorizationCommandInternal command) {
return Collections.emptyList();
}
public void apply(AccountAuthorizedEvent event) {
if(balance.isGreaterThanOrEqual(event.getMoney())) {
setBalance(getBalance().subtract(event.getMoney()));
}
}
public void apply(AccountLimitSufficientEvent event) {
}
public void apply(AccountLimitExceededEvent event) {
}
public void apply(SagaReplyRequestedEvent event) {
// TODO - need a way to not need this method
}
}