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:
dartpopikyardo
2016-09-07 19:50:17 +03:00
parent 8b5b54ed01
commit 7c47d590df
4 changed files with 47 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
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.AccountTransactionInfo;
import net.chrisrichardson.eventstore.javaexamples.banking.common.transactions.TransferState;
@@ -19,7 +20,8 @@ public class AccountInfo {
private List<AccountChangeInfo> changes;
private Map<String, AccountTransactionInfo> transactions;
private String version;
private Date date;
@JsonProperty("date")
private Date creationDate;
private AccountInfo() {
}
@@ -28,7 +30,7 @@ public class AccountInfo {
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.customerId = customerId;
@@ -38,7 +40,7 @@ public class AccountInfo {
this.changes = changes;
this.transactions = transactions;
this.version = version;
this.date = date;
this.creationDate = creationDate;
}
public String getId() {
@@ -73,7 +75,7 @@ public class AccountInfo {
return version;
}
public Date getDate() {
return date;
public Date getCreationDate() {
return creationDate;
}
}

View File

@@ -40,7 +40,7 @@ public class AccountInfoUpdateService {
.set("description", description)
.set("balance", toIntegerRepr(initialBalance))
.push("changes", ci)
.set("date", new Date())
.set("date", getFromEventId(version))
.set("version", version),
AccountInfo.class);
logger.info("Saved in mongo");
@@ -55,12 +55,10 @@ public class AccountInfoUpdateService {
public void addTransaction(String accountId, AccountTransactionInfo ti) {
System.out.println("Start addTransaction for: "+ti.toString());
mongoTemplate.upsert(new Query(where("id").is(accountId)),
new Update().
set("transactions." + ti.getTransactionId(), ti),
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) {
System.out.println("Start updateTransactionStatus "+accountId +" "+transactionId+" "+status);
mongoTemplate.upsert(new Query(where("id").is(accountId)),
new Update().
set("transactions." + transactionId + ".status", status),
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));
}
}

View File

@@ -15,6 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.Date;
import static net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.MoneyUtil.toIntegerRepr;
@@ -40,6 +41,7 @@ public class AccountQueryWorkflow {
String customerId = event.getCustomerId();
String title = event.getTitle();
String description = event.getDescription();
accountInfoUpdateService.create(id, customerId, title, initialBalance, description, eventId);
}
@@ -68,7 +70,6 @@ public class AccountQueryWorkflow {
String accountId = de.getEntityId();
String transactionId = de.getEvent().getTransactionId();
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.DEBITED);
saveChange(de, -1);
}
@@ -77,16 +78,37 @@ public class AccountQueryWorkflow {
String accountId = de.getEntityId();
String transactionId = de.getEvent().getTransactionId();
accountInfoUpdateService.updateTransactionStatus(accountId, transactionId, TransferState.COMPLETED);
saveChange(de, +1);
}
@EventHandlerMethod
public void recordFailed(DispatchedEvent<AccountDebitFailedDueToInsufficientFundsEvent> de) {
String accountId = de.getEntityId();
String transactionId = de.getEvent().getTransactionId();
public void updateDebitTransactionState(DispatchedEvent<DebitRecordedEvent> de) {
String transactionId = de.getEntityId();
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) {
@@ -101,5 +123,4 @@ public class AccountQueryWorkflow {
accountInfoUpdateService.updateBalance(accountId, changeId, balanceDelta, ci);
}
}

View File

@@ -64,12 +64,12 @@ public class GatewayController {
logger.info("request: {}", proxiedRequest);
HttpResponse proxiedResponse = httpClient.execute(proxiedRequest);
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();
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;
}