private-event-sourcing-examples-26
solved comments on previous commit: - changed getting Content-Type header for GatewayController - updated transaction state change logic - renamed "date" field in AccountInfo
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts;
|
package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.accounts.AccountChangeInfo;
|
import net.chrisrichardson.eventstore.javaexamples.banking.common.accounts.AccountChangeInfo;
|
||||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.accounts.AccountTransactionInfo;
|
import net.chrisrichardson.eventstore.javaexamples.banking.common.accounts.AccountTransactionInfo;
|
||||||
import net.chrisrichardson.eventstore.javaexamples.banking.common.transactions.TransferState;
|
import net.chrisrichardson.eventstore.javaexamples.banking.common.transactions.TransferState;
|
||||||
@@ -19,7 +20,8 @@ public class AccountInfo {
|
|||||||
private List<AccountChangeInfo> changes;
|
private List<AccountChangeInfo> changes;
|
||||||
private Map<String, AccountTransactionInfo> transactions;
|
private Map<String, AccountTransactionInfo> transactions;
|
||||||
private String version;
|
private String version;
|
||||||
private Date date;
|
@JsonProperty("date")
|
||||||
|
private Date creationDate;
|
||||||
|
|
||||||
private AccountInfo() {
|
private AccountInfo() {
|
||||||
}
|
}
|
||||||
@@ -28,7 +30,7 @@ public class AccountInfo {
|
|||||||
this(id, customerId, title, description, balance, changes, transactions, version, new Date());
|
this(id, customerId, title, description, balance, changes, transactions, version, new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public AccountInfo(String id, String customerId, String title, String description, long balance, List<AccountChangeInfo> changes, Map<String, AccountTransactionInfo> transactions, String version, Date date) {
|
public AccountInfo(String id, String customerId, String title, String description, long balance, List<AccountChangeInfo> changes, Map<String, AccountTransactionInfo> transactions, String version, Date creationDate) {
|
||||||
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.customerId = customerId;
|
this.customerId = customerId;
|
||||||
@@ -38,7 +40,7 @@ public class AccountInfo {
|
|||||||
this.changes = changes;
|
this.changes = changes;
|
||||||
this.transactions = transactions;
|
this.transactions = transactions;
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.date = date;
|
this.creationDate = creationDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
@@ -73,7 +75,7 @@ public class AccountInfo {
|
|||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date getDate() {
|
public Date getCreationDate() {
|
||||||
return date;
|
return creationDate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class AccountInfoUpdateService {
|
|||||||
.set("description", description)
|
.set("description", description)
|
||||||
.set("balance", toIntegerRepr(initialBalance))
|
.set("balance", toIntegerRepr(initialBalance))
|
||||||
.push("changes", ci)
|
.push("changes", ci)
|
||||||
.set("date", new Date())
|
.set("date", getFromEventId(version))
|
||||||
.set("version", version),
|
.set("version", version),
|
||||||
AccountInfo.class);
|
AccountInfo.class);
|
||||||
logger.info("Saved in mongo");
|
logger.info("Saved in mongo");
|
||||||
@@ -55,12 +55,10 @@ public class AccountInfoUpdateService {
|
|||||||
|
|
||||||
|
|
||||||
public void addTransaction(String accountId, AccountTransactionInfo ti) {
|
public void addTransaction(String accountId, AccountTransactionInfo ti) {
|
||||||
System.out.println("Start addTransaction for: "+ti.toString());
|
|
||||||
mongoTemplate.upsert(new Query(where("id").is(accountId)),
|
mongoTemplate.upsert(new Query(where("id").is(accountId)),
|
||||||
new Update().
|
new Update().
|
||||||
set("transactions." + ti.getTransactionId(), ti),
|
set("transactions." + ti.getTransactionId(), ti),
|
||||||
AccountInfo.class);
|
AccountInfo.class);
|
||||||
System.out.println("End addTransaction for: "+ti.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -74,11 +72,17 @@ public class AccountInfoUpdateService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateTransactionStatus(String accountId, String transactionId, TransferState status) {
|
public void updateTransactionStatus(String accountId, String transactionId, TransferState status) {
|
||||||
System.out.println("Start updateTransactionStatus "+accountId +" "+transactionId+" "+status);
|
|
||||||
mongoTemplate.upsert(new Query(where("id").is(accountId)),
|
mongoTemplate.upsert(new Query(where("id").is(accountId)),
|
||||||
new Update().
|
new Update().
|
||||||
set("transactions." + transactionId + ".status", status),
|
set("transactions." + transactionId + ".status", status),
|
||||||
AccountInfo.class);
|
AccountInfo.class);
|
||||||
System.out.println("End updateTransactionStatus "+accountId +" "+transactionId+" "+status);
|
}
|
||||||
|
|
||||||
|
private Date getFromEventId(String eventId) {
|
||||||
|
String[] s = eventId.split("-");
|
||||||
|
if (s.length != 2) {
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
return new Date(Long.parseUnsignedLong(s[0], 16));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import static net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.MoneyUtil.toIntegerRepr;
|
import static net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.MoneyUtil.toIntegerRepr;
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ public class AccountQueryWorkflow {
|
|||||||
String customerId = event.getCustomerId();
|
String customerId = event.getCustomerId();
|
||||||
String title = event.getTitle();
|
String title = event.getTitle();
|
||||||
String description = event.getDescription();
|
String description = event.getDescription();
|
||||||
|
|
||||||
accountInfoUpdateService.create(id, customerId, title, initialBalance, description, eventId);
|
accountInfoUpdateService.create(id, customerId, title, initialBalance, description, eventId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +70,6 @@ public class AccountQueryWorkflow {
|
|||||||
String accountId = de.getEntityId();
|
String accountId = de.getEntityId();
|
||||||
String transactionId = de.getEvent().getTransactionId();
|
String transactionId = de.getEvent().getTransactionId();
|
||||||
|
|
||||||
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.DEBITED);
|
|
||||||
saveChange(de, -1);
|
saveChange(de, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,16 +78,37 @@ public class AccountQueryWorkflow {
|
|||||||
String accountId = de.getEntityId();
|
String accountId = de.getEntityId();
|
||||||
String transactionId = de.getEvent().getTransactionId();
|
String transactionId = de.getEvent().getTransactionId();
|
||||||
|
|
||||||
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.COMPLETED);
|
|
||||||
saveChange(de, +1);
|
saveChange(de, +1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandlerMethod
|
@EventHandlerMethod
|
||||||
public void recordFailed(DispatchedEvent<AccountDebitFailedDueToInsufficientFundsEvent> de) {
|
public void updateDebitTransactionState(DispatchedEvent<DebitRecordedEvent> de) {
|
||||||
String accountId = de.getEntityId();
|
String transactionId = de.getEntityId();
|
||||||
String transactionId = de.getEvent().getTransactionId();
|
String fromAccountId = de.getEvent().getDetails().getFromAccountId();
|
||||||
|
String toAccountId = de.getEvent().getDetails().getToAccountId();
|
||||||
|
|
||||||
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.FAILED_DUE_TO_INSUFFICIENT_FUNDS);
|
accountInfoUpdateService.updateTransactionStatus(fromAccountId, transactionId, TransferState.DEBITED);
|
||||||
|
accountInfoUpdateService.updateTransactionStatus(toAccountId, transactionId, TransferState.DEBITED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandlerMethod
|
||||||
|
public void updateCreditTransactionState(DispatchedEvent<CreditRecordedEvent> de) {
|
||||||
|
String transactionId = de.getEntityId();
|
||||||
|
String fromAccountId = de.getEvent().getDetails().getFromAccountId();
|
||||||
|
String toAccountId = de.getEvent().getDetails().getToAccountId();
|
||||||
|
|
||||||
|
accountInfoUpdateService.updateTransactionStatus(fromAccountId, transactionId, TransferState.COMPLETED);
|
||||||
|
accountInfoUpdateService.updateTransactionStatus(toAccountId, transactionId, TransferState.COMPLETED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandlerMethod
|
||||||
|
public void recordFailed(DispatchedEvent<FailedDebitRecordedEvent> de) {
|
||||||
|
String transactionId = de.getEntityId();
|
||||||
|
String fromAccountId = de.getEvent().getDetails().getFromAccountId();
|
||||||
|
String toAccountId = de.getEvent().getDetails().getToAccountId();
|
||||||
|
|
||||||
|
accountInfoUpdateService.updateTransactionStatus(fromAccountId, transactionId, TransferState.FAILED_DUE_TO_INSUFFICIENT_FUNDS);
|
||||||
|
accountInfoUpdateService.updateTransactionStatus(toAccountId, transactionId, TransferState.FAILED_DUE_TO_INSUFFICIENT_FUNDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends AccountChangedEvent> void saveChange(DispatchedEvent<T> de, int delta) {
|
public <T extends AccountChangedEvent> void saveChange(DispatchedEvent<T> de, int delta) {
|
||||||
@@ -101,5 +123,4 @@ public class AccountQueryWorkflow {
|
|||||||
|
|
||||||
accountInfoUpdateService.updateBalance(accountId, changeId, balanceDelta, ci);
|
accountInfoUpdateService.updateBalance(accountId, changeId, balanceDelta, ci);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,12 +64,12 @@ public class GatewayController {
|
|||||||
logger.info("request: {}", proxiedRequest);
|
logger.info("request: {}", proxiedRequest);
|
||||||
HttpResponse proxiedResponse = httpClient.execute(proxiedRequest);
|
HttpResponse proxiedResponse = httpClient.execute(proxiedRequest);
|
||||||
logger.info("Response {}", proxiedResponse.getStatusLine().getStatusCode());
|
logger.info("Response {}", proxiedResponse.getStatusLine().getStatusCode());
|
||||||
return new ResponseEntity<>(read(proxiedResponse.getEntity().getContent()), processHeaders(proxiedResponse.getAllHeaders()), HttpStatus.valueOf(proxiedResponse.getStatusLine().getStatusCode()));
|
return new ResponseEntity<>(read(proxiedResponse.getEntity().getContent()), processHeaders(proxiedResponse.getFirstHeader("Content-Type")), HttpStatus.valueOf(proxiedResponse.getStatusLine().getStatusCode()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpHeaders processHeaders(Header[] headers) {
|
private HttpHeaders processHeaders(Header h) {
|
||||||
HttpHeaders result = new HttpHeaders();
|
HttpHeaders result = new HttpHeaders();
|
||||||
Stream.of(headers).filter(h -> h.getName().equalsIgnoreCase("Content-Type")).forEach( h -> result.set(h.getName(), h.getValue()));
|
result.set(h.getName(), h.getValue());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user